1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.history.test;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\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
30 public class TestPerformance {
\r
32 static final double NaN = Double.NaN;
\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
39 // Subscription formats
\r
40 SamplingFormat simple, allfields, vector, minmax, byteformat, string;
\r
43 HistoryManager historian;
\r
45 Collector collector;
\r
48 public void testPerformance() throws Exception {
\r
50 File workarea = FileUtils.createTmpDir();
\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
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
77 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", byteformat );
\r
78 historian.create( hi );
\r
79 collector.addItem( hi );
\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
86 collector.beginStep(Bindings.DOUBLE, i * 0.1);
\r
87 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
\r
88 collector.endStep();
\r
92 long elapsedtime = System.nanoTime()-startTime;
\r
93 System.out.println("Elapsed: "+(elapsedtime/1000L)+"µs");
\r
96 if ( workarea != null) {
\r
98 FileUtils.deleteAll(workarea);
\r
99 } catch (IOException e) {
\r
100 e.printStackTrace();
\r