#!/bin/bash

# We need the node list, but don't overwrite the nodemap generated by run-all.sh
apt_gen_nodes=1
overwrite_nodemap=0
source $(dirname $0)/apt-params.sh

# Process the output of each machine in /tmp/$apt_logdir.
# Prints per-machine and average throughput

#
# Copy per-machine outputs to $apt_combined_logdir
#
rm -rf $apt_combined_logdir
mkdir $apt_combined_logdir

for i in $apt_nodes; do
	echo "Fetching output of node $i"
	node_name="akalianode-$i.RDMA.fawn.apt.emulab.net"

	# We don't have the node ID to HoTS machine ID mapping here, so use the
	# remote filename as the target filename
	scp $node_name:$apt_logdir/out* $apt_combined_logdir/ \
		1>/dev/null 2>/dev/null &
done
wait

#
# Process the accumulated output files
#
for filename in $apt_combined_logdir/*; do
	# Print a machine's throughput
	machine_tput_string=`tail -1000 $filename | \
		grep "Machine commit tput" | tail -1`
	echo $filename " " $machine_tput_string

	# Assemble a string with each machine's throughput
	machine_tput_double=`echo $machine_tput_string | cut -d' ' -f 5`
	tputs_string=$tputs_string" "$machine_tput_double

	num_machines=`expr $num_machines + 1`
done

echo "tputs_string: $tputs_string"

# Take the average of the combined throughput string
avg_tput=`echo $tputs_string | awk '{
	s = 0; \
	for(i = 1; i <= NF; i++) \
		s += $i; \
	print s / NF; \
	}'`

echo "Average throughput ($num_machines) machines = $avg_tput M/s"
