#!/bin/sh -eu
input=$1

suffixes='g1.4 g2.2 g2.3 n2.3 ossl osno cust'
count=$( grep -c 'ENC-g1[.]4:[[:blank:]]*User time' "$input" )
cmin=$(( count / 20 + 1 ))      # bottom 5% (1-based)
cmax=$(( count - cmin + 1 ))    # top 5%
cnew=$(( cmax - cmin + 1 ))     # used count
echo "(taking average $cmin..$cmax results from '$input')" >&2

get_avg() {
    src=$(grep "^$dir-$suffix:[[:blank:]]*$1" "$input")
    if test $(echo "$src" | wc -l) != $count; then
        echo "wrong count for $1 search" >&2
        exit 1
    fi
    values=$(echo "$src" | sed -e 's/.*: //' | LC_ALL=C sort -V)
    echo "$values" | sed -e "$cmin,$cmax!d" | awk -v c=$cnew \
        '{s+=$1}END{if(c==NR){print int('$2'*s/c)}}'
}

echo ';Encryption;;;;Decryption;;;;'
echo ";user (ms);sys (ms);total (ms);mem (10K);\
user (ms);sys (ms);total (ms);mem (10K);"
for suffix in $suffixes; do
    echo -n "$suffix;"
    for dir in ENC DEC; do
        utime=$(get_avg "User time" 1000); stime=$(get_avg "System time" 1000)
        echo -n "$utime;$stime;$((utime + stime));"
        echo -n "$(get_avg "Maximum resident set size" 0.1);"
    done
    echo
done
