KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > MDRPerformanceTests


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.test;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25 import java.text.SimpleDateFormat JavaDoc;
26
27 import junit.extensions.*;
28
29 import org.netbeans.api.mdr.*;
30 import org.openide.util.Lookup;
31 import org.netbeans.junit.*;
32
33 import org.netbeans.mdr.util.*;
34 import org.netbeans.mdr.NBMDRepositoryImpl;
35 import org.netbeans.lib.jmi.xmi.*;
36 import org.netbeans.lib.jmi.mapping.*;
37
38 import org.xml.sax.*;
39
40 import javax.jmi.reflect.*;
41 import javax.jmi.model.*;
42
43 /**
44  * Test case measuring performance of various MDR operations.
45  * To run the test case, run ant with the property
46  * "xtest.testtype=perf". By default, test results can be found in
47  * xtest's log files. In addition, when specifying the
48  * property "xtest.userdata.propertyfile=perf.properties", test
49  * results are recorded in our local performace tests database.
50  * The performace data stored in this database can be viewed on
51  * http://beetle.czech.sun.com:8080/PerformanceChartsViewer/PerformanceChartsViewer
52  */

53 public class MDRPerformanceTests extends NbTestCase {
54         
55     private static final URL MODEL_XMI_URL = MDRPerformanceTests.class.getResource("data/java-model.xml");
56     private static final String JavaDoc MOFMODEL = "MOF";
57     private static final String JavaDoc METAMODEL = "MOF4PerformaceTest";
58     private static final String JavaDoc MODEL = "PerformanceTest";
59     private static final String JavaDoc PACKAGE = "Physical";
60     
61     private static final short MAX_INST = 100;
62     private static final short MAX_ASSOC = 20;
63     private static final short MAX_ALL_OF_CLASS = 10;
64     private static final short MAX_QUERY_ASSOC = 20;
65     private static final short MAX_INST_TO_DEL = 100;
66     private static final short MAX_INST_TO_MODIFY = 10;
67     private static final int MAX_ITERATIONS = 20;
68     private static final int MAX_XMI_ITERATIONS = 5;
69     private static final int MAX_DEL_ITERATIONS = 1;
70     private static float correction = 0;
71
72     private static MDRepository repository = null;
73     private static RefPackage pkg = null;
74     private static RandomDataSupport random = null;
75     private static boolean enabled = false;
76
77     private static RefObject findPackage(ModelPackage extent, String JavaDoc name) {
78         for (Iterator it = extent.getMofPackage().refAllOfClass().iterator(); it.hasNext();) {
79             ModelElement temp = (ModelElement) it.next();
80             if (temp.getName().equals(name)) {
81                 return temp;
82             }
83         }
84         return null;
85     }
86
87     private void initSupport() {
88         if (random == null) {
89             if (pkg == null)
90                 fail("Extent does not exist.");
91             random = new RandomDataSupport(pkg);
92         }
93     }
94     
95     public MDRPerformanceTests(String JavaDoc testName) {
96         super (testName);
97     }
98     
99     public static void main (String JavaDoc[] args) {
100         junit.textui.TestRunner.run (suite ());
101     }
102     
103     public static NbTest suite () {
104         NbTestSuite suite = new NbTestSuite ();
105         suite.addTestSuite (MDRPerformanceTests.class);
106         
107         NbTestSetup setup = new NbTestSetup (suite) {
108             public void setUp () {
109             }
110             public void tearDown () {
111             }
112         };
113         return setup;
114     }
115     
116     protected void setUp() {
117         String JavaDoc workDir = System.getProperty("work.dir");
118         
119         // properties will be set only if running outside of IDE
120
if (System.getProperty("org.openide.version") == null) {
121             System.setProperty("org.netbeans.mdr.persistence.Dir", workDir + File.separator + "test_repository_1");
122             System.setProperty("org.netbeans.mdr.storagemodel.StorageFactoryClassName",
123             "org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeFactory");
124             System.setProperty("org.openide.util.Lookup", "org.netbeans.mdr.test.MDRTestLookup");
125             System.setProperty("org.netbeans.mdr.SaveStorageOnExit", "true");
126         }
127         
128         if (repository == null) {
129             MDRManager mdrManager = MDRManager.getDefault();
130             if (mdrManager == null)
131                 fail("Unable to lookup MDRManager");
132             repository = mdrManager.getDefaultRepository();
133             if (repository == null)
134                 fail ("Repository not found.");
135         }
136         
137         String JavaDoc timestamp = null;
138         File timestampFile = new File(workDir + File.separator + "timestamp");
139         if (timestampFile.exists()) {
140             try {
141                 timestamp = new DataInputStream(new FileInputStream(timestampFile)).readUTF();
142             } catch (Exception JavaDoc e) {}
143         }
144         else {
145             timestamp = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss").format(new Date());
146             try {
147                 DataOutputStream dos = new DataOutputStream(new FileOutputStream(timestampFile));
148                 dos.writeUTF(timestamp);
149                 dos.close();
150             } catch (Exception JavaDoc e) {}
151         }
152
153         if (!enabled && "true".equals(System.getProperty("netbeans.performance.enabled"))) {
154             enabled = true;
155             System.setProperty("netbeans.performance.serverPassword","");
156             System.setProperty("netbeans.performance.serverUsername","perfserver");
157             System.setProperty("netbeans.performance.serverURL","jdbc:mysql://beetle.czech.sun.com:3306/performance");
158             System.setProperty("netbeans.performance.testSuite","MDRPerformanceTests");
159             System.setProperty("netbeans.performance.totalTestCases","12");
160             System.setProperty("org.openide.version",timestamp);
161             System.setProperty("netbeans.build.branch","");
162         }
163
164     }
165
166     /** Tests performance of the first MDR startup.
167      * As a result 1) a total time to boot MOF within a newly created repository
168      * is recorded together with 2) a total time to instantitate then the MOF
169      * within the repository.
170      */

171     public void testMDRFirstStart () {
172         long start = System.currentTimeMillis();
173         repository.getExtent(MOFMODEL);
174         long result = System.currentTimeMillis() - start;
175
176         PerformanceServerConnection conn = null;
177         if (enabled) {
178             conn = new PerformanceServerConnection();
179             conn.logTestCase("MDR - booting MOF (first start)", result);
180         }
181         PrintStream log = getLog();
182         log.println("MDR first startup performace results:");
183         log.println("------------------------------------");
184         log.println("Booting MOF: " + (float)result/(float)1000 + " s");
185         
186         ModelPackage mmExtent = null;
187         try {
188             start = System.currentTimeMillis();
189             mmExtent = (ModelPackage) repository.createExtent(METAMODEL);
190             result = System.currentTimeMillis() - start;
191         } catch (CreationFailedException e) {
192             fail ("Package instantiation failed: " + METAMODEL);
193         }
194         
195         if (enabled)
196             conn.logTestCase("MDR - instantiating MOF (first start)", result);
197         log.println("Creating MOF instance: " + (float)result/(float)1000 + " s");
198     }
199     
200     /** Tests performance of the repetitional MDR startup.
201      * As a result 1) a total time to obtain a reference to MOF within an already
202      * existing repository is recorded together with 2) a total time to instantitate
203      * then the MOF within the repository.
204      */

205     public void testMDRStart () {
206         long start = System.currentTimeMillis();
207         repository.getExtent(MOFMODEL);
208         long result = System.currentTimeMillis() - start;
209
210         PerformanceServerConnection conn = null;
211         if (enabled) {
212             conn = new PerformanceServerConnection();
213             conn.logTestCase("MDR - booting MOF", result);
214         }
215         PrintStream log = getLog();
216         log.println("MDR startup performace results:");
217         log.println("------------------------------------");
218         log.println("Booting MOF: " + (float)result/(float)1000 + " s");
219         
220         ModelPackage mmExtent = null;
221         try {
222             start = System.currentTimeMillis();
223             mmExtent = (ModelPackage) repository.createExtent(METAMODEL);
224             result = System.currentTimeMillis() - start;
225         } catch (CreationFailedException e) {
226             fail ("Package instantiation failed: " + METAMODEL);
227         }
228         
229         if (enabled)
230             conn.logTestCase("MDR - instantiating MOF", result);
231         log.println("Creating MOF instance: " + (float)result/(float)1000 + " s");
232         
233         XMIReader reader = XMIReader.getDefault();
234         if (reader == null)
235             fail("Unable to lookup XMIReader");
236
237         try {
238             repository.beginTrans (true);
239             reader.read (MODEL_XMI_URL.toString(), new RefPackage[] {mmExtent});
240         } catch (Exception JavaDoc e) {
241             fail (e.getMessage ());
242         } finally {
243             repository.endTrans ();
244         }
245
246         RefObject mmPackage = findPackage(mmExtent, PACKAGE);
247         if (mmPackage == null)
248             fail("Unable to find package: " + PACKAGE);
249         
250         try {
251             pkg = repository.createExtent(MODEL, mmPackage);
252         } catch (CreationFailedException e) {
253             fail ("Package instantiation failed: " + MODEL);
254         }
255     }
256
257     /** Tests performance of object creation.
258      * Objects are created randomly within a Java metamodel's extent.
259      * As a result 1) an average time to create an object instance is recoreded
260      * both excuding and including time necessary to start and finish an
261      * appropriate transaction. Separately, 2) a time to create the first instance
262      * of a class is recorded. This time reflects an overhead of generating handlers
263      * in MDR.
264      */

265     public void testObjectCreation () {
266         initSupport();
267         long[] results = new long[MAX_ITERATIONS];
268         long[] extResults = new long[MAX_ITERATIONS];
269         int[] nums = new int[MAX_ITERATIONS];
270         System.out.println("Object creation performance test started (it takes some time to complete)...");
271         for (int i = 0; i < MAX_ITERATIONS; i++) {
272             System.gc();
273             long seed = System.currentTimeMillis();
274             random.generateObjects (seed, MAX_INST, i == 0, true);
275             System.gc();
276             long warmUpStart = System.currentTimeMillis();
277             random.generateObjects (seed, MAX_INST, i == 0, true);
278             long warmUp = System.currentTimeMillis() - warmUpStart;
279             System.gc();
280             long extStart = System.currentTimeMillis();
281             try {
282                 repository.beginTrans (true);
283                 long start = System.currentTimeMillis();
284                 random.generateObjects (seed, MAX_INST, i == 0, false);
285                 results[i] = System.currentTimeMillis() - start - warmUp;
286             } catch (Exception JavaDoc e) {
287                 fail (e.getMessage ());
288             }
289             finally {
290                 repository.endTrans ();
291             }
292             extResults[i] = System.currentTimeMillis() - extStart - warmUp;
293             nums[i] = random.getInstancesCounter();
294         }
295         System.out.println("Done.");
296
297         int numberOfInst = 0;
298         long totalTime = 0;
299         long totalExtTime = 0;
300         int validInst = 0;
301         long validTime = 0;
302         long validExtTime = 0;
303         for (int i = 0; i < MAX_ITERATIONS; i++) {
304             numberOfInst += nums[i];
305             totalTime += results[i];
306             totalExtTime += extResults[i];
307             if (i >= 10) {
308                 validTime += results[i];
309                 validExtTime += extResults[i];
310                 validInst += nums[i];
311                 if (enabled && nums[i] > 0) {
312                     PerformanceServerConnection conn = new PerformanceServerConnection();
313                     conn.logTestCase("MDR - 100 objects creation time", results[i] * 100 / nums[i]);
314                     conn.logTestCase("MDR - 100 objects creation time (including transactions)", extResults[i] * 100 / nums[i]);
315                 }
316             }
317         }
318         
319         PrintStream log = getLog();
320         log.println("Random object creation test results:");
321         log.println("------------------------------------");
322         log.println("Number of created instances: " + numberOfInst);
323         log.println("Total time (without transactions): " + (float)totalTime/(float)1000 + " s");
324         log.println("Total time: " + (float)totalExtTime/(float)1000 + " s");
325         log.println("First object creation average time (without transactions): " + (float)results[0] / (float)nums[0] + " ms");
326         log.println("First object creation average time: " + (float)extResults[0] / (float)nums[0] + " ms");
327         log.println("Average object creation time (without transactions): " + (correction = (float) validTime / (float) validInst) + " ms");
328         log.println("Average object creation time: " + (float) validExtTime / (float) validInst + " ms");
329         log.println();
330         
331     }
332     
333     /** Tests performance of association creation.
334      * Associations are created randomly within a Java metamodel's extent.
335      * As a result, an average time to create an associations instance is recoreded
336      * both excuding and including time necessary to start and finish an
337      * appropriate transaction.
338      */

339     public void testAssociationCreation () {
340         initSupport();
341         long[] results = new long[MAX_ITERATIONS];
342         long[] extResults = new long[MAX_ITERATIONS];
343         int[] nums = new int[MAX_ITERATIONS];
344         System.out.println("Association creation performance test started (it takes some time to complete)...");
345         random.prepareAssociationEnds();
346         for (int i = 0; i < MAX_ITERATIONS; i++) {
347             System.gc();
348             long seed = System.currentTimeMillis();
349             random.generateAssociations (seed, MAX_ASSOC, i == 0, true);
350             System.gc();
351             long warmUpStart = System.currentTimeMillis();
352             random.generateAssociations (seed, MAX_ASSOC, i == 0, true);
353             long warmUp = System.currentTimeMillis() - warmUpStart;
354             System.gc();
355             long extStart = System.currentTimeMillis();
356             try {
357                 repository.beginTrans (true);
358                 long start = System.currentTimeMillis();
359                 random.generateAssociations (seed, MAX_ASSOC, i == 0, false);
360                 results[i] = System.currentTimeMillis() - start - warmUp - (long)(correction * random.getInstancesCounter());
361             } catch (Exception JavaDoc e) {
362                 fail (e.getMessage ());
363             }
364             finally {
365                 repository.endTrans ();
366             }
367             extResults[i] = System.currentTimeMillis() - extStart - warmUp - (long)(correction * random.getInstancesCounter());
368             nums[i] = random.getLinksCounter();
369         }
370         System.out.println("Done.");
371         
372         int numberOfInst = 0;
373         long totalTime = 0;
374         long totalExtTime = 0;
375         int validInst = 0;
376         long validTime = 0;
377         long validExtTime = 0;
378         for (int i = 0; i < MAX_ITERATIONS; i++) {
379             numberOfInst += nums[i];
380             totalTime += results[i];
381             totalExtTime += extResults[i];
382             if (i >= 10) {
383                 validTime += results[i];
384                 validExtTime += extResults[i];
385                 validInst += nums[i];
386                 if (enabled && nums[i] > 0) {
387                     PerformanceServerConnection conn = new PerformanceServerConnection();
388                     conn.logTestCase("MDR - 100 associations creation time", results[i] * 100 / nums[i]);
389                     conn.logTestCase("MDR - 100 associations creation time (including transactions)", extResults[i] * 100 / nums[i]);
390                 }
391             }
392         }
393         
394         PrintStream log = getLog();
395         log.println("Random association creation test results:");
396         log.println("------------------------------------");
397         log.println("Number of created associations: " + numberOfInst);
398         log.println("Total time (without transactions): " + (float)totalTime/(float)1000 + " s");
399         log.println("Total time: " + (float)totalExtTime/(float)1000 + " s");
400         log.println("Average association creation time (without transactions): " + (float)validTime / (float)validInst + " ms");
401         log.println("Average association creation time: " + (float)validExtTime / (float)validInst + " ms");
402         log.println();
403     }
404     
405     /** Tests performance of the RefClass.refAllOfClass() operation.
406      * All classes of the Java metamodel are queried.
407      * As a result, an average time to obtain the requested collection of instances
408      * of the class is recorded.
409      */

410     public void testAllOfClass() {
411         initSupport();
412         int numberOfClasses = random.getNumberOfClasses();
413         long[] results = new long[numberOfClasses];
414         System.out.println("refAllOfClass performance test started...");
415         for (int i = 0; i < numberOfClasses; i++) {
416             System.gc();
417             long seed = System.currentTimeMillis();
418             random.findAllOfClass(i);
419             results[i] = System.currentTimeMillis() - seed;
420         }
421         System.out.println("Done.");
422
423         long totalTime = 0;
424         
425         for (int i = 0; i < numberOfClasses; i++) {
426             totalTime += results[i];
427             if (enabled && results[i] > 0) {
428                 PerformanceServerConnection conn = new PerformanceServerConnection();
429                 conn.logTestCase("MDR - refAllOfClass invocations time", results[i]);
430             }
431         }
432
433         PrintStream log = getLog();
434         log.println("Random refAllOfClass test results:");
435         log.println("------------------------------------");
436         log.println("Number of queried classes: " + numberOfClasses);
437         log.println("Total time: " + (float)totalTime/(float)1000 + " s");
438         log.println("Average time: " + (float)totalTime / (float)numberOfClasses + " ms");
439         log.println();
440     }
441     
442     /** Tests performance of iterators iterating through the RefClass.refAllOfClass() operation's
443      * results.
444      * As a result, an average time to iterate through a collection of instances
445      * of the class is recorded.
446      */

447     public void testIteratingAllOfClass () {
448         initSupport();
449         int numberOfClasses = random.getNumberOfClasses();
450         long[] results = new long[numberOfClasses];
451         int[] nums = new int[numberOfClasses];
452         System.out.println("Iterating through refAllOfClass started...");
453         for (int i = 0; i < numberOfClasses; i++) {
454             System.gc();
455             long seed = System.currentTimeMillis();
456             nums[i] = random.iterateAllOfClass(i);
457             results[i] = System.currentTimeMillis() - seed;
458         }
459         System.out.println("Done.");
460         
461         int numberOfInst = 0;
462         long totalTime = 0;
463         for (int i = 0; i < numberOfClasses; i++) {
464             numberOfInst += nums[i];
465             totalTime += results[i];
466             if (enabled && nums[i] > 0) {
467                 PerformanceServerConnection conn = new PerformanceServerConnection();
468                 conn.logTestCase("MDR - Iterating through 100 objects returned by refAllOfClass", results[i] * 100 / nums[i]);
469             }
470         }
471         
472         PrintStream log = getLog();
473         log.println("Iterating through refAllOfClass results:");
474         log.println("------------------------------------");
475         log.println("Number of iterated classes: " + numberOfClasses);
476         log.println("Number of iterated instances: " + numberOfInst);
477         log.println("Total time: " + (float)totalTime/(float)1000 + " s");
478         log.println("Average number of instances per class: " + numberOfInst/numberOfClasses);
479         log.println("Average time per iterated class: " + (float)totalTime/(float)numberOfClasses + " ms");
480         log.println("Average time per iterated instance: " + (float)totalTime/(float)numberOfInst + " ms");
481         log.println();
482     }
483     
484     /** Tests performance of the RefAssociation.refQuery(...) operation.
485      * Randomly queries associations previously created within a Java metamodel's extent.
486      * As a result, an average time to query a randomly selected association
487      * both excluding and including time necessary to start and finish an appropriate
488      * transaction is recorded.
489      */

490     public void testQueryAssociations() {
491         initSupport();
492         long[] results = new long[MAX_ITERATIONS];
493         int[] nums = new int[MAX_ITERATIONS];
494         System.out.println("Random query associations test started...");
495         for (int i = 0; i < MAX_ITERATIONS; i++) {
496             System.gc();
497             long seed = System.currentTimeMillis();
498             nums[i] = random.queryAssociations(seed, MAX_QUERY_ASSOC);
499             results[i] = System.currentTimeMillis() - seed;
500         }
501         System.out.println("Done.");
502
503         int others = random.getOthersCounter();
504         int numberOfInst = 0;
505         long totalTime = 0;
506         for (int i = 0; i < MAX_ITERATIONS; i++) {
507             numberOfInst += nums[i];
508             totalTime += results[i];
509             if (enabled && nums[i] > 0) {
510                 PerformanceServerConnection conn = new PerformanceServerConnection();
511                 conn.logTestCase("MDR - query 100 associations time", results[i] * 100 / nums[i]);
512             }
513         }
514         
515         PrintStream log = getLog();
516         log.println("Random query associations test results:");
517         log.println("------------------------------------");
518         log.println("Number of queried association ends: " + numberOfInst);
519         log.println("Number of traversed other ends: " + others);
520         log.println("Total time: " + (float)totalTime/(float)1000 + " s");
521         log.println("Average time per queried association end: " + (float)totalTime/(float)numberOfInst + " ms");
522         log.println("Average time per other end traversed: " + (float)totalTime/(float)others + " ms");
523         log.println();
524     }
525     
526     /** Tests performance of object modifications.
527      * Randomly modifies objects previously created within a Java metamodel's extent.
528      * As a result, an average time to modify a randomly selected attribute of a
529      * randomly selected object both excluding and including time necessary to
530      * start and finish an appropriate transaction is recorded.
531      */

532     public void testObjectModification() {
533         initSupport();
534         long[] results = new long[MAX_ITERATIONS];
535         long[] extResults = new long[MAX_ITERATIONS];
536         int[] nums = new int[MAX_ITERATIONS];
537         System.out.println("Object modification performance test started (it takes some time to complete)...");
538         random.modifyObjects(0, MAX_INST_TO_MODIFY, true, true);
539         for (int i = 0; i < MAX_ITERATIONS; i++) {
540             System.gc();
541             long seed = System.currentTimeMillis();
542             random.modifyObjects(seed, MAX_INST_TO_MODIFY, false, true);
543             long warmUp = System.currentTimeMillis() - seed;
544             System.gc();
545             long extStart = System.currentTimeMillis();
546             try {
547                 repository.beginTrans (true);
548                 long start = System.currentTimeMillis();
549                 random.modifyObjects(seed, MAX_INST_TO_MODIFY, false, false);
550                 results[i] = System.currentTimeMillis() - start - warmUp;
551             } catch (Exception JavaDoc e) {
552                 fail (e.getMessage ());
553             }
554             finally {
555                 repository.endTrans ();
556             }
557             extResults[i] = System.currentTimeMillis() - extStart - warmUp;
558             nums[i] = random.getInstancesCounter();
559             if (results[i] < 0) {
560                 results[i] = 0;
561                 extResults[i] = 0;
562                 nums[i] = 0;
563             }
564         }
565         System.out.println("Done.");
566
567         int numberOfInst = 0;
568         long totalTime = 0;
569         long totalExtTime = 0;
570         for (int i = 0; i < MAX_DEL_ITERATIONS; i++) {
571             numberOfInst += nums[i];
572             totalTime += results[i];
573             totalExtTime += extResults[i];
574             if (enabled && nums[i] > 0) {
575                 PerformanceServerConnection conn = new PerformanceServerConnection();
576                 conn.logTestCase("MDR - 100 object modifications time", results[i] * 100 / nums[i]);
577                 conn.logTestCase("MDR - 100 object modifications time (including transactions)", extResults[i] * 100 / nums[i]);
578             }
579         }
580
581         PrintStream log = getLog();
582         log.println("Random object modification test results:");
583         log.println("------------------------------------");
584         log.println("Number of modified instances: " + numberOfInst);
585         log.println("Total time (without transactions): " + (float)totalTime/(float)1000 + " s");
586         log.println("Total time: " + (float)totalExtTime/(float)1000 + " s");
587         log.println("Average object modification time (without transactions): " + (float)totalTime/(float)numberOfInst + " ms");
588         log.println("Average object modification time: " + (float)totalExtTime/(float)numberOfInst + " ms");
589         log.println();
590     }
591     
592     /** Tests performance of object modifications.
593      * Randomly modifies objects previously created within a Java metamodel's extent.
594      * As a result, an average time to modify a randomly selected attribute of a
595      * randomly selected object within an autocommited transaction is recorded.
596      */

597     public void testAutocommitedModifications() {
598         initSupport();
599         long[] results = new long[MAX_ITERATIONS];
600         long[] extResults = new long[MAX_ITERATIONS];
601         int[] nums = new int[MAX_ITERATIONS];
602         System.out.println("Autocommited object modification performance test started (it takes some time to complete)...");
603         random.modifyObjects(0, MAX_INST_TO_MODIFY, true, true);
604         for (int i = 0; i < MAX_ITERATIONS; i++) {
605             System.gc();
606             long seed = System.currentTimeMillis();
607             random.modifyObjects(seed, MAX_INST_TO_MODIFY, false, true);
608             long warmUp = System.currentTimeMillis() - seed;
609             System.gc();
610             long start = System.currentTimeMillis();
611             random.modifyObjects(seed, MAX_INST_TO_MODIFY, false, false);
612             results[i] = System.currentTimeMillis() - start - warmUp;
613             nums[i] = random.getInstancesCounter();
614         }
615         System.out.println("Done.");
616
617         int numberOfInst = 0;
618         long totalTime = 0;
619         for (int i = 0; i < MAX_DEL_ITERATIONS; i++) {
620             numberOfInst += nums[i];
621             totalTime += results[i];
622             if (enabled && nums[i] > 0) {
623                 PerformanceServerConnection conn = new PerformanceServerConnection();
624                 conn.logTestCase("MDR - 100 autocommited object modifications time", results[i] * 100 / nums[i]);
625             }
626         }
627
628         PrintStream log = getLog();
629         log.println("Random autocommited object modification test results:");
630         log.println("------------------------------------");
631         log.println("Number of modified instances: " + numberOfInst);
632         log.println("Total time: " + (float)totalTime/(float)1000 + " s");
633         log.println("Average object modification time: " + (float)totalTime/(float)numberOfInst + " ms");
634         log.println();
635     }
636     
637     /** Tests performance of object deletion.
638      * Ramdomly deletes objects previously created within a Java metamodel's extent.
639      * As a result, an average time to delete an object instance both excluding and
640      * including time necessary to start and finish an appropriate transaction
641      * is recorded.
642      */

643     public void testObjectDeletion() {
644         initSupport();
645         long[] results = new long[MAX_DEL_ITERATIONS];
646         long[] extResults = new long[MAX_DEL_ITERATIONS];
647         int[] nums = new int[MAX_DEL_ITERATIONS];
648         System.out.println("Object deletion performance test started (it takes some time to complete)...");
649         for (int i = 0; i < MAX_DEL_ITERATIONS; i++) {
650             System.gc();
651             long seed = System.currentTimeMillis();
652             nums[i] = random.prepareObjectsToDelete(seed, MAX_INST_TO_DEL);
653             System.gc();
654             long extStart = System.currentTimeMillis();
655             try {
656                 repository.beginTrans (true);
657                 long start = System.currentTimeMillis();
658                 random.deleteObjects ();
659                 results[i] = System.currentTimeMillis() - start;
660             } catch (Exception JavaDoc e) {
661                 fail (e.getMessage ());
662             }
663             finally {
664                 repository.endTrans ();
665             }
666             extResults[i] = System.currentTimeMillis() - extStart;
667         }
668         System.out.println("Done.");
669
670         int numberOfInst = 0;
671         long totalTime = 0;
672         long totalExtTime = 0;
673         for (int i = 0; i < MAX_DEL_ITERATIONS; i++) {
674             numberOfInst += nums[i];
675             totalTime += results[i];
676             totalExtTime += extResults[i];
677             if (enabled && nums[i] > 0) {
678                 PerformanceServerConnection conn = new PerformanceServerConnection();
679                 conn.logTestCase("MDR - 100 objects deletion time", results[i] * 100 / nums[i]);
680                 conn.logTestCase("MDR - 100 objects deletion time (including transactions)", extResults[i] * 100 / nums[i]);
681             }
682         }
683
684         PrintStream log = getLog();
685         log.println("Random object deletion test results:");
686         log.println("------------------------------------");
687         log.println("Number of deleted instances: " + numberOfInst);
688         log.println("Total time (without transactions): " + (float)totalTime/(float)1000 + " s");
689         log.println("Total time: " + (float)totalExtTime/(float)1000 + " s");
690         log.println("Average object deletion time (without transactions): " + (float)totalTime/(float)numberOfInst + " ms");
691         log.println("Average object deletion time: " + (float)totalExtTime/(float)numberOfInst + " ms");
692         log.println();
693     }
694             
695     public void testXMIExportImport () {
696         XMIReader reader = XMIReader.getDefault();
697         if (reader == null)
698             fail("Unable to lookup XMIReader");
699         XMIWriter writer = XMIWriter.getDefault();
700         if (writer == null)
701             fail("Unable to lookup XMIWriter");
702         ModelPackage mmExtent = (ModelPackage) repository.getExtent(METAMODEL);
703         RefObject mmPackage = findPackage(mmExtent, PACKAGE);
704         if (mmPackage == null)
705             fail("Unable to find package: " + PACKAGE);
706         
707         RefPackage javaPackage = null;
708         long time = 0, extTime = 0;
709         long[] writeResults = new long[MAX_XMI_ITERATIONS];
710         long[] readResults = new long[MAX_XMI_ITERATIONS];
711         long[] extReadResults = new long[MAX_XMI_ITERATIONS];
712         int[] linksCounts = new int[MAX_XMI_ITERATIONS];
713         int[] objectsCounts = new int[MAX_XMI_ITERATIONS];
714         
715         File file = new File ("temp.xml");
716         String JavaDoc uri = null;
717         try {
718             uri = file.toURL ().toExternalForm ();
719         } catch (MalformedURLException e) {
720             fail (e.getMessage ());
721         }
722         
723         System.out.println("XMI export/import performance test started (it takes some time to complete)...");
724         for (int i = 0; i < MAX_XMI_ITERATIONS; i++) {
725             try {
726                 javaPackage = repository.createExtent(MODEL, mmPackage);
727             } catch (CreationFailedException e) {
728                 fail ("Package instantiation failed: " + MODEL);
729             }
730             
731             long seed = System.currentTimeMillis();
732             RandomDataSupport support = new RandomDataSupport (javaPackage);
733             support.generateObjects (seed, MAX_INST, false, false);
734             objectsCounts[i] = support.getInstancesCounter();
735             support.generateAssociations (seed, MAX_INST, false, false);
736             linksCounts[i] = support.getLinksCounter();
737             objectsCounts[i] += support.getInstancesCounter();
738                         
739             OutputStream os = null;
740             try {
741                 os = new FileOutputStream (file);
742             } catch (IOException e) {
743                 fail (e.getMessage ());
744             }
745             System.gc();
746             
747             try {
748                 time = System.currentTimeMillis();
749                 writer.write (os, javaPackage);
750                 os.flush ();
751                 os.close ();
752             } catch (Exception JavaDoc e) {
753                 fail (e.getMessage ());
754             }
755             writeResults[i] = System.currentTimeMillis() - time;
756             
757             javaPackage.refDelete ();
758             try {
759                 javaPackage = repository.createExtent(MODEL, mmPackage);
760             } catch (CreationFailedException e) {
761                 fail ("Package instantiation failed: " + MODEL);
762             }
763             RefPackage [] extents = new RefPackage [] {javaPackage};
764             System.gc();
765             
766             extTime = System.currentTimeMillis();
767             try {
768                 repository.beginTrans (true);
769                 time = System.currentTimeMillis();
770                 reader.read (uri, extents);
771                 readResults[i] = System.currentTimeMillis() - time;
772             } catch (Exception JavaDoc e) {
773                 fail (e.getMessage ());
774             } finally {
775                 repository.endTrans ();
776             }
777             extReadResults[i] = System.currentTimeMillis() - extTime;
778             javaPackage.refDelete ();
779             
780         } // for
781
System.out.println("Done.");
782         
783         int numberOfInst = 0, numberOfLinks = 0;
784         long totalWriteTime = 0, totalReadTime = 0, totalExtReadTime = 0;
785         for (int i = 0; i < MAX_XMI_ITERATIONS; i++) {
786             numberOfInst += objectsCounts[i];
787             numberOfLinks += linksCounts[i];
788             totalWriteTime += writeResults[i];
789             totalReadTime += readResults[i];
790             totalExtReadTime += extReadResults[i];
791             if (enabled) {
792                 PerformanceServerConnection conn = new PerformanceServerConnection();
793                 conn.logTestCase("MDR - 100 objects import time", readResults[i] * 100 / objectsCounts[i]);
794                 conn.logTestCase("MDR - 100 objects import time (including transactions)", extReadResults[i] * 100 / objectsCounts[i]);
795                 conn.logTestCase("MDR - 100 objects export time", writeResults[i] * 100 / objectsCounts[i]);
796             }
797         }
798
799         PrintStream log = getLog();
800         log.println("Random data export/import test results:");
801         log.println("---------------------------------------");
802         log.println("Number of exported/imported instances: " + numberOfInst);
803         log.println("Number of exported/imported links: " + numberOfLinks);
804         log.println("Total import time (without transactions): " + (float)totalReadTime/(float)1000 + " s");
805         log.println("Total import time: " + (float)totalExtReadTime/(float)1000 + " s");
806         log.println("Total export time: " + (float)totalWriteTime/(float)1000 + " s");
807         log.println("Average import time (without transactions) per instance: " + (float)totalReadTime/(float)numberOfInst + " ms");
808         log.println("Average import time per instance: " + (float)totalExtReadTime/(float)numberOfInst + " ms");
809         log.println("Average export time per instance: " + (float)totalWriteTime/(float)numberOfInst + " ms");
810         log.println();
811     }
812
813 }
814
815
816
Popular Tags