#!/bin/bash

title="HTTPS via HTTP/2.0"
wget_options="-q --no-config -O/dev/null"
wget_version=$(wget --version|head -1|cut -d' ' -f3)
wget2_options="-q --no-config -O/dev/null"
wget2_version=$(../src/wget2_noinstall --version 2>/dev/null|head -1|cut -d' ' -f3)
curl_options="-s --cert-status --http2 -o/dev/null"
curl_version=$(curl --version|head -1|cut -d' ' -f2)

NCHECKS=3
MINX=1
MAXX=3
SLEEP=0.5

rm -f wget.data wget2.data curl.data

for ((nreq=$MINX;nreq<=$MAXX;nreq++)); do
  urls=""
  for ((i=1;i<=nreq;i++)); do
    urls="$urls https://www.google.com/a${i}.html"
  done

  # warmup run (e.g. caching OCSP responses)
  if [ $nreq -eq 1 ]; then
    wget ${wget_options} $urls
    ../src/wget2_noinstall ${wget2_options} $urls
    curl ${curl_options} $urls >/dev/null 2>&1
  fi

  # timings for wget
  for ((i=0;i<$NCHECKS;i++)); do
    t1=$(date +%s%3N)
    wget ${wget_options} $urls
    t2=$(date +%s%3N)
    echo $nreq $((t2-t1))
    sleep $SLEEP
  done >>wget.data

  # timings for wget2
  for ((i=0;i<$NCHECKS;i++)); do
    t1=$(date +%s%3N)
    ../src/wget2_noinstall ${wget2_options} $urls
    t2=$(date +%s%3N)
    echo $nreq $((t2-t1))
    sleep $SLEEP
  done >>wget2.data

  # timings for curl
  for ((i=0;i<$NCHECKS;i++)); do
    t1=$(date +%s%3N)
    curl ${curl_options} $urls >/dev/null 2>&1
    t2=$(date +%s%3N)
    echo $nreq $((t2-t1))
    sleep $SLEEP
  done >>curl.data
done


cat <<EOF | gnuplot
  set terminal svg
  set output "$0.svg"

  set title "${title}\n\
 Debian Sid amd64, Intel i3-2100 at 3.10GHz\n\
 50MBit/s DSL, RTT 35ms to www.google.com\n\
 wget ${wget_version} options: ${wget_options}\n\
 wget2 ${wget2_version} options: ${wget2_options}\n\
 curl ${curl_version} options: ${curl_options}"

  # aspect ratio for image size
  #set size 1,0.7

  # y-axis grid
  set grid y

  # x-axis label
  set xlabel "number of URLs"

  #y-axis label
  set ylabel "wall time (ms)"

  plot \
    "wget2.data" using 1:2 smooth sbezier with lines title "wget2", \
    "wget.data" using 1:2 smooth sbezier with lines title "wget", \
    "curl.data" using 1:2 smooth sbezier with lines title "curl"

EOF
