]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/base/CommandSequenceTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / base / CommandSequenceTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.db.testing.base;
13
14 import gnu.trove.list.array.TLongArrayList;
15
16 import org.apache.commons.math3.stat.regression.SimpleRegression;
17 import org.junit.Test;
18 import org.simantics.db.Session;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.testing.cases.FreshDatabaseTest;
21 import org.simantics.db.testing.common.Command;
22 import org.simantics.db.testing.common.CommandSequenceEnvironment;
23 import org.simantics.db.testing.common.CommandSpec;
24
25
26 /**
27  * 
28  * @author Antti Villberg <antti.villberg@semantum.fi>
29  * 
30  */
31 abstract public class CommandSequenceTest extends FreshDatabaseTest implements CommandSequenceEnvironment {
32
33         private static final boolean TRACE = true;
34
35         protected TLongArrayList executionTimes = new TLongArrayList();
36
37         protected double getSlowdown() {
38
39                 SimpleRegression regression = new SimpleRegression();
40                 for(int i=0;i<executionTimes.size();i++) {
41                         regression.addData(i, executionTimes.getQuick(i));
42                 }
43                 double intercept = regression.getIntercept();
44                 double slope = regression.getSlope();
45                 
46                 return (slope * executionTimes.size())/Math.abs(intercept);
47
48         }
49         
50         protected String getExecutionData() {
51                 StringBuilder b = new StringBuilder();
52                 for(int i=0;i<executionTimes.size();i++) {
53                         b.append(executionTimes.get(i) + "\n");
54                 }
55                 return b.toString();
56         }
57         
58         protected void printExecutionTimes() {
59                 for(int i=0;i<executionTimes.size();i++) {
60                         System.err.println("" + i + ": " + executionTimes.getQuick(i));
61                 }
62         }
63         
64         protected void assertSlowdown(double slowdown) {
65                 assertLess(getSlowdown(), 0.1, getExecutionData());
66         }
67         
68         protected int getSequenceSize() {
69                 return 0;
70         }
71         
72         protected Command[] beforeSequence(CommandSequenceEnvironment environment) throws Exception {
73                 return new Command[0];
74         }
75         
76         protected Command[] afterSequence(CommandSequenceEnvironment environment) throws Exception {
77                 return new Command[0];
78         }
79
80         protected CommandSpec<?>[] getFactories() {
81                 return null;
82         }
83         
84         @Override
85         public Session getSession() {
86                 return super.getSession();
87         }
88         
89         public int randomNatural() {
90                 return (int)(Math.random() * Integer.MAX_VALUE);
91         }
92         
93         protected Command[] newSequence() throws Exception {
94                 int size = getSequenceSize();
95                 CommandSpec<?>[] classes = getFactories();
96                 int index = 0;
97                 Command[] result = new Command[size];
98                 double totalWeight = 0;
99                 for(CommandSpec<?> p : classes) totalWeight += p.weight;
100                 double[] wts = new double[classes.length];
101                 index=0;
102                 double previous = 0;
103                 for(CommandSpec<?> p : classes) {
104                         double weight = p.weight/totalWeight;
105                         wts[index++] = previous + weight;
106                         previous += weight;
107                 }
108                 
109                 for(int i=0;i<size;i++) {
110                         double n = Math.random();
111                         for(int j=0;j<classes.length;j++) {
112                                 if(n < wts[j]) {
113                                         result[i] = classes[j].clazz.newInstance();
114                                         break;
115                                 }
116                                 if(j==result.length-1) {
117                                         result[i] = classes[result.length-1].clazz.newInstance();
118                                         break;
119                                 }
120                         }
121                 }
122                 return result;
123         }
124         
125         protected CommandSequenceEnvironment createEnvironment() {
126                 return this;
127         }
128         
129         protected void initialize(CommandSequenceEnvironment environment) throws Exception {
130                 
131         }
132
133         protected void analyse(CommandSequenceEnvironment environment) throws Exception {
134                 
135         }
136         
137         @Test
138         public void test() throws Exception {
139
140                 CommandSequenceEnvironment environment = createEnvironment();
141                 
142                 initialize(environment);
143                 
144         for(Command command : beforeSequence(environment)) {
145                 command.run(environment);
146         }
147                 
148         int counter=0;
149         int lastProgress=0;
150         
151         Command[] sequence = newSequence();
152         for(Command command : sequence) {
153                 if (TRACE)
154                         System.err.println("[" + counter + "/" + sequence.length +  "]: " + command.getClass().getSimpleName());
155                 long start = System.nanoTime();
156                 try {
157                         command.run(environment);
158                 } catch (Throwable t) {
159                         if(!isAllowed(t)) {
160                                 throw new AssertionError(t);
161                         }
162                 }
163                 long duration = System.nanoTime() - start;
164                 executionTimes.add(duration);
165                 int progress = 100*(counter++) / sequence.length;
166                 if(progress > lastProgress) {
167                         System.err.println("[" + counter + "/" + sequence.length +  "]: " + progress + "%");
168                         lastProgress = progress;
169                 }
170         }
171
172         for(Command command : afterSequence(environment)) {
173                 command.run(environment);
174         }
175         
176                 analyse(environment);
177         
178         }
179         
180         public void invoke(CommandSequenceEnvironment environment, Command command) {
181                 try {
182                         command.run(environment);
183                 } catch (Throwable t) {
184                         if(!isAllowed(t)) {
185                                 throw new AssertionError(t);
186                         }
187                 }
188         }
189         
190         boolean isAllowed(Throwable t) {
191                 if(t instanceof AllowedThrowable) return true;
192                 if(t instanceof DatabaseException) {
193                         DatabaseException d = (DatabaseException)t;
194                         if(d.getCause() instanceof AllowedThrowable) return true;
195                 }
196                 return false;
197         }
198         
199 }