#!/bin/bash

# SPDX-FileCopyrightText: 2021-2022 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
# SPDX-License-Identifier: GPL-3.0-or-later

# 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

TMP=`pwd`/tmp
export TMP
mkdir -p $TMP

if ! $TMP/bin/parallel-20140722 --version; then
    mkdir -p $TMP/ftp
    (
	cd $TMP/ftp
	wget -c ftp://ftp.gnu.org/old-gnu/parallel/p*
	wget -c ftp://mirrors.dotsrc.org/gnu/parallel/p*
	parallel 'gpg --auto-key-locate keyserver --keyserver-options auto-key-retrieve {}' ::: *.sig
	parallel --plus 'tar xvf {.} && cd {...} && ./configure --prefix '$TMP'/ftp/{.}-bin && make && make install' ::: *sig
	perl -i -pe 's/qw\(keys/(keys/' parallel*/src/parallel
	perl -i -pe 's/defined(\@/(\@/' parallel*/src/parallel
	perl -i -pe 's/defined\s+\@/ \@/' 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
    MHZ=1700
    echo Running $OUTER test with $INNER jobs each on $CORES cores
    
    # Force cpuspeed at 1.7GHz - seems to give tighter results
    #forever 'parallel sudo cpufreq-set -g performance -u '$MHZ'MHz -d '$MHZ'MHz -c{} ::: {0..3};sleep 10' &

    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("$TMP/joblog$CORES-$INNER-$OUTER.csv",sep="\t");
      jl\$Command <- as.factor(substr(jl\$Command,
                                      nchar(as.character(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 overhead for different versions\n$OUTER trials each running $INNER jobs");
_
    cp $TMP/boxplot.pdf $TMP/boxplot-j$CORES-${MHZ}MHz-$OUTER-${INNER}v$VERSION.pdf
    evince $TMP/boxplot.pdf
}

#measure 3000 1000 2 1
measure 30 10 50% 1
measure 300 100 50% 1
measure 3000 1000 50% 1