import java.io.*;
import java.awt.Graphics;
import java.awt.Font;

public class BenchmarkRunner extends java.applet.Applet implements Runnable {

  char outputBuffer[];
  String s = null;
  Thread mythread = null;
  int i;
  int x_coord = 0, y_coord = 0;
  String num;
  int speed=35;
  int counter =0;
  boolean threadSuspended = false; //added by kwalrath

public void init() {
  setFont(new Font("TimesRoman",Font.BOLD,36));
}

public void start() {
  if(mythread == null) {
   mythread = new Thread(this);
   mythread.start();
   }
}

public void stop() {
  mythread = null;
}

public void run() {
  while (mythread != null) {
    try {Thread.sleep(100);} catch (InterruptedException e){}
    repaint();
    }
  mythread = null;
}

public void paint(Graphics g) {
 //   g.drawChars(separated, i,1,x_coord,y_coord);
 }

} // end class BenchmarkRunner


public class Benchmark {

public static void main(char argv[]) {
  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();

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

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

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

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

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

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

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

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

  io_t.start();
  io_t.report();
}

} // 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() {
    System.out.println("GC Test results:");
    System.out.println("Collected " + objs_collected + " of " + 
      GC_OBJECTS + " total objects (average size " +
      (total_size / GC_OBJECTS) + " bytes)");
    t.report();
    System.out.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() {
    System.out.println("Empty loop results:");
    System.out.println("Iterated " + ITERATIONS + " times");
    t.report();
    System.out.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() {
    System.out.println("Array results:");
    System.out.println("Assigned to " + ARRAY_INTS + " array ints");
    t.report();
    System.out.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() {
    System.out.println("Field access results:");
    System.out.println("Accessed " + FIELD_ACCESSES + " int fields");
    t.report();
    System.out.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() {
    t1 = new Timer();	
    t2 = new Timer();	

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

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

  public void report() {
    System.out.println("Arithmetic results:");
    System.out.println("Added " + ADDS + " integers");
    t1.report();
    System.out.println("Multipled " + MULTS + " integers");
    t2.report();
    System.out.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() {
    System.out.println("Method invocation results:");
    System.out.println("Called " + METHOD_CALLS + " in same object.");
    t1.report();
    System.out.println("Called " + METHOD_CALLS + " in other object.");
    t2.report();
    System.out.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() {
    System.out.println("Throw/catch results:");
    System.out.println("Threw and caught " + THROWS + " exceptions.");
    t.report();
    System.out.println("");
  }
}

class MyException extends Exception { }
      
class ThreadTest {
  MyThread thr1, thr2, thr3;
  Timer t;
  int threadsDone = 0;

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

    thr1 = new MyThread();
    thr2 = new MyThread();
    thr3 = new MyThread();

    t.mark();
    thr1.start(this);
    thr2.start(this);
    thr3.start(this);

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

  public void signal() {
    threadsDone++;
  }

  public void report() {
    System.out.println("Thread results:");
    System.out.println("Ran three threads, switched " + MyThread.SWITCHES 
      + " times each.");
    t.report();
    System.out.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() {
    t1 = new Timer();
    t2 = new Timer();

    try {
      out = new FileOutputStream("tmpfile");
      } catch(IOException e) {
	System.out.println("Could not create tmpfile!");
	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() {
    System.out.println("I/O results:");
    System.out.println(BUFSIZE + " writes of one byte:");
    t1.report();
    System.out.println("One write of " + BUFSIZE + " bytes:");
    t2.report();
  }
}

