A simple Waba performance test.
- By Gernot Starke
- August 14, 2000
POWER JAVA
Java on the Palm Pilot
Gernot Starke
Listing 1. A simple Waba performance test.
/*
PerformanceEvaluation.java
*/
import waba.ui.*;
import waba.fx.*;
import Timer;
public class PerformanceEvaluation extends MainWindow {
ResultPanel resPanel;
Button closeButton, againButton;
public void onEvent(Event event) {
if (event.type == ControlEvent.PRESSED) {
if (event.target == closeButton)
exit(0);
else if (event.target == againButton) {
resPanel.repaint();
};
}
}
public void onStart() {
// create user-interface
Title title = new Title("Waba Performance Evaluation");
title.setRect(0, 0, this.width, 15);
add(title);
// Output-Panel
resPanel = new ResultPanel();
resPanel.setRect(5, 20, this.width - 10, this.height - 40);
add(resPanel);
// 2 Buttons
closeButton = new Button("Close");
closeButton.setRect(0, this.height - 15, 44, 15);
add(closeButton);
againButton = new Button("Start");
againButton.setRect(46, this.height - 15, 44, 15);
add(againButton);
}
}
/*
ResultPanel.java
*/
import waba.ui.*;
import waba.fx.*;
import PerformanceTester;
/**
* A container displaying performance results.
*/
public class ResultPanel extends Container {
Font font;
public ResultPanel() {
font = new Font("Helvetica", Font.PLAIN, 12);
}
public void onPaint(Graphics g) {
g.setFont(font);
g.setColor(0, 0, 0);
// begin with Integer Arithmetic test
g.drawText(PerformanceTester.doIntegerArithmeticTest( 10000 ),
0, 5);
// then perform float arithmetic test
g.drawText(PerformanceTester.doFloatArithmeticTest(5000), 0, 20);
// String test
g.drawText( PerformanceTester.doStringTest( 200), 0,35);
}
}
/**
* PerformanceTester implements some testing algorithms.
* gs, 11.Dec.1999
*
*/
import waba.sys.Convert;
public class PerformanceTester {
static Timer l_timer = new Timer();
public PerformanceTester() { super(); }
private static String assembleResults( String nameOfTest, int bound) {
return Convert.toString( bound ) + " " + nameOfTest
+ " ops: "
+ l_timer.getTimeDifference() + " ms";
}
public static String doFloatArithmeticTest( int bound) {
l_timer.startTimer();
int i=0;
float f = 1.21f;
float f2 = 5.141592f;
for (i=0; i<bound; i++) {
f2 = f2-f;
f2 = f2*f;
}
l_timer.stopTimer();
return assembleResults( "Float", bound );
} // doFloatArithmeticTest
public static String doIntegerArithmeticTest( int bound) {
l_timer.startTimer();
int i, j=0;
for (i=0; i<bound; i++) j = j+i;
l_timer.stopTimer();
return assembleResults( "Integer", bound );
}
public static String doStringTest( int bound) {
l_timer.startTimer();
int i=0;
String s = "";
for (i=0; i<bound; i++) {
if (s.length() > 1000)
s = "";
else s = s + "a";
}
l_timer.stopTimer();
return assembleResults( "String", bound );
} // doStringTest
}
/**
* Timer implements a "stopwatch" class for the Waba VM.
* Can only measure differences <= 24hours.
* Gets time info from Waba-class VM
*/
import waba.sys.Time;
import waba.sys.Vm;
import waba.sys.Convert;
public class Timer {
private boolean started;
private int startTime;
private int stopTime;
public Timer() { started = false; }
public String getTimeDifference() {
int timeDiff = stopTime - startTime;
return Convert.toString(timeDiff);
}
public void startTimer() {
startTime = Vm.getTimeStamp();
started = true;
}
public void stopTimer() {
stopTime = Vm.getTimeStamp();
started = false;
}
} // class Timer