]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/test/org/simantics/history/test/TestPerformance.java
Sleep 1 second before writing diagram SVG in hope to fix profile bug
[simantics/platform.git] / bundles / org.simantics.history / test / org / simantics / history / test / TestPerformance.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.history.test;
13
14 import java.io.File;
15 import java.io.IOException;
16
17 import org.junit.Test;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.Datatypes;
20 import org.simantics.databoard.type.RecordType;
21 import org.simantics.history.Collector;
22 import org.simantics.history.History;
23 import org.simantics.history.HistoryManager;
24 import org.simantics.history.impl.CollectorImpl;
25 import org.simantics.history.impl.CollectorState;
26 import org.simantics.history.util.subscription.SubscriptionItem;
27 import org.simantics.history.util.subscription.SamplingFormat;
28 import org.simantics.utils.FileUtils;
29
30 public class TestPerformance {
31
32         static final double NaN = Double.NaN;
33         
34         // Time            0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4
35         double[] data1 = { 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0, 7.0, 8.0, 9.0, NaN,10.0, NaN, NaN, NaN }; 
36         double[] data2 = { 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0, 7.0, 8.0, 9.0,10.0, 9.0, 5.0, 4.0, 3.0 };
37         byte[] data3 = { 5, 6, 7, 8, 9, 10, 9, 5, 4, 3 };
38         
39         // Subscription formats
40         SamplingFormat simple, allfields, vector, minmax, byteformat, string;
41
42         // History
43         HistoryManager historian;
44         File workarea;
45         Collector collector;
46         
47         @Test
48         public void testPerformance() throws Exception {
49
50         File workarea = FileUtils.createTmpDir();
51             
52                 byteformat = new SamplingFormat();
53                 byteformat.formatId = "byte";
54                 byteformat.format = new RecordType();
55                 RecordType format = (RecordType) (byteformat.format = new RecordType());
56                 format.addComponent("time", Datatypes.DOUBLE);
57                 format.addComponent("endTime", Datatypes.DOUBLE);
58                 format.addComponent("value", Datatypes.BYTE);
59                 format.addComponent("lastValue", Datatypes.BYTE);               
60                 format.addComponent("min", Datatypes.BYTE);
61                 format.addComponent("max", Datatypes.BYTE);
62                 format.addComponent("avg", Datatypes.DOUBLE);
63                 format.addComponent("median", Datatypes.BYTE);
64                 format.addComponent("quality", Datatypes.BYTE);
65                 format.addComponent("count", Datatypes.INTEGER);
66                 byteformat.interval = NaN;
67                 byteformat.deadband = NaN;              
68                 
69                 CollectorState cs;
70                 // Write data
71                 for (int z=0; z<20; z++) {
72                         cs = new CollectorState();
73                         historian = History.openFileHistory(workarea);
74                         collector = new CollectorImpl( historian );
75                                 
76                         // 2. Write data
77                         SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", byteformat );
78                         historian.create( hi );
79                         collector.addItem( hi );
80                         System.gc();
81                         long startTime = System.nanoTime();
82                         double[] data = data2;
83                         for (int j=0; j<10000; j++) {
84                                 for (int i=0; i<data.length; i++) 
85                                 {
86                                         collector.beginStep(Bindings.DOUBLE, i * 0.1);
87                                         collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
88                                         collector.endStep();
89                                 }                       
90                         }
91                         collector.close();
92                         long elapsedtime = System.nanoTime()-startTime;
93                         System.out.println("Elapsed: "+(elapsedtime/1000L)+"µs");
94                 }
95
96         if ( workarea != null) {
97             try {
98                 FileUtils.deleteAll(workarea);
99             } catch (IOException e) {
100                 e.printStackTrace();
101             }
102         }
103                 
104         }
105         
106 }