KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OO1BenchmarkApp


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: OO1BenchmarkApp.java,v 1.3 2002/09/18 06:54:18 per_nyfelt Exp $
8

9 package org.ozoneDB.tools;
10
11 import java.applet.Applet JavaDoc;
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.util.Vector JavaDoc;
15
16
17 /** */
18 class BenchmarkThread extends Thread JavaDoc {
19     /** */
20     public String JavaDoc benchmarkClassName;
21     private int benchmarkId = 1;
22     private int size = 1000;
23     private String JavaDoc objectName = "cattell";
24     private String JavaDoc serverHost = "localhost";
25     private BenchmarkProgressLog log = new BenchmarkProgressLog();
26     
27     
28     /** */
29     public BenchmarkThread( String JavaDoc className ) {
30         benchmarkClassName = className;
31     }
32     
33     
34     /** */
35     public void setProgressLog( BenchmarkProgressLog blog ) {
36         log = blog;
37     }
38     
39     
40     /** */
41     public String JavaDoc[] filterAndApplyArgs( String JavaDoc[] args ) {
42         Vector JavaDoc remainingArgs = new Vector JavaDoc();
43
44         for (int i = 0; i < args.length; i++) {
45             if (args[i].startsWith( "-benchmark=" )) {
46                 benchmarkId = new Integer JavaDoc( args[i].substring( 11 ) ).intValue();
47             } else if (args[i].startsWith( "-size=" )) {
48                 size = new Integer JavaDoc( args[i].substring( 6 ) ).intValue();
49             } else if (args[i].startsWith( "-name=" )) {
50                 objectName = args[i].substring( 6 );
51             } else if (args[i].startsWith( "-host=" )) {
52                 serverHost = args[i].substring( 6 );
53                 log.logMessage("serverHost=" + serverHost);
54             } else {
55                 remainingArgs.addElement( args[i] );
56             }
57         }
58         
59         String JavaDoc[] result = new String JavaDoc[remainingArgs.size()];
60         for (int i = 0; i < result.length; i++) {
61             result[i] = (String JavaDoc)remainingArgs.elementAt( i );
62         }
63         
64         return result;
65     }
66     
67     
68     /** */
69     public static String JavaDoc availableArgs() {
70         return "-benchmark=<test> -size=<size> [-name=<name>] [-host=<host[:port]>]";
71     }
72     
73     
74     /** */
75     public static String JavaDoc helpText() {
76         return
77                 " -benchmark= : 1: create database; size: 100..(10000)\n"
78                 + " 2: lookup; size 1..\n" + " 3: traversal; size: 1..7\n"
79                 + " 4: insert and delete; size: 1..\n" + " 5: insert; size: 1..\n"
80                 + " 6: insert size*1000 objects\n"
81                 + " 7: traversal on the first size cattell objects\n"
82                 + " 8: insert on the first size cattell objects\n"
83                 + " -size : the size of the test\n"
84                 + " -name : optional name of the benchmark object\n"
85                 + " -host : optional name of the server host\n";
86     }
87     
88     
89     /** */
90     public void run() {
91         try {
92             Class JavaDoc benchmarkClass = Class.forName( benchmarkClassName );
93             OO1Benchmark benchmark = (OO1Benchmark)benchmarkClass.newInstance();
94             benchmark.setProgressLog( log );
95             switch (benchmarkId) {
96             case 1:
97                 benchmark.create( size, objectName, serverHost );
98                 break;
99             case 2:
100                 benchmark.lookup( size, objectName, serverHost );
101                 break;
102             case 3:
103                 benchmark.traversal( size, objectName, serverHost );
104                 break;
105             case 4:
106                 benchmark.insertAndDelete( size, objectName, serverHost );
107                 break;
108             case 5:
109                 benchmark.insert( size, objectName, serverHost );
110                 break;
111             case 55:
112                 benchmark.delete( size, objectName, serverHost );
113                 break;
114             case 6:
115                 benchmark.createObjects( size, objectName, serverHost );
116                 break;
117             case 7:
118                 benchmark.traversalObjects( size, objectName, serverHost );
119                 break;
120             case 8:
121                 benchmark.insertObjects( size, objectName, serverHost );
122                 break;
123             }
124         } catch (Exception JavaDoc e) {
125             e.printStackTrace();
126         }
127     }
128 }
129
130
131 /** */
132 public class OO1BenchmarkApp extends Applet JavaDoc implements ActionListener, ItemListener {
133     
134     
135     /** */
136     public class AppletProgressLog extends BenchmarkProgressLog {
137         
138         
139         /** */
140         public AppletProgressLog() {
141         }
142         
143         
144         /** */
145         public void logMessage( String JavaDoc msg ) {
146             if (outText != null) {
147                 outText.append( msg + "\n" );
148             }
149         }
150         
151         
152         /** */
153         public void logTime( long time ) {
154             if (outText != null) {
155                 outText.append( "time: " + time + "msec\n" );
156             }
157             if (chart != null) {
158                 chart.appendTime( time );
159             }
160         }
161     }
162     
163     /** */
164     protected Button startButton;
165     protected Choice testChoice;
166     protected TextField sizeText;
167     protected TextField nameText;
168     protected TextField serverText;
169     protected TextField portText;
170     protected TextArea outText;
171     protected Label statusLabel;
172     protected ChartCanvas chart;
173     protected BenchmarkThread benchmarkThread;
174     
175     
176     /** */
177     public OO1BenchmarkApp() {
178     }
179     
180     
181     /** */
182     public void init() {
183         init( false, new BenchmarkThread( "CattellImpl" ) );
184     }
185     
186     
187     /** */
188     public void init( boolean chartOnly, BenchmarkThread bmt ) {
189         benchmarkThread = bmt;
190         setLayout( new BorderLayout() );
191         
192         if (!chartOnly) {
193             Panel panel = new Panel();
194             panel.setLayout( new FlowLayout( FlowLayout.LEFT ) );
195             
196             startButton = new Button( " Start " );
197             startButton.setFont( new Font( "Monospaced", Font.BOLD, 14 ) );
198             startButton.addActionListener( this );
199             panel.add( startButton );
200             
201             testChoice = new Choice();
202             testChoice.add( "Help" );
203             testChoice.add( "1 Create" );
204             testChoice.add( "2 Lookup" );
205             testChoice.add( "3 Traversal" );
206             testChoice.add( "4 Ins & del" );
207             testChoice.add( "5 Insert" );
208             testChoice.add( "6 Ins*1000" );
209             testChoice.add( "7 Traversal" );
210             testChoice.add( "8 Insert" );
211             testChoice.addItemListener( this );
212             panel.add( testChoice );
213             
214             sizeText = new TextField( "1000", 5 );
215             sizeText.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );
216             sizeText.addActionListener( this );
217             panel.add( sizeText );
218             
219             nameText = new TextField( "cattell", 9 );
220             nameText.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );
221             nameText.addActionListener( this );
222             panel.add( nameText );
223             
224             serverText = new TextField( "localhost", 9 );
225             serverText.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );
226             serverText.addActionListener( this );
227             panel.add( serverText );
228             
229             portText = new TextField( "3333", 9 );
230             portText.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );
231             portText.addActionListener( this );
232             panel.add( portText );
233             
234             add( "North", panel );
235             
236             panel = new Panel();
237             panel.setLayout( new BorderLayout() );
238             
239             outText = new TextArea();
240             panel.add( outText, "North" );
241             
242             chart = new ChartCanvas();
243             panel.add( chart, "Center" );
244             
245             add( "Center", panel );
246             
247             statusLabel = new Label( "Test:Build - Size:1000 - Name:cattell " );
248             add( "South", statusLabel );
249         } else {
250             chart = new ChartCanvas();
251             add( "Center", chart );
252         }
253         
254         benchmarkThread.setProgressLog( new AppletProgressLog() );
255     }
256     
257     
258     /** */
259     public void itemStateChanged( ItemEvent e ) {
260         String JavaDoc[] defaults = {"0", "1000", "100", "7", "100", "100", "10", "10", "10"};
261         if (testChoice.getSelectedIndex() > -1) {
262             sizeText.setText( defaults[testChoice.getSelectedIndex()] );
263         }
264     }
265     
266     
267     /** */
268     public void actionPerformed( ActionEvent e ) {
269         if (startButton.getLabel().equals( " Start " )) {
270             int test = testChoice.getSelectedIndex();
271             int size = Integer.valueOf( sizeText.getText() ).intValue();
272             String JavaDoc name = nameText.getText();
273             String JavaDoc server = serverText.getText();
274             String JavaDoc port = portText.getText();
275             statusLabel.setText( "Test: " + test + " Size: " + size + " Name: " + name + " Host: " + server + ":"
276                     + port );
277             
278             String JavaDoc[] args = {"-benchmark=" + test, "-size=" + size, "-name=" + name, "-host=" + server + ":" + port};
279             benchmarkThread.filterAndApplyArgs( args );
280             startButton.setLabel( " Stop " );
281             benchmarkThread.start();
282         } else {
283             benchmarkThread.stop();
284             benchmarkThread = new BenchmarkThread( benchmarkThread.benchmarkClassName );
285             benchmarkThread.setProgressLog( new AppletProgressLog() );
286             startButton.setLabel( " Start " );
287         }
288     }
289     
290     
291     /** */
292     public static void createFrame( boolean chartOnly, BenchmarkThread thread ) {
293         Frame f = new Frame( "Cattell Benchmark" );
294         OO1BenchmarkApp applet = new OO1BenchmarkApp();
295         
296         applet.init( chartOnly, thread );
297         applet.start();
298         
299         f.add( "Center", applet );
300         f.pack();
301         f.setSize( chartOnly ? 300 : 550, chartOnly ? 120 : 400 );
302         f.show();
303         f.addWindowListener( new WindowAdapter() {
304             
305             
306             public void windowClosing( WindowEvent e ) {
307                 System.exit( 0 );
308             }
309         } );
310         
311         if (chartOnly) {
312             thread.run();
313         }
314     }
315     
316     
317     /** */
318     public static void main( String JavaDoc[] args ) {
319         boolean chartOnly = false;
320         boolean noGui = false;
321         String JavaDoc benchmarkClass = "CattellImpl";
322         
323         for (int i = 0; i < args.length; i++) {
324             if (args[i].startsWith( "-chartOnly" )) {
325                 chartOnly = true;
326             } else if (args[i].startsWith( "-noGui" )) {
327                 noGui = true;
328             } else if (args[i].startsWith( "-class=" )) {
329                 benchmarkClass = args[i].substring( 7 );
330             } else {
331                 if (args[i].startsWith( "-help" ) || args[i].startsWith( "-?" )) {
332                     System.out.println( "usage: java OO1BenchmarkApp " + BenchmarkThread.availableArgs() + " "
333                             + ChartCanvas.availableArgs() + " "
334                             + "[-chartOnly] [-noGui] [-class=<classname>] [-help|-h|-?]\n" + "Details:\n"
335                             + BenchmarkThread.helpText() + ChartCanvas.helpText()
336                             + " -chartOnly : show only the chart panel and perform the given test\n"
337                             + " -noGui : show no GUI at all and perform the given test\n"
338                             + " -class= : the benchmark class, default is CattellImpl\n"
339                             + " -help|-h|-? : shows this help" );
340                     return;
341                 }
342             }
343         }
344         
345         BenchmarkThread thread = new BenchmarkThread( benchmarkClass );
346         thread.filterAndApplyArgs( args );
347         if (noGui) {
348             thread.run();
349         } else {
350             createFrame( chartOnly, thread );
351         }
352     }
353 }
354
Popular Tags