import java.io.*;

public class Benchmark {

public static void main(String argv[]) {
  runbenchmarks(System.out);
}

public static void runbenchmarks(PrintStream pstream) {

  Timer cumtimer = new Timer();
  cumtimer.mark();

  runArithTest(pstream);
  runLoopTest(pstream);
  runArrayTest(pstream);
  runFieldTest(pstream);
  runMethodTest(pstream);
  runExceptionTest(pstream);
  runThreadTest(pstream);
  runGCTest(pstream);
  runIOTest(pstream);

  cumtimer.record();
  pstream.println("Cumulative runtime: " + cumtimer.elapsed() + " sec");
}

public static void runGCTest(PrintStream pstream) {
  GCTest gc = new GCTest();
  gc.start();
  gc.report(pstream);
}

public static void runLoopTest(PrintStream pstream) {
  LoopTest lt = new LoopTest();
  lt.start();
  lt.report(pstream);
}

public static void runArrayTest(PrintStream pstream) {
  ArrayTest arr_t = new ArrayTest();
  arr_t.start();
  arr_t.report(pstream);
}

public static void runFieldTest(PrintStream pstream) {
  FieldTest ft = new FieldTest();
  ft.start();
  ft.report(pstream);
}

public static void runArithTest(PrintStream pstream) {
  ArithmeticTest arith_t = new ArithmeticTest();
  arith_t.start(pstream);
  arith_t.report(pstream);
}

public static void runMethodTest(PrintStream pstream) {
  MethodTest meth_t = new MethodTest();
  meth_t.start();
  meth_t.report(pstream);
}

public static void runExceptionTest(PrintStream pstream) {
  ExceptionTest throw_t = new ExceptionTest();
  throw_t.start();
  throw_t.report(pstream);
}

public static void runThreadTest(PrintStream pstream) {
  ThreadTest thread_t = new ThreadTest();
  thread_t.start();
  thread_t.report(pstream);
}

public static void runIOTest(PrintStream pstream) {
  IOTest io_t = new IOTest();
  io_t.start(pstream);
  io_t.report(pstream);
}

} // end class Benchmark

class GCTest {
  static final int GC_POINTERS = 100;
  static final int GC_OBJECTS = 2000;
  static final int MAX_OBJ_SIZE = 10000;
    GCTestElem[] objects = new GCTestElem[GC_POINTERS];
  static int objs_collected = 0;
  long total_size = 0;
  Timer t;

    public GCTest() { }

  public void start() {
    int size, which;

    t = new Timer();
    t.mark();

    for(int i = 0; i < GC_OBJECTS; i++) {
      size = (int)(Math.random() * MAX_OBJ_SIZE);
      total_size += size;
      which = (int)(Math.random() * GC_POINTERS);
      objects[which] = new GCTestElem(size);
      }

    t.record();
  }

  public void report(PrintStream pstream) {
    pstream.println("GC'd " + objs_collected + " of " +
		    GC_OBJECTS + " total objects (avg. " +
		    (total_size / GC_OBJECTS) + " bytes): " + t.elapsed() +
		    " sec\n");
  }
}

class GCTestElem {
  byte collect[];

  public GCTestElem(int size) {
    collect = new byte[size];
  }

  protected void finalize() {
    GCTest.objs_collected++;
  }
}

class LoopTest {
  static final int ITERATIONS = 1000000;
  Timer t;

  public void start() {
    t = new Timer();
    t.mark();
    for(int i = 0; i < ITERATIONS; i++);
    t.record();
  }

  public void report(PrintStream pstream) {
    pstream.println("Empty loop iterated " + ITERATIONS + " times: " +
		    t.elapsed() + " sec\n");
  }
}

class ArrayTest {
  static final int ARRAY_INTS = 1000000;
  int[] arr = new int[ARRAY_INTS];
  Timer t;

  public void start() {
    t = new Timer();

    t.mark();
    for(int i = 0; i < ARRAY_INTS; i++) {
      arr[i] = i;
      }
    t.record();
  }

  public void report(PrintStream pstream) {
    pstream.println("Assigned to " + ARRAY_INTS + " array ints: " +
		    t.elapsed() + " sec\n");
  }
}

class FieldTest {
  static final int FIELD_ACCESSES = 1000000;
  FieldTestElem fte = new FieldTestElem();
  Timer t;

  public void start() {
    int temp;

    t = new Timer();

    t.mark();
    for(int i = 0; i < FIELD_ACCESSES; i++) {
      temp = fte.data;
      }
    t.record();
  }

  public void report(PrintStream pstream) {
    pstream.println(FIELD_ACCESSES + " object int field accesses: " +
		    t.elapsed() + " sec\n");
  }
}

class FieldTestElem {
  int data = 1;
}

class ArithmeticTest {
  static final int ADDS = 1000000;
  static final int MULTS = 1000000;
  static final double FADDS = 1000000.0;
  static final double FMULTS = 1000000.0;
  Timer intaddtimer, intmulttimer;
  Timer doubleaddtimer, doublemulttimer;
  int sum = 0, prod = 1;
  double fsum = 0.0, fprod = 1.0;

