#! /bin/sh

# Copyright 1997,1998 Alexandre Oliva <oliva@dcc.unicamp.br>

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Runs the Guarana performance test program

# The following program names are assumed:
#  jg: Kaffe+Guarana with JIT
#  jk: Kaffe with JIT
#  ig: Kaffe+Guarana without JIT
#  ik: Kaffe without JIT
#  java: Sun JDK

# usage: ./run

rm -rf perftest.$$
trap 'rm -rf perftest.$$' 1 2 15
timecmd () {
    cmdname=$1; shift
    { { time -p ${1+"$@"}; } |
      awk '/JIT/ { jittime += $4; next; }
	   END {
	     if (jittime) print "'"$cmdname"'-JIT: " jittime/1000.0;
	   }'; } 2>&1 |
    awk '/^user [0-9.]*$/ { usertime=0+$2; next; }
	 /^(real|sys) [0-9.]*$/ { next; }
	 $1 == "'"$cmdname"'-JIT:" { jitline=$0; next; }
    	 { print; }
	 END {
	     print "'"$cmdname"': " usertime;
	     if (jitline) print jitline;
	 }' >&2
}
    
test -f MetaLogger.class ||
{ test -f ../tutorial-src/MetaLogger.class &&
  CLASSPATH=${CLASSPATH-.}:../tutorial-src && export CLASSPATH; } ||
{ echo MetaLogger.class is missing >&2 && exec false; }
if test -f pizza.jar; then
  CLASSPATH=${CLASSPATH-.}:`pwd`/pizza.jar && export CLASSPATH
else
  case "$CLASSPATH" in *pizza.jar*) :;; *)
    echo pizza.jar is missing, cannot run compiler test with Sun JDK >&2
    nojdkcompile=
  esac
fi

argstopass=""
repeat=1
while test $# -gt 0; do
    case "$1" in
    --time) argstopass="$1 $2"; shift;;
    --tolerance) argstopass="$1 $2"; shift;;
    --repeat) repeat=$2; shift;;
    *) echo $0: invalid argument: $1 >&2; exec false;;
    esac
    shift
done

while repeat=`expr $repeat - 1`; test "$repeat" -ge 0; do
    {   # use echo instead of cat <<, it's easier to comment something out
	echo jk performance Kaffe JIT without Guarana
	echo jg performance Kaffe JIT with Guarana
	echo java performance JDK
	echo ik performance Kaffe intrp without Guarana
	echo ig performance Kaffe intrp with Guarana
	echo jg nullMetaPerformance Kaffe JIT with Guarana and nullMetaPerformance
	echo ig nullMetaPerformance Kaffe intrp with Guarana and nullMetaPerformance
	echo jg logMetaPerformance Kaffe JIT with Guarana and MetaLogger
	echo ig logMetaPerformance Kaffe intrp with Guarana and MetaLogger
    } |
    while read java class comment; do
	echo "$comment" >&2
	$java performance --list |
	egrep -v '^(getTime|new/invokespecial|newarray|multianewarray)$' |
	xargs $java $class $argstopass
	if test x"$class" = x"performance" &&
	   { test x"$comment" != x"JDK" || 
	     test x"${nojdkcompile+set}" != x"set"; }
	then
	    case "$comment" in
	    *JIT*) verbosejit=-verbosejit;;
	    *) verbosejit=;;
	    esac
	    mkdir perftest.$$
	    timecmd compile $java $verbosejit pizza.compiler.Main -d perftest.$$ performance.java
	    rm -rf perftest.$$
	fi
	echo >&2
    done
done
