import java.io.*;

public class Benchmark {

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

public static void runbenchmarks(PrintStream pstream) {

  GCTest gc = new GCTest();
  LoopTest lt = new LoopTest();
  ArrayTest arr_t = new ArrayTest();
  FieldTest ft = new FieldTest();
  ArithmeticTest arith_t = new ArithmeticTest();
  MethodTest meth_t = new MethodTest();
  ThrowTest throw_t = new ThrowTest();
  ThreadTest thread_t = new ThreadTest();
  IOTest io_t = new IOTest();
  Timer cumtimer = new Timer();

  cumtimer.mark();

  gc.start();
  gc.report(pstream);

  lt.start();
  lt.report(pstream);

  arr_t.start();
  arr_t.report(pstream);

  ft.start();
  ft.report(pstream);

  arith_t.start(pstream);
  arith_t.report(pstream);

  meth_t.start();
  meth_t.report(pstream);

  throw_t.start();
  throw_t.report(pstream);

  thread_t.start();
  thread_t.report(pstream);

  io_t.start(pstream);
  io_t.report(pstream);

  cumtimer.record();

  pstream.println("");
  pstream.println("Cumulative runtime: " + cumtime.elapsed() + " sec");

}

} // 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 Test results:");
    pstream.println("Collected " + objs_collected + " of " + 
      GC_OBJECTS + " total objects (average size " +
      (total_size / GC_OBJECTS) + " bytes)");
    t.report(pstream);
    pstream.println("");
  }
}

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");
    pstream.println("");
  }
}

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("Array results:");
    pstream.println("Assigned to " + ARRAY_INTS + " array ints");
    t.report(pstream);
    pstream.println("");
  }
}

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 access results:");
    pstream.println("Accessed " + FIELD_ACCESSES + " int fields");
    t.report(pstream);
    pstream.println("");
  }
}

class FieldTestElem {
  int data = 1;
}

class ArithmeticTest {
  static final int ADDS = 1000000;
  static final int MULTS = 1000000;
  Timer t1, t2;
  int sum = 0, prod = 1;

  public void start(PrintStream pstream) {
    t1 = new Timer();	
    t2 = new Timer();	

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

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

  public void report(PrintStream pstream) {
    pstream.println("Arithmetic results:");
    pstream.println("Added " + ADDS + " integers");
    t1.report(pstream);
    pstream.println("Multipled " + MULTS + " integers");
    t2.report(pstream);
    pstream.println("");
  }
}

class MethodTest {
  static final int METHOD_CALLS = 1000000;
  Timer t1, t2;
  MethodTest mt;

  public void start() {
    t1 = new Timer();
    t2 = new Timer();
    mt = new MethodTest();

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

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

  public void report(PrintStream pstream) {
    pstream.println("Method invocation results:");
    pstream.println("Called " + METHOD_CALLS + " in same object.");
    t1.report(pstream);
    pstream.println("Called " + METHOD_CALLS + " in other object.");
    t2.report(pstream);
    pstream.println("");
  }

  public void empty() { } 
}

class ThrowTest {
  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("Throw/catch results:");
    pstream.println("Threw and caught " + THROWS + " exceptions.");
    t.report(pstream);
    pstream.println("");
  }
}

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("Thread results:");
    pstream.println("Ran " + THREADCNT + " threads, switched " +
      MyThread.SWITCHES + " times each.");
    t.report(pstream);
    pstream.println("");
  }
}

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 t1, t2;
  static final int BUFSIZE = 100000;
  byte buffer[];
  FileOutputStream out = null;

  public void start(PrintStream pstream) {
    t1 = new Timer();
    t2 = new Timer();

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

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

  public void report(PrintStream pstream) {
    pstream.println("I/O results:");
    pstream.println(BUFSIZE + " writes of one byte:");
    t1.report(pstream);
    pstream.println("One write of " + BUFSIZE + " bytes:");
    t2.report(pstream);
  }
}

