#!/bin/sh

DIR=/tmp/LMbench/bin/i686-pc-linux-gnu

function launch () {
  for i in $(seq 1 $1)
  do
	# spill is just a stupid process that eat CPU time
	./spill &
  done
}


function do_bench_ctx () {

  n=0
  max=50

  while [ $n -le $max ]
  do
	echo
	echo "# latency context switching with $(printf %03d $n) process"
  	for i in $(seq 0 2)
  	do
  		$DIR/lat_ctx -s 0 2 2>&1 | grep '^2' | cut -f 2 -d' '
  	done

	if [ $n -lt $max ]; then
  		launch 10
	fi
	n=$((n+10))
  done
  killall spill
}

function do_bench() {
uname -r

echo "# latency process"
for j in fork exec shell
do
	for i in $(seq 0 4)
	do
		$DIR/lat_proc $j
	done
done
echo

echo "# latency select"
for i in 10 100 250 500 1000; do $DIR/lat_select file $i; done
echo
for i in 10 100 250 500 1000; do $DIR/lat_select tcp $i; done

do_bench_ctx
}

do_bench 2>&1 | tee result-ctxt-$(uname -r)
echo Done
