1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.db.testing.base;
14 import gnu.trove.list.array.TLongArrayList;
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;
28 * @author Antti Villberg <antti.villberg@semantum.fi>
31 abstract public class CommandSequenceTest extends FreshDatabaseTest implements CommandSequenceEnvironment {
33 private static final boolean TRACE = true;
35 protected TLongArrayList executionTimes = new TLongArrayList();
37 protected double getSlowdown() {
39 SimpleRegression regression = new SimpleRegression();
40 for(int i=0;i<executionTimes.size();i++) {
41 regression.addData(i, executionTimes.getQuick(i));
43 double intercept = regression.getIntercept();
44 double slope = regression.getSlope();
46 return (slope * executionTimes.size())/Math.abs(intercept);
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");
58 protected void printExecutionTimes() {
59 for(int i=0;i<executionTimes.size();i++) {
60 System.err.println("" + i + ": " + executionTimes.getQuick(i));
64 protected void assertSlowdown(double slowdown) {
65 assertLess(getSlowdown(), 0.1, getExecutionData());
68 protected int getSequenceSize() {
72 protected Command[] beforeSequence(CommandSequenceEnvironment environment) throws Exception {
73 return new Command[0];
76 protected Command[] afterSequence(CommandSequenceEnvironment environment) throws Exception {
77 return new Command[0];
80 protected CommandSpec<?>[] getFactories() {
85 public Session getSession() {
86 return super.getSession();
89 public int randomNatural() {
90 return (int)(Math.random() * Integer.MAX_VALUE);
93 protected Command[] newSequence() throws Exception {
94 int size = getSequenceSize();
95 CommandSpec<?>[] classes = getFactories();
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];
103 for(CommandSpec<?> p : classes) {
104 double weight = p.weight/totalWeight;
105 wts[index++] = previous + weight;
109 for(int i=0;i<size;i++) {
110 double n = Math.random();
111 for(int j=0;j<classes.length;j++) {
113 result[i] = classes[j].clazz.newInstance();
116 if(j==result.length-1) {
117 result[i] = classes[result.length-1].clazz.newInstance();
125 protected CommandSequenceEnvironment createEnvironment() {
129 protected void initialize(CommandSequenceEnvironment environment) throws Exception {
133 protected void analyse(CommandSequenceEnvironment environment) throws Exception {
138 public void test() throws Exception {
140 CommandSequenceEnvironment environment = createEnvironment();
142 initialize(environment);
144 for(Command command : beforeSequence(environment)) {
145 command.run(environment);
151 Command[] sequence = newSequence();
152 for(Command command : sequence) {
154 System.err.println("[" + counter + "/" + sequence.length + "]: " + command.getClass().getSimpleName());
155 long start = System.nanoTime();
157 command.run(environment);
158 } catch (Throwable t) {
160 throw new AssertionError(t);
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;
172 for(Command command : afterSequence(environment)) {
173 command.run(environment);
176 analyse(environment);
180 public void invoke(CommandSequenceEnvironment environment, Command command) {
182 command.run(environment);
183 } catch (Throwable t) {
185 throw new AssertionError(t);
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;