package ckelling;

import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
//import java.io.*;
//import java.net.*;


/**
 *	PerformanceTest.java
 *
 *	Geschwindigkeitstest fuer Java-Code
 *
 *	@author		Carsten Kelling
 *	@version	0.3.0, 28.09.96
 */
public class PerformanceTest extends Applet
{
    public final static String VERSIONSTRING = "Geschwindigkeitstest für Java-Code 0.3.0";

    public final static int OUTERLOOP	= 20;
    public final static int INNERLOOP	= 4000;
    public final static int NUMBERBASE	= 32;

	private boolean hasBeenStopped = false;

	private Frame		frame;
	private TextArea 	textArea;
	private Graphics 	g;

	//private MyCanvas	myCanvas;
	//private TextCell	tc2;
	//private int			painter;

	public String getAppletInfo()
	{
		return VERSIONSTRING;
	}

	public void init()
	{
		//initialize();
		g = getGraphics();

		//System.getProperties().list (System.out);
		frame = new Frame();
		frame.setTitle("Ausgabe");
		Rectangle bounds = bounds();
		frame.setLayout(new BorderLayout());
		frame.show();
		textArea = new TextArea("");
		frame.add("Center", textArea);
		frame.reshape(bounds.x + bounds.width, bounds.y, 350, 450);
		textArea.appendText("java.class.version: " + System.getProperty("java.class.version") + "\n");
		textArea.appendText("java.vendor: " + System.getProperty("java.vendor") + "\n");
		textArea.appendText("java.vendor.url: " + System.getProperty("java.vendor.url") + "\n");
		textArea.appendText("java.version: " + System.getProperty("java.version") + "\n");
		textArea.appendText("os.arch: " + System.getProperty("os.arch") + "\n");
		textArea.appendText("os.name: " + System.getProperty("os.name") + "\n");
		String str = System.getProperty("java.home", "[nicht verfügbar]");
		if (str.equals("[nicht verfügbar]"))
			textArea.appendText("  Dieses applet wurde über das Netz geladen\n  (java.home ist nicht zugreifbar).\n\n");
		else
			textArea.appendText("  Dieses applet wurde lokal geladen\n  (java.home: " + str + ").\n\n");

		str = new String(VERSIONSTRING + "\n");
		str = new String(str + "--------------------------------------------------------------\n");
		System.out.print(str);
		textArea.appendText(str);

		//TextCell tc1 = new TextCell((Rechner) this, (Container) frame);
		//frame.add("North", tc1);
		//tc1.setText("TC1");
		//tc1.show();

		//tc2 = new TextCell((Rechner) this, (Container) frame);
		//frame.add("South", tc2);
		//tc2.reshape(10, 10, 50, 50);
		//tc2.setText("HALLO WELT!");
		//tc2.show();

		//myCanvas = new MyCanvas((Rechner) this, (Container) frame);
		//frame.add("South", myCanvas);
		//myCanvas.hide();
		//frame.validate();

		//painter = 0;

		show();
		paint(g);

		calculate();

	} /* end init */

	/**
	public void paint(Graphics g)
	{
		painter++;
		if (painter <= 2)
		{
			tc2.show();
			myCanvas.hide();
			frame.validate();
		}
		else if ((painter > 2) || (painter <= 4))
		{
			tc2.hide();
			myCanvas.show();
			frame.validate();
		}
		else
			painter = 0;

		super.paint(g);
	}
	*/


	public void stop()
	{
		hasBeenStopped = true;
		frame.hide();
		frame.dispose();
	}

	public void start()
	{
		if (hasBeenStopped)
			init();
	}


	private void calculate()
	{
		String str;
		int totalTime = 0;

		for (int i = 1; i <= OUTERLOOP; i++)
		{
			int calcTime = calculateInner();
			str = new String("Berechnung Nr. " + Integer.toString(i) + " benötigte " + Integer.toString(calcTime) + " ms.");
			System.out.println(str);
			textArea.appendText(str + "\n");
			textArea.paint(g);
			totalTime = totalTime + calcTime;
		}
		str = new String("--------------------------------------------------------\n");
		str = new String(str + "Gesamtzeit für " + Integer.toString(OUTERLOOP) + " Durchläufe: " + Integer.toString(totalTime) + " ms\n");
		System.out.print(str);
		textArea.appendText(str);
	} /* end calculate */

	private int calculateInner()
	{
		long timeBefore = System.currentTimeMillis();

		double a, b, c;

		for (int j = 1; j <= INNERLOOP; j++)
		{
			a = Math.random() * Math.pow(2, NUMBERBASE);
			b = Math.pow(2, NUMBERBASE);
			c = a/b;
			b = Math.pow(2, NUMBERBASE) * c;
			a = Math.pow(2, NUMBERBASE) * (1-c);
			c = -10 + a + b;
			a = c - b;
			b = c / b;
			b = b / 11;
			a = a / 17;
			c = a + b;
		}

		return (int) (System.currentTimeMillis() - timeBefore);

	} /* end calculateInner */

	 synchronized void paintActivated(Graphics onScreenGC) {}
	 void deactivateAll() {}
	 void scrollAll() {}
	 void updateAll() {}
	 synchronized void invalidateAllImageCaches() {}

	 synchronized void timerWokeUp(String title) {}
	 void stopSimulation() {}
	 void singleInstruction() {}
	 void singleInstructionBack() {}
	 void singleStep(boolean doRepaint) {}
	 void demonstrate() {}
	 void demonstrate(boolean doRepaint) {}
	 void demonstrateBack() {}
	 synchronized void bufferComputer() {}
	 synchronized void setComputer() {}
	 void initCacheComponents(int cSize, int tSize, int assoc) {}
	 void removeCacheRessources() {}

} /* end PerformanceTest */


