]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.history/test/org/simantics/history/test/TestPerformance.java b/bundles/org.simantics.history/test/org/simantics/history/test/TestPerformance.java
new file mode 100644 (file)
index 0000000..a70da5e
--- /dev/null
@@ -0,0 +1,106 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.history.test;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+import org.junit.Test;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.type.RecordType;\r
+import org.simantics.history.Collector;\r
+import org.simantics.history.History;\r
+import org.simantics.history.HistoryManager;\r
+import org.simantics.history.impl.CollectorImpl;\r
+import org.simantics.history.impl.CollectorState;\r
+import org.simantics.history.util.subscription.SubscriptionItem;\r
+import org.simantics.history.util.subscription.SamplingFormat;\r
+import org.simantics.utils.FileUtils;\r
+\r
+public class TestPerformance {\r
+\r
+       static final double NaN = Double.NaN;\r
+       \r
+       // 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
+       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
+       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
+       byte[] data3 = { 5, 6, 7, 8, 9, 10, 9, 5, 4, 3 };\r
+       \r
+       // Subscription formats\r
+       SamplingFormat simple, allfields, vector, minmax, byteformat, string;\r
+\r
+       // History\r
+       HistoryManager historian;\r
+       File workarea;\r
+       Collector collector;\r
+       \r
+       @Test\r
+       public void testPerformance() throws Exception {\r
+\r
+        File workarea = FileUtils.createTmpDir();\r
+           \r
+               byteformat = new SamplingFormat();\r
+               byteformat.formatId = "byte";\r
+               byteformat.format = new RecordType();\r
+               RecordType format = (RecordType) (byteformat.format = new RecordType());\r
+               format.addComponent("time", Datatypes.DOUBLE);\r
+               format.addComponent("endTime", Datatypes.DOUBLE);\r
+               format.addComponent("value", Datatypes.BYTE);\r
+               format.addComponent("lastValue", Datatypes.BYTE);               \r
+               format.addComponent("min", Datatypes.BYTE);\r
+               format.addComponent("max", Datatypes.BYTE);\r
+               format.addComponent("avg", Datatypes.DOUBLE);\r
+               format.addComponent("median", Datatypes.BYTE);\r
+               format.addComponent("quality", Datatypes.BYTE);\r
+               format.addComponent("count", Datatypes.INTEGER);\r
+               byteformat.interval = NaN;\r
+               byteformat.deadband = NaN;              \r
+               \r
+               CollectorState cs;\r
+               // Write data\r
+               for (int z=0; z<20; z++) {\r
+                       cs = new CollectorState();\r
+                       historian = History.openFileHistory(workarea);\r
+                       collector = new CollectorImpl( historian );\r
+                               \r
+                       // 2. Write data\r
+                       SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", byteformat );\r
+                       historian.create( hi );\r
+                       collector.addItem( hi );\r
+                       System.gc();\r
+                       long startTime = System.nanoTime();\r
+                       double[] data = data2;\r
+                       for (int j=0; j<10000; j++) {\r
+                               for (int i=0; i<data.length; i++) \r
+                               {\r
+                                       collector.beginStep(Bindings.DOUBLE, i * 0.1);\r
+                                       collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);\r
+                                       collector.endStep();\r
+                               }                       \r
+                       }\r
+                       collector.close();\r
+                       long elapsedtime = System.nanoTime()-startTime;\r
+                       System.out.println("Elapsed: "+(elapsedtime/1000L)+"µs");\r
+               }\r
+\r
+        if ( workarea != null) {\r
+            try {\r
+                FileUtils.deleteAll(workarea);\r
+            } catch (IOException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+               \r
+       }\r
+       \r
+}\r