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