#!/bin/bash

# Create a boxplot of running 1000 jobs 10 times on all released
# versions of GNU Parallel

# test
#  --cpus-as-cores (virker ikke)
#  hvor stor forskel mlm 2 run
#  hvor stor forskel mlm 1000/3000/10000
#  hvor stor forskel mlm 10/30/100

# Non-fixed cpu-speed: 50% spread=1-2 ms
# Fixed cpu-speed: 50% spread=0.7-1.5 ms
# 4-cpu: 30% faster: 9 ms -> 6 ms


if ! /tmp/bin/parallel-20140722 --version; then
  wget -c ftp://ftp.gnu.org/old-gnu/parallel/p*
  wget -c ftp://ftp.uni-kl.de/pub/gnu/parallel/p*
  parallel 'gpg --auto-key-locate keyserver --keyserver-options auto-key-retrieve {}' ::: *.sig
  parallel --plus 'tar xvf {.} && cd {...} && ./configure --prefix /tmp/{.}-bin && make && make install' ::: *sig
  perl -i -pe 's/qw\(keys/(keys/' parallel*/src/parallel
  mkdir /tmp/bin
  parallel cp {} /tmp/bin/'{=s:/.*::=}' ::: parallel*/src/parallel
fi

measure() {
    # 100: Much jumping
    # 300: Same sort order every time
    # 1000: Same sort order every time
    OUTER=$1
    INNER=$2
    CORES=$3
    VERSION=$4
    GHZ=3.0

    # Force cpuspeed at 1.7GHz - seems to give tighter results
#    forever 'sleep 10;parallel sudo cpufreq-set -f ${GHZ}GHz -c{} ::: {0..7}' &
#    forever 'sleep 10;parallel sudo cpufreq-set -f 1700MHz -c{} ::: {0..7}' &

    PATH=/tmp/bin:$PATH
    cd /tmp/bin
    ls parallel-* | parallel --shuf -j$CORES --joblog ~/tmp/joblog$CORES-$INNER-$OUTER.csv 'seq '$INNER' | {2} true' :::: <(seq $OUTER) - 

    killall forever

    Rscript - <<_
      jl<-read.csv("$HOME/tmp/joblog$CORES-$INNER-$OUTER.csv",sep="\t");
      jl\$Command <- as.factor(substr(jl\$Command, 12, nchar(as.character(jl\$Command))-5))
      pdf("/tmp/boxplot.pdf");
      par(cex.axis=0.5);
      boxplot(JobRuntime/$INNER*1000~Command,data=jl,las=2,outline=F,
              ylab="milliseconds/job",main="GNU Parallel performance\n$OUTER trials each running $INNER");
_
    cp /tmp/boxplot.pdf $HOME/tmp/boxplot-j$CORES-${GHZ}ghz-$OUTER-${INNER}v$VERSION.pdf
    evince /tmp/boxplot.pdf
}

measure 3000 1000 2 1