KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BenchmarkImpl


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by Thorsten Fiebig are
5
// Copyright (C) 2000-@year@ by Thorsten Fiebig. All rights reserved.
6
// Code portions created by SMB are
7
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
8
//
9
// $Id: BenchmarkImpl.java,v 1.3 2000/10/28 16:55:26 daniela Exp $
10

11 import java.util.*;
12
13 import org.ozoneDB.DxLib.*;
14 import org.ozoneDB.*;
15
16
17 public class BenchmarkImpl extends OzoneObject implements Benchmark {
18     // data base parameters
19
private final static int fTest1Conn = 0;
20     private final static int fTest3Conn = 1;
21     private final static int fTiny = 2;
22     private final static int fSmall = 3;
23     
24     private final static int[] fNumAtomicPerComp = {20, 20, 20, 20};
25     private final static int[] fConnPerAtomic = {1, 3, 3, 3};
26     private final static int[] fDocumentSize = {20, 20, 20, 2000};
27     private final static int[] fManualSize = {1000, 1000, 1000, 100000};
28     private final static int[] fNumCompPerModule = {5, 5, 50, 500};
29     private final static int[] fNumAssmPerAssm = {3, 3, 3, 3};
30     private final static int[] fNumAssmLevels = {3, 3, 7, 7};
31     private final static int[] fNumCompPerAssm = {3, 3, 3, 3};
32     private final static int[] fNumModules = {1, 1, 1, 1};
33     
34     final static boolean verbose = false;
35     
36     static Random theRandom = null;
37     
38     int theScale = 0;
39     
40     long theOid = 0;
41     
42     OO7_Module theModule = null;
43     
44     
45     public static void main( String JavaDoc[] args ) {
46         if (args.length == 0) {
47             printUsage();
48             System.exit( 1 );
49         } else {
50             if (args.length == 1 && args[1] == "query") {
51                 printUsage();
52                 System.exit( 1 );
53             }
54         }
55         
56         RemoteDatabase anDatabase = new RemoteDatabase();
57         
58         try {
59             // open the connection on localhost at port 3333
60
anDatabase.open( "localhost", 3333 );
61             System.out.println( "Connected ..." );
62             // reload our database classes if we changed them
63
anDatabase.reloadClasses();
64             
65             long start = System.currentTimeMillis();
66             
67             if (args[0].equals( "query" )) {
68                 if (args[1].equals( "traversal" )) {
69                     Benchmark anBenchmark = (Benchmark)anDatabase.objectForName( "OO7Benchmark" );
70                     anBenchmark.traversalQuery();
71                 } else {
72                     if (args[1].equals( "match" )) {
73                         Benchmark anBenchmark = (Benchmark)anDatabase.objectForName( "OO7Benchmark" );
74                         anBenchmark.matchQuery();
75                     }
76                 }
77             } else {
78                 if (args[0].equals( "create" )) {
79                     int scale = -1;
80                     if (args[1].equals( "test3Conn" )) {
81                         scale = fTest3Conn;
82                     } else if (args[1].equals( "test1Conn" )) {
83                         scale = fTest1Conn;
84                     } else if (args[1].equals( "tiny" )) {
85                         scale = fTiny;
86                     } else if (args[1].equals( "small" )) {
87                         scale = fSmall;
88                     } else {
89                         System.out.println( "Invalid scale" );
90                         System.exit( 1 );
91                     }
92                     Benchmark anBenchmark =
93                             (Benchmark)anDatabase.createObject( BenchmarkImpl.class.getName(), OzoneInterface.Public,
94                             "OO7Benchmark" );
95                     anBenchmark.create( scale );
96                 }
97             }
98             
99             System.out.println( "time: " + (System.currentTimeMillis() - start) + "msec" );
100             
101             // close the connection
102
anDatabase.close();
103         
104         } catch (Exception JavaDoc e) {
105             System.out.println( e );
106             e.printStackTrace();
107             try {
108                 anDatabase.close();
109             } catch (Exception JavaDoc ex) {
110                 System.out.println( ex );
111                 ex.printStackTrace();
112             }
113         }
114     }
115     
116     
117     static void printUsage() {
118         System.out.println( "usage: ojvm BanchmarkImpl (create|query) [options]" );
119         System.out.println( " create options:" );
120         System.out.println( " size - (tiny|small|large)" );
121         System.out.println( " query options:" );
122         System.out.println( " type - (traversal|match)" );
123     }
124     
125     
126     static int getRandomInt( int lower, int upper ) {
127         if (theRandom == null) {
128             theRandom = new Random();
129         }
130         
131         int rVal;
132         do {
133             rVal = theRandom.nextInt();
134             rVal %= upper;
135         //System.out.println("rVal: " + rVal + " lower: " + lower + " upper: " + upper);
136
} while (rVal < lower || rVal >= upper);
137         return rVal;
138     }
139     
140     
141     protected long getAtomicPartOid() {
142         return theOid++;
143     }
144     
145     
146     public void create( int anScale ) throws Exception JavaDoc {
147         theScale = anScale;
148         theModule = createModule();
149     }
150     
151     
152     public void traversalQuery() throws Exception JavaDoc {
153         Hashtable table = new Hashtable();
154         long time = System.currentTimeMillis();
155         traversal( theModule.designRoot(), table );
156         time = (System.currentTimeMillis() - time);
157         System.out.println( "Millis: " + time );
158     }
159     
160     
161     protected void traversal( OO7_Assembly anAssembly, Hashtable aTable ) throws Exception JavaDoc {
162         if (anAssembly.getClass() == Class.forName( "OO7_BaseAssemblyImpl_Proxy" )) {
163             System.out.println( "Base Assembly Class: " );
164             OO7_BaseAssembly baseAssembly = (OO7_BaseAssembly)anAssembly;
165             DxIterator compIterator = baseAssembly.componentsShar().iterator();
166             while (compIterator.next() != null) {
167                 OO7_CompositePart compositePart = (OO7_CompositePart)compIterator.object();
168                 dfs( compositePart );
169             }
170         } else {
171             OO7_ComplexAssembly complexAssembly = (OO7_ComplexAssembly)anAssembly;
172             DxIterator aIterator = complexAssembly.subAssemblies().iterator();
173             while (aIterator.next() != null) {
174                 traversal( (OO7_Assembly)aIterator.object(), aTable );
175             }
176         }
177     }
178     
179     
180     protected void dfs( OO7_CompositePart aPart ) throws Exception JavaDoc {
181         Hashtable table = new Hashtable();
182         dfsVisit( aPart.rootPart(), table );
183         System.out.println( "AtomicParts visited: " + table.size() );
184     }
185     
186     
187     protected void dfsVisit( OO7_AtomicPart anAtomicPart, Hashtable aTable ) throws Exception JavaDoc {
188         DxIterator connIterator = anAtomicPart.from().iterator();
189         while (connIterator.next() != null) {
190             OO7_Connection connection = (OO7_Connection)connIterator.object();
191             OO7_AtomicPart part = connection.to();
192             if (!aTable.containsKey( part )) {
193                 aTable.put( part, part );
194                 dfsVisit( part, aTable );
195             }
196         }
197     }
198     
199     
200     public void matchQuery() throws Exception JavaDoc {
201         int atomicParts = fNumAtomicPerComp[theScale] * fNumCompPerModule[theScale];
202         long[] oids = new long[10];
203         int i;
204         for (i = 0; i < 10; ++i) {
205             oids[i] = getRandomInt( 0, atomicParts );
206             System.out.println( "oids[" + i + "] : " + oids[i] );
207         }
208         long time = System.currentTimeMillis();
209         for (i = 0; i < 10; ++i) {
210             OO7_AtomicPart part = (OO7_AtomicPart)database().objectForName( "OO7_AtomicPart" + oids[i] );
211         }
212         time = (System.currentTimeMillis() - time);
213         System.out.println( "Millis: " + time );
214     }
215     
216     
217     protected OO7_Module createModule() throws Exception JavaDoc {
218         OO7_CompositePart[] compositeParts = new OO7_CompositePart[fNumCompPerModule[theScale]];
219         for (int i = 0; i < fNumCompPerModule[theScale]; ++i) {
220             compositeParts[i] = createCompositePart();
221         }
222         OO7_Module module = (OO7_Module)database().createObject( OO7_ModuleImpl.class.getName(), 0, "Module" );
223         OO7_ComplexAssembly designRoot =
224                 (OO7_ComplexAssembly)createAssembly( module, fNumAssmLevels[theScale], compositeParts );
225         module.setDesignRoot( designRoot );
226         
227         return module;
228     }
229     
230     
231     protected OO7_CompositePart createCompositePart() throws Exception JavaDoc {
232         // Document erzeugen
233
OO7_Document document = (OO7_Document)database().createObject( OO7_DocumentImpl.class.getName(), 0 );
234         // CompositeParterzeugen
235
OO7_CompositePart compositePart =
236                 (OO7_CompositePart)database().createObject( OO7_CompositePartImpl.class.getName(), 0 );
237         if (verbose) {
238             System.out.println( "CompositePart created" );
239         }
240         compositePart.setDocumentation( document );
241         
242         OO7_AtomicPart[] atomicParts = new OO7_AtomicPart[fNumAtomicPerComp[theScale]];
243         // AtomicParts erzeugen
244
for (int i = 0; i < fNumAtomicPerComp[theScale]; ++i) {
245             long oid = getAtomicPartOid();
246             atomicParts[i] = (OO7_AtomicPart)database().createObject( OO7_AtomicPartImpl.class.getName() );
247             if (verbose) {
248                 System.out.println( "AtomicPart: " + oid + " created" );
249             }
250             compositePart.addPart( atomicParts[i] );
251             atomicParts[i].setPartOf( compositePart );
252         }
253         compositePart.setRootPart( atomicParts[0] );
254         
255         // AtomicParts miteinander verbinden
256
for (int i = 0; i < fNumAtomicPerComp[theScale]; ++i) {
257             int next = (i + 1) % fNumAtomicPerComp[theScale];
258             OO7_Connection connection =
259                     (OO7_Connection)database().createObject( OO7_ConnectionImpl.class.getName(), 0 );
260             connection.setFrom( atomicParts[i] );
261             atomicParts[i].addFrom( connection );
262             connection.setTo( atomicParts[next] );
263             atomicParts[next].addTo( connection );
264             if (verbose) {
265                 System.out.println( "Connection: from: " + i + " to: " + next );
266             }
267             for (int j = 0; j < (fConnPerAtomic[theScale] - 1); ++j) {
268                 next = getRandomInt( 0, fNumAtomicPerComp[theScale] );
269                 connection = (OO7_Connection)database().createObject( OO7_ConnectionImpl.class.getName(), 0 );
270                 connection.setFrom( atomicParts[j] );
271                 atomicParts[j].addFrom( connection );
272                 connection.setTo( atomicParts[next] );
273                 atomicParts[next].addTo( connection );
274                 if (verbose) {
275                     System.out.println( "Connection: from: " + j + " to: " + next );
276                 }
277             }
278         }
279         return compositePart;
280     }
281     
282     
283     protected OO7_Assembly createAssembly( OO7_Module aModule, int aLevel, OO7_CompositePart[] someCompositeParts )
284             throws Exception JavaDoc {
285         if (verbose) {
286             System.out.println( "level: " + aLevel );
287         }
288         if (aLevel == 1) {
289             OO7_BaseAssembly baseAssembly =
290                     (OO7_BaseAssembly)database().createObject( OO7_BaseAssemblyImpl.class.getName(), 0 );
291             aModule.addAssembly( baseAssembly );
292             for (int j = 0; j < fNumCompPerAssm[theScale]; ++j) {
293                 int k = getRandomInt( 0, fNumCompPerModule[theScale] );
294                 baseAssembly.addComponentsShar( someCompositeParts[k] );
295             }
296             return baseAssembly;
297         } else {
298             OO7_ComplexAssembly complexAssembly =
299                     (OO7_ComplexAssembly)database().createObject( OO7_ComplexAssemblyImpl.class.getName(), 0 );
300             aModule.addAssembly( complexAssembly );
301             for (int i = 0; i < fNumAssmPerAssm[theScale]; ++i) {
302                 complexAssembly.addSubAssembly( createAssembly( aModule, aLevel - 1, someCompositeParts ) );
303             }
304             return complexAssembly;
305         }
306     }
307 }
308
Popular Tags