#!/bin/bash

function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; }

runid=${1:-Unset}
[[ $runid == "Unset" ]] && bail "Specify runid as parameter"
 
rundir=run/$runid

mkdir $rundir
config_file=config_p4_bench.yml
cd $rundir
cp ../../$config_file .

p4port=`grep 666 $config_file | head -1 | sed -e 's/\s*port:\s*//'`
p4user=`grep user $config_file | sed -e 's/\s*user:\s*//'`

port=`echo "$p4port" | cut -d: -f2`
instance=${port:0:1}

p4 -p $p4port -u $p4user configure show > config.out
sudo ls -l /p4/$instance/ >> config.out

cat config*

sudo mv /p4/$instance/logs/log .
sudo mv /remote/FB_DB/p4/rep/$instance/logs/log rep.log
sudo chmod +r rep.log
python2.7 ~/bin/log2sql.py -d run -s log
python2.7 ~/bin/log2sql.py -d run -s rep.log

sqlite3 -header run.db <<EOF
.mode column
select cmd, count(cmd),  round(cast(avg(completedLapse) AS DECIMAL(9,2)), 2) as "Avg Time", 
round(cast(max(completedLapse) AS DECIMAL(9, 2)), 2) as "Max Time",
cast(sum(completedLapse) as decimal(9,2)) as "Sum Time",
round(cast(avg(rpcsizeout) AS DECIMAL(9,2)), 2) as "Avg Sent(MB)",
cast(sum(rpcsizeout) AS DECIMAL(9,2)) as "Sum Sent(MB)",
round(cast(sum(rpcsizeout) as decimal(9,2)) / cast(sum(completedLapse) as decimal(9,2)), 2) as "Rate MB/s"
FROM process where (cmd = "user-sync") and completedLapse > 1;

select min(starttime), max(endtime), count(completedlapse) from process where cmd = "user-sync" and completedlapse > 1;
EOF

