/** * ResponseTest.java runs a background process that (only) sleeps * for a pre-determined amount of time. When it awakens, it displays * the difference between the actual sleep and the expected sleep. * * Copyright &copy; 1996-1998 Martin Minow. All Rights Reserved.<p> * * Permission to use, copy, modify, and redistribute this software and its * documentation for personal, non-commercial use is hereby granted provided that * this copyright notice and appropriate documentation appears in all copies. This * software may not be distributed for fee or as part of commercial, "shareware," * and/or not-for-profit endevors including, but not limited to, CD-ROM collections, * online databases, and subscription services without specific license.<p> * * @author <a href="mailto:minow@apple.com">Martin Minow</a> * @version 1.0 * Set tabs every 4 characters. */import java.applet.Applet;import java.awt.*;import java.awt.event.*;	/* For test main program */public class ResponseTest extends Panel implements Runnable{	public static final long		defaultSleepTime	= 2000;	public static final String		startString			= "Start";	public static final String		stopString			= "Stop";		private ResponseTestGraph		responseTestGraph;	/*	 * Initialize the startStopButton with the longer of the two strings.	 */	private Button					startStopButton		= new Button(startString);	private Button					initButton			= new Button("Clear");	private UpdateIntervalPanel		updateIntervalPanel	= new UpdateIntervalPanel();	private long					sleepTime			= defaultSleepTime;	private transient int			maxDelay			= 0;	private transient int[]			delay				= new int[3];	private EtchedBorder			border;	private transient int			borderMaxDelay		= 0;	private transient int			borderDelay			= Integer.MAX_VALUE;	private Thread					thread				= null;		/**	 * ResponseTest provides a simple measure of system performance.	 * It sleeps for a specified time (default is one second) then	 * measures the actual sleep time. The difference between the	 * expected and actual time is then written into a histogram	 * (in ResponseTestCanvas.java)	 */	public ResponseTest()	{		setFont(GetDialogFont.getFont());		responseTestGraph = new ResponseTestGraph();		responseTestGraph.setBackground(Color.white);		border = new EtchedBorder(					new SimpleBorder(responseTestGraph)				);		border.setBackground(Color.gray);		border.setLabelFont(new Font("TimesRoman", Font.BOLD, 12));		border.setThickness(3).setGap(1);		/* */		startStopButton.setBackground(Color.white);		initButton.setBackground(Color.white);		/* */		Panel bottomButtonPanel		= new Panel();		bottomButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 2));		bottomButtonPanel.setFont(GetDialogFont.getFont());		bottomButtonPanel.setBackground(Color.gray);		/* */		bottomButtonPanel.add(initButton);		bottomButtonPanel.add(startStopButton);		/* */		Panel buttonPanel			= new Panel();		buttonPanel.setLayout(new BorderLayout());		buttonPanel.add("North", updateIntervalPanel);		buttonPanel.add("South", bottomButtonPanel);		/* */		setLayout(new BorderLayout(2, 2));		add("Center", border);		add("South", buttonPanel);		validate();		start();	}	public Dimension preferredSize()	{		return (new Dimension(220, 144));	}	public Dimension getPreferredSize()	{		return (preferredSize());	}	public void start()	{		if (thread == null) {			thread = new Thread(this);			thread.setName("Response Test");			thread.start();			startStopButton.setLabel(stopString);		}	}	public void stop()	{		thread = null;		startStopButton.setLabel(startString);	}	public void run()	{		while (thread != null) {			long	thisSleep	= sleepTime;			long	start		= System.currentTimeMillis();			try {				thread.sleep(thisSleep);			}			catch (InterruptedException e) { }			long	now			= System.currentTimeMillis();			int thisDelay = (int) (now - start - thisSleep);			for (int i = 1; i < 3; i++) {				delay[i]		= delay[i - 1];			}			delay[0]			= thisDelay;			int	averageDelay	= Integer.MAX_VALUE;			if (delay[2] != Integer.MAX_VALUE) {				averageDelay	= (int)					((((3.0 * (double) delay[0])					+ (2.0 * (double) delay[1])					+ (      (double) delay[2])) / 6.0) + 0.5);			}			if (thisDelay > maxDelay) {				maxDelay		= thisDelay;			}			if (maxDelay != borderMaxDelay || averageDelay != borderDelay) {				String	label	=					((averageDelay != Integer.MAX_VALUE)						? (Integer.toString(averageDelay) + " msec. (aver), ")						: "")					+ Integer.toString(maxDelay) + " msec. max";				border.setLabelText(label);			}			responseTestGraph.addValue(thisDelay);		}	}	public boolean action(			Event			event,			Object			arg		)	{		boolean		result		= true;		if (event.target == startStopButton) {			if (thread != null) {				stop();			}			else {				start();			}		}		else if (event.target == initButton) {			maxDelay = 0;			for (int i = 0; i < 3; i++) {				delay[i] = Integer.MAX_VALUE;			}			responseTestGraph.initialize();		}		else if (event.target == updateIntervalPanel) {			setSleepTime(updateIntervalPanel.getUpdateInterval());		}		else {			result = false;		}		return (result);	}	/*	 * Accessor functions to allow ResponseTest to live as a JavaBean.	 *	 * These accessors pass the request to the Graph component.	 */	public Color	getGraphBackground()	{ return (responseTestGraph.getBackground());	}	public Color	getGraphForeground()	{ return (responseTestGraph.getForeground());	}	public void		setGraphBackground(			Color			color		)	{		responseTestGraph.setBackground(color);	}	public void		setGraphForeground(			Color			color		)	{		responseTestGraph.setBackground(color);	}	/*	 * These accessors manage the ResponseTest component as a whole.	 */	public boolean	isRunning()			{ return (thread != null); }	public void	setRunning(			boolean		runNow		)	{		if (runNow != (thread != null)) {			stop();			if (runNow) {				start();			}		}	}	public long getSleepTime()			{ return (sleepTime); }	public void setSleepTime(			long			sleepTime		)	{		this.sleepTime		= sleepTime;		if (thread != null) {			thread.interrupt();		}	}}