#! /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

test x"${1+set}" = x"set" && suffix=$1
test x"${2+set}" = x"set" && err=$2
rm -rf perftest.$$
trap 'rm -rf perftest.$$' 1 2 15
timecmd () {
    cmdname=$1; shift
    { time ${1+"$@"}; } 2>&1 |
    awk '/^[0-9.]*user/ { usertime=0+$1; }
         /^user.*0m[0-9.]s$/ { usertime=0+lst[split($0, lst, "m")]; }
    	 /JIT/ { jittime += $4; }
	 END {
	     print "'"$cmdname"': " usertime;
	     if (jittime) print "'"$cmdname"'-JIT: " jittime/1000.0;
	 }'
}
    
test -f MetaLogger.class || ln ../tutorial-src/MetaLogger.class . ||
{ echo MetaLogger.class is missing >&2 && exec false; }

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
    {
	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"
	$java $class --list |
	egrep -v '^(getTime|new/invokespecial|newarray|multianewarray)$' |
	xargs $java $class $argstopass
	if test x"$class" = x"performance"; then
	    case "$comment" in
	    *JIT*) verbosejit=-verbosejit;;
	    *) verbosejit=;;
	    esac
	    mkdir perftest.$$
	    timecmd compile $java $verbosejit sun.tools.javac.Main -d perftest.$$ performance.java
	    rm -rf perftest.$$
	fi
	echo
    done
done
