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