# $Id: bsort,v 1.3 1999/03/02 16:38:15 roca Exp $
#
# gnusort
#
#	Created on  : 1994
#	Author	    : J.D. Sorace
#	Modified on : 11/04/95
#	Author	    : V. Roca
#
#	Parses the data file generated by bencht and generates data and
#	dem files to be used by gnuplot.
#	Modified for the ETNA project.

# (c) Copyright 1998 - VR: this tool is provided as is, without any
# warranty.
# Permission to use, copy and modify is provided for non commercial
# purposes as long as this notice appears on all copies.


#
# Parse bencht file and create a single sortfile file.
#
#	The format of bencht file is a list of:
#
#		Header generated by bencht (see below for format).
#		Then for each TSDU length, a list of:
#			Header generated by bench/benchd.
#			Performance traces of bench/benchd.
#			trailer generated by bench/benchd including the
#				averT and averD measures used here.
#
#	The format of the generated sortfile is a list of:
#		title "i curve_name with linespoints"
#		iT #	Test co_bench date: 436.1_11H00
#		iT #	Host: etna01 Peerhost: localhost
#		iT #	System: AIX 9438A_up-UP Provider: CCA.tcp
#		iT #	API: xti.t_sndseg Fork: 1 CPU: 1
#		iT x1 y1
#		iT x2 y2
#		...
#	for throughput, and:
#		iD #	Test co_bench date: 436.1_11H00
#		iD #	Host: etna01 Peerhost: localhost
#		iD #	System: AIX 9438A_up-UP Provider: CCA.tcp
#		iD #	API: xti.t_sndseg Fork: 1 CPU: 1
#		iD x1 y1
#		iD x2 y2
#		...
#	for delay (i is the current curve number, we reuse the title of
#	the throughput curve).
#
# WARNING: on Solaris, use nawk, on Linux use awk !
awk '
	#
	# Search in record rec the string str and return the value
	# that follows the = char.
	#
	function getval(rec,str) {
		n=split(rec,A,str);
		if (n<2) {
			printf ("gnusort: ERROR, string %s not found in %s\n",
				str,rec);
			exit;
		}
		n=split(A[2],B,"=");
		if (n<2) {
			printf("gnusort: ERROR, char = not found in %s\n",
				str,rec);
			exit;
		}
		return B[2];
	}
	BEGIN {
		count  = 0;		# number of curves
		cumulT = 0.0;		# cumulative throughput over
					# the connections
		cumulD = 0.0;		# cumulative delay over
					# the connections
		nbcumul = 0;		# number of connections
		style  = "with linespoints";
	}
	END {
		# Record the number of curves to plot.
		# This is the only way to pass the info to the shell.
		#
		print "count " count
	}
	/Test/ {
		#
		# Parse the fixed-size control part generated by bencht.
		# Format and fields (example):
		#	Test co_bench date: 436.1_11H00 \
		#		$2		$4
		#	Host: etna01 Peerhost: localhost \
		#		$6		$8
		#	System: AIX 9438A_up-UP Provider: CCA.tcp
		#		$10	$11		   $13
		#	API: xti.t_sndseg Fork: 1 CPU: 1
		#		$15	       $17    $19
		#

		appli = $2;
#		date = $4;
#		host = $6;
#		peerhost = $8;
#		sysname = $10$11;
		provider = $13;
		api = $15;
		conn_nb = $17;
		cpu_nb = $19;

		# VR 05/09/94:	curve legend.
		#
		curvename = appli"."conn_nb"."api"."provider;

		# Curve counter.
		# Used later to partition the file in its various .dat files.
		#
		count++;

		# Create the title command of the future curves (throughput
		# and delay).
		#
		printf ("title \"%s %s\" %s \n", count, curvename, style);

		# Save bencht header to identify precisely the future
		# .dat file.
		#
		printf ("%s# %s\n", count, $0);

		# Now reset the cumulative throughput variables.
		#
		cumulT = 0.0;
		cumulD = 0.0;
		nbcumul = 0.0;
	}
	/CLIENT length of msg/ {
		Length = $6;
	}
	/averT/ {
		Throughput=getval($0,"averT");
		Delay=getval($0,"averD");
		cumulT += Throughput;
		cumulD += Delay;
		nbcumul++;
	}
	/end bench/ {
		# All the connections are now closed and bench has finished.
		# Now print the average throughput/delay of the various
		# connections.
		# The various curves are distinguished by:
		#	iT  -> throughput
		#	iD  -> delay in ms
		#
		if (nbcumul > 0.0) {
			# Throughput
			cumulT /= nbcumul;
			printf ("%sT %s %f\n",count, Length, cumulT);
			# Delay
			cumulD /= nbcumul;
			printf ("%sD %s %f\n",count, Length, cumulD);
		}
		# Sanity check on the trace file.
		#
		if (nbcumul != conn_nb) {
			printf ("gnusort: ERROR, %d conn announced, %d found\n",
				conn_nb, nbcumul);
			exit;
		}
		cumulT = 0.0;
		cumulD = 0.0;
		nbcumul = 0.0;
	}
