#! /bin/sh

# Copyright 1997,1998,1999 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
}
    
case "$CLASSPATH" in *pizza.jar*) :;; *)
  echo pizza.jar is missing, cannot run compiler test with Sun JDK >&2
  nojdkcompile=
esac

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

errstatus=0

while repeat=`expr $repeat - 1`; test "$repeat" -ge 0; do
    if test -n "$JAVA"; then
        echo "$JAVA" performance Current build
        case "$CLASSPATH" in *guarana.jar*)
	    echo "$JAVA" nullMetaPerformance Current build and nullMetaPerformance
	    echo "$JAVA" logMetaPerformance Current build and logMetaPerformance
	  ;;
	esac
    else
	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
    fi | {
      while read java class comment; do
	echo "$comment" >&2
	$java $class $argstopass \
	  `$java performance --list | egrep -v '^getTime$'` || errstatus=$?
	if test x"$class" = x"performance" &&
	   { test x"$comment" != x"JDK" || 
	     test x"${nojdkcompile+set}" != x"set"; }
	then
	    case "$comment" in
	    *JIT* | Current*) verbosejit=-verbosejit;;
	    *) verbosejit=;;
	    esac
	    mkdir perftest.$$
	    timecmd compile $java $verbosejit pizza.compiler.Main -d perftest.$$ performance.java || errstatus=$?
	    rm -rf perftest.$$
	fi
	echo >&2
      done
      exit $errstatus
    } || errstatus=$?
done

exit $errstatus
