#!/bin/bash

#
# This variant of sdperfhist_c uses the "coarsed" coarsed timings from report (intended for autotests)
#

[[ $# -lt 1 ]] && { echo -e "Usage:\n\t$0 <CurrentRunTestName>\nExamples:\n\t$0 baseline\n\t$0 with-pruning"; exit 1; }

SNAME="$1"
OPNAME="sdperfhistC"
IFILE="log/current-rtyserver-stdout.log"

TS=$(stat -c %Y $IFILE) || exit 1
NAME="$SNAME-$OPNAME-$TS"
OFILE="$NAME-report.txt"

grep -e '^Query' -e '^TOTALS' log/current-rtyserver-stdout.log >  $NAME.log.tsv
grep -e '^Query' $NAME.log.tsv | sed 's:\t::' | cut -f1,6 | sort -nk2 | awk -v OFS='\t' '{print NR, $0;}' > tmp.txt
{ 
    awk -v NSTR=$(cat tmp.txt | wc -l) '{print int($1*1000.0/NSTR), $0;}' tmp.txt | sort -unk1 \
        | awk '$1 <= 30 && $1 % 5 == 0 || $1 % 50 == 0 || $1 > 950 && $1 % 5 == 0 || $1 >= 980' \
        | awk -v OFS='\t' '{q=sprintf("%.1f%%",$1/10.0); $1=""; print q, $0;}' | cut -f1,4-; 
    grep ^TOTALS $NAME.log.tsv; 
} | column -t > $OFILE
rm tmp.txt

echo "$OPNAME report saved as $OFILE"