  // WGG - note that there are extra adds hidden in the loop counter
  public void start(PrintStream pstream) {
    intaddtimer = new Timer();	
    intmulttimer = new Timer();	
    doubleaddtimer = new Timer();	
    doublemulttimer = new Timer();	

    intaddtimer.mark();
    for(int i = 0; i < ADDS; i++) {
      try {
      	sum += i;
        } catch(Exception e) { pstream.println("int add exception"); }
      }
    intaddtimer.record();

    intmulttimer.mark();
    for(int i = 1; i <= MULTS; i++) {
      try {
      	prod *= i;
        } catch(Exception e) { pstream.println("int mult exception"); }
      }
    intmulttimer.record();
  
    doubleaddtimer.mark();
    for(int i = 0; i < FADDS; i++) {
      try {
      	sum += i;
        } catch(Exception e) { pstream.println("double float add exception"); }
      }
    doubleaddtimer.record();

    doublemulttimer.mark();
    for(int i = 1; i <= FMULTS; i++) {
      try {
      	prod *= i;
        } catch(Exception e) { pstream.println("double float mult exception"); }
      }
    doublemulttimer.record();
  }

  public void report(PrintStream pstream) {
    pstream.println("Added " + ADDS + " ints in loop: " +
		    intaddtimer.elapsed() + " sec");
    pstream.println("Multipled " + MULTS + " ints in loop: " +
		    intmulttimer.elapsed() + " sec");
    pstream.println("Added " + ADDS + " doubles in loop: " +
		    doubleaddtimer.elapsed() + " sec");
    pstream.println("Multipled " + MULTS + " doubles in loop: " +
		    doublemulttimer.elapsed() + " sec\n");
  }
}

class MethodTest {
  static final int METHOD_CALLS = 1000000;
  Timer sametimer, othertimer;
  MethodTest mt;

  public void start() {
    sametimer = new Timer();
    othertimer = new Timer();
    mt = new MethodTest();

    sametimer.mark();
    for(int i = 0; i < METHOD_CALLS; i++) {
      empty();
      }
    sametimer.record();

    othertimer.mark();
    for(int i = 0; i < METHOD_CALLS; i++) {
      mt.empty();
      }
    othertimer.record();
  }

  public void report(PrintStream pstream) {
    pstream.println(METHOD_CALLS + " method calls in same object: " +
		    sametimer.elapsed() + " sec");
    pstream.println(METHOD_CALLS + " method calls in other object: " +
		    othertimer.elapsed() + " sec\n");
  }

  public void empty() { } 
}

class ExceptionTest {
  static final int THROWS = 1000000;
  Timer t;
  MyException exc;

  public void start() {
    t = new Timer();
    exc = new MyException();

    t.mark();
    for(int i = 0; i < THROWS; i++) {
      try {
      	throw exc;
        } catch(MyException e) { }
      }
    t.record();
  }

  public void report(PrintStream pstream) {
    pstream.println("Threw and caught " + THROWS + " exceptions: " +
		    t.elapsed() + " sec\n");
  }
}

class MyException extends Exception { }
      
class ThreadTest {
  static final int THREADCNT = 3;
  MyThread threads[] = new MyThread[THREADCNT];
  Timer t;
  int threadsDone = 0;
  int i;

  public void start() {
    t = new Timer();

    for (i = 0 ; i < THREADCNT ; i++)
      threads[i] = new MyThread();

    t.mark();

    for (i = 0 ; i < THREADCNT ; i++)
      threads[i].start(this);

    while(threadsDone != THREADCNT) {
      try {
      	Thread.yield();
        } catch(Exception e) { }
      }
    t.record();
  }

  public void signal() {
    threadsDone++;
  }

  public void report(PrintStream pstream) {
    pstream.println(THREADCNT + " threads, switched " + MyThread.SWITCHES +
		    " times each." + t.elapsed() + " sec\n");
  }
}

class MyThread extends Thread {
  static final int SWITCHES = 10000;
  ThreadTest caller;

  public void start(ThreadTest caller) {
    this.caller = caller;
    start();
  }

  public void run() {
    for(int i = 0; i < SWITCHES; i++) {
      try {
      	Thread.yield();
        } catch(Exception e) { }
      }

    caller.signal();
  }
}

class IOTest {
  Timer bytetimer, blocktimer;
  static final int BUFSIZE = 100000;
  byte buffer[];
  FileOutputStream out = null;

  public void start(PrintStream pstream) {
    bytetimer = new Timer();
    blocktimer = new Timer();

    try {
      out = new FileOutputStream("tmpfile");
      } catch(IOException e) {
	pstream.println("Could not create \"tmpfile\" for IO benchmark");
	return;
        }
    
    bytetimer.mark();
    for(int i = 0; i < BUFSIZE; i++) {
      try {
      	out.write(0);
        } catch(Exception e) { }
      }
    bytetimer.record();

    buffer = new byte[BUFSIZE];
    blocktimer.mark();
    try {
      out.write(buffer);
      } catch(Exception e) { }
    blocktimer.record();
  }

  public void report(PrintStream pstream) {
    pstream.println(BUFSIZE + " writes of one byte: " +
		    bytetimer.elapsed() + " sec");
    pstream.println("One write of " + BUFSIZE + " bytes: " +
		    blocktimer.elapsed() + " sec\n");
  }
}