' > /tmp/sortfile$$


#
# Get the count variable. Saved in the last line of /tmp/sortfile$$
#
count=`awk '/count/ { print $2 }' /tmp/sortfile$$`


#
# Now split the single sortfile in several .dat.T and .dat.D files, one for
# each curve (identified by the begining count number).
#
while true
do
	if [ "$count" = "0" ]
	then
		break
	fi
	#
	# Read the common sortfile$$ file, extract the lines beginning with
	# $countT and $countD, remove this number, and save them in a different
	# sortfile$$.dat.T.$count and sortfile$$.dat.D.$count file.
	# 
	cat /tmp/sortfile$$ | grep "^${count}T" 2>/dev/null | sed -e "s/^${count}T//" > /tmp/sortfile$$.dat.T.$count
	cat /tmp/sortfile$$ | grep "^${count}D" 2>/dev/null | sed -e "s/^${count}D//" > /tmp/sortfile$$.dat.D.$count
	count=`expr $count - 1 `
done


#
# Now let do the throughput .dem file
#
echo "set title  \"Throughput Measures\"" > /tmp/sortfile$$.T.dem
echo "set xlabel \"TSDU Length\"" >> /tmp/sortfile$$.T.dem
echo "set ylabel \"kBytes/s\"" >> /tmp/sortfile$$.T.dem
echo "set autoscale" >> /tmp/sortfile$$.T.dem
echo "set nolabel" >> /tmp/sortfile$$.T.dem
echo "set grid" >> /tmp/sortfile$$.T.dem

# Issue the plot command with all the curves.
# VR dec 97: add -n for GNU echo
#
echo -n "plot " >> /tmp/sortfile$$.T.dem
cat /tmp/sortfile$$ | awk '
	BEGIN {
		count = 1
	}
	/^title/ {
		if (count == 1) {
			printf ("\"%s.dat.T.%s\" %s", file, count, $0)
		} else {
			printf (", \"%s.dat.T.%s\" %s", file, count, $0)
		}
		count++
	}
	END {
		printf ("\n")
	}
' file=/tmp/sortfile$$ >> /tmp/sortfile$$.T.dem

echo "pause -1 \"Hit return to continue\"" >>/tmp/sortfile$$.T.dem


#
# Now let do the delay .dem file
#
echo "set title  \"Delay Measures\"" > /tmp/sortfile$$.D.dem
echo "set xlabel \"TSDU Length\"" >> /tmp/sortfile$$.D.dem
echo "set ylabel \"ms\"" >> /tmp/sortfile$$.D.dem
echo "set autoscale" >> /tmp/sortfile$$.D.dem
echo "set nolabel" >> /tmp/sortfile$$.D.dem
echo "set grid" >> /tmp/sortfile$$.D.dem

# Issue the plot command with all the curves.
# VR dec 97: add -n to suppress \n
#
echo -n "plot " >> /tmp/sortfile$$.D.dem
cat /tmp/sortfile$$ | awk '
	BEGIN {
		count = 1
	}
	/^title/ {
		if (count == 1) {
			printf ("\"%s.dat.D.%s\" %s", file, count, $0)
		} else {
			printf (", \"%s.dat.D.%s\" %s", file, count, $0)
		}
		count++
	}
	END {
		printf ("\n")
	}
' file=/tmp/sortfile$$ >> /tmp/sortfile$$.D.dem

echo "pause -1 \"Hit return to continue\"" >>/tmp/sortfile$$.D.dem


# And now, let's see it...
#
xterm -e gnuplot -fn 5x8 /tmp/sortfile$$.T.dem
xterm -e gnuplot -fn 5x8 /tmp/sortfile$$.D.dem
# TERM=xterm;DISPLAY=129.183.144.144:0;xgnuplot /tmp/sortfile$$.load

# rm /tmp/sortfile$$*


