#!/bin/bash

#
# This script reads inj-*.log from current directory and builds the gnuplot
# script required to graph the benchmark report. The 5 best values of each
# report are ignored, then the remaining 20 best ones are averaged. This
# ensures that occasional out-of-range measurement are evicted, while keeping
# a good average stability. It is suited for 1-minute runs.
#

if [ $# -eq 0 ]; then
  title_line="set title \"$(date +%Y/%m/%d): haproxy-1.3.14-82, 16kB buffers, nbproc 1, Linux-2.6.22-wt3,\\\nCore2Duo 2.66 GHz, intel X38, 2*Myri-10G PCI-Express NICs\""
else
  title_line="set title \"$(date +%Y/%m/%d): $*,\\\nCore2Duo 2.66 GHz, intel X38, 2*Myri-10G PCI-Express NICs\""
fi

name=( 64 512 1k 2k 3k 5k 10k 20k 32k 50k 100k 200k 500k \
       1m 2m 5m 10m 20m 50m 100m )

size=( 64 512 1024 2048 3072 5120 10240 20480 32768 51200 102400 204800 512000 \
       1048576 2097152 5242880 10485760 20971520 52428800 104857600 )

nbval=${#size[@]}
kbps=( )
hits=( )
kbps_scale=0
hits_scale=0

i=0
while [ $i -lt $nbval ]; do
  x=$(cat inj-${name[$i]}.log |\
      awk '/^[0-9]/{print $11}' | sort -nr | tail +6 | head -20 | \
      awk '{x+=$1} END {printf "%d\n",x/NR*8}' \
     )
  kbps[$i]=$x
  [ $x -gt $kbps_scale ] && kbps_scale=$x

  x=$(cat inj-${name[$i]}.log |\
      awk '/^[0-9]/{print $7}' | sort -nr | tail +6 | head -20 | \
      awk '{x+=$1} END {printf "%d\n",x/NR}' \
     )
  hits[$i]=$x
  [ $x -gt $hits_scale ] && hits_scale=$x

  i=$((i+1))
done

# avoid overlapping ytics. Use /40 for X11, /30 for png.
ytics_height=$(($hits_scale/30))
y2tics_height=$(($kbps_scale/30))

size_line="size(n) = "
hits_line="hits(n) = "
kbps_line="kbps(n) = "

xtics_line="set xtics (\"\" 0"
ytics_line="set ytics (\"\" 0"
ytics_next=0

i=0
while [ $i -lt $nbval ]; do
  size_line="${size_line}(n<$((i+1)))?${size[$i]}:"
  hits_line="${hits_line}(n<$((i+1)))?${hits[$i]}:"
  kbps_line="${kbps_line}(n<$((i+1)))?${kbps[$i]}:"

  xtics_line="${xtics_line}, \"${name[$i]}\" ${size[$i]}"
  if [ $i -eq 0 ] || [ ${hits[$i]} -lt $ytics_next ]; then
    if [ ${hits[$i]} -ne 0 ]; then
      ytics_line="${ytics_line}, ${hits[$i]}"
      ytics_next=$((${hits[$i]} - $ytics_height))
    fi
  fi
  i=$((i+1))
done

size_line="${size_line}0"
hits_line="${hits_line}0"
kbps_line="${kbps_line}0"

xtics_line="${xtics_line})"
ytics_line="${ytics_line})"

# Y2 tics must be checked backwards so that we can reserve
# place for high values. We also take this opportunity to
# add a few reference tics.

y2tics=( ${kbps[@]} ) ;# 2000000 5000000
y2tics=( $( IFS=$'\n'; echo "${y2tics[*]}" | sort -nu) )
nby2tics=${#y2tics[@]}

y2tics_line="set y2tics (\"\" 0"
y2tics_next=0

i=$nby2tics
while [ $i -gt 0 ]; do
  i=$((i-1))
  if [ $i -eq $((nby2tics-1)) ] || [ ${y2tics[$i]} -lt $y2tics_next ]; then
    if [ ${y2tics[$i]} -ne 0 ]; then
      y2tics_line="${y2tics_line}, \"$[${y2tics[$i]}/1000] Mbps\" ${y2tics[$i]}"
      y2tics_next=$((${y2tics[$i]} - $y2tics_height))
    fi
  fi
done

y2tics_line="${y2tics_line})"


echo "# $nbval values"
echo $size_line
echo $hits_line
echo $kbps_line
echo $xtics_line
echo $ytics_line
echo $y2tics_line

echo "set yrange [0:$((hits_scale*102/100))]"
echo "set y2range [0:$((kbps_scale*102/100))]"

cat << EOF
set parametric
set logscale x
set grid xtics ytics y2tics
set xtics axis out scale 0.5,0.25 nomirror norotate  offset character 0, 0, 0  font "courb,8"
set ytics border out scale 0.5,0.25 nomirror norotate  offset character 0, 0, 0  font "courb,8"
set y2tics border out scale 0.5,0.25 nomirror norotate  offset character 0, 0, 0  font "courb,8"

set xlabel "Object Size (bytes)"  offset character 0, 0, 0 font "courb,8" textcolor lt -1 norotate
set ylabel "HTTP Connections/s"  offset character 0, 0, 0 font "courb,8" textcolor lt -1 rotate by 90
set y2label "HTTP Bandwidth"  offset character 0, 0, 0 font "courb,8" textcolor lt -1 rotate by 90

set key title ""
set key inside right center vertical Right noreverse noinvert enhanced samplen 3 spacing 1 width 1 height 1 autotitles box linetype -1 linewidth 0.500
EOF

echo "set terminal png truecolor nocrop enhanced font courbi 8 size 750,250 xffffff x000000 xa0a0a0 xff0000 x00c000 x0080ff xc000ff x00eeee xc04000 xeeee00 x2020c0 xffc020 x008040 xa080ff x804000 xff80ff x00c060 x00c0c0 x006080 xc06080 x008000 x40ff80 x306080 x806000 x404040 x408000 x000080 x806010 x806060 x806080 x0000c0 x0000ff x006000 xe3b0c0 x40c080 x60a0c0 x60c000 x60c0a0 x800000 x800080 x602080 x606060 x202020 x204040 x204080 x608020 x608060 x608080 x808040 x208020 x808080 xa0a0a0 xa0d0e0 xc02020 x008080 xc06000 x80c0e0 xc060c0 xc08000 xc08060 xff4000 xff4040 x80c0ff xff8060 xff8080 xc0a000 xc0c0c0 xc0ffc0 xff0000 xff00ff xff80a0 xc0c0a0 xff6060 x00ff00 xff8000 xffa000 x80e0e0 xa0e0e0 xa0ff20 xc00000 xc000c0 xa02020 xa020ff x802000 x802020 x804020 x804080 x8060c0 x8060ff x808000 xc0c000 xff8040 xffa040 xffa060 xffa070 xffc0c0 xffff00 xffff80 xffffc0 "
echo "set output 'bench.png'"
echo "$title_line"
echo "plot [0:$(($nbval-1))] size(t),kbps(t) with linespoints lw 2 pt 5 ps 1 axes x1y2 title \"Bandwidth\", " \
     "size(t),hits(t) with linespoints lw 2 pt 9 ps 1 axes x1y1 title \"Connections/s\""
#echo "pause 10"
