summaryrefslogtreecommitdiffstats
path: root/contrib/benchmarks/apache/src/test/java/org/uscxml/benchmark/BenchmarkTest.java
blob: 08a9a383de5312b91678956f92247e099ffe1c9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.uscxml.benchmark;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.net.URL;
import java.util.List;

import org.apache.commons.scxml2.*;

import org.apache.commons.scxml2.env.SimpleDispatcher;
import org.apache.commons.scxml2.env.SimpleErrorReporter;
import org.apache.commons.scxml2.env.Tracer;
import org.apache.commons.scxml2.io.SCXMLReader;
import org.apache.commons.scxml2.io.SCXMLReader.Configuration;
import org.apache.commons.scxml2.model.CustomAction;
import org.apache.commons.scxml2.model.EnterableState;
import org.apache.commons.scxml2.model.Transition;
import org.apache.commons.scxml2.model.SCXML;
import org.apache.commons.scxml2.model.TransitionTarget;
import org.apache.commons.scxml2.model.EnterableState;
import org.apache.commons.scxml2.model.TransitionTarget;

/**
 * Unit test for simple App.
 */
public class BenchmarkTest extends TestCase {

    public long initMs = 0;

    class PerformanceListener extends SimpleErrorReporter implements SCXMLListener {
        public long iterations = 0;
        public long mark = System.currentTimeMillis();

        public void onEntry(final EnterableState state) {
            if (state.getId().equals("mark")) {
                iterations++;
                long now = System.currentTimeMillis();
                if (now - mark > 1000) {
                    System.out.println(initMs + ", " + iterations);
                    mark = now;
                    iterations = 0;
                }
            }
        }
        public void onExit(final EnterableState state) {}
        public void onTransition(final TransitionTarget from, final TransitionTarget to, final Transition transition, String event) {}

    }

  public SCXML parse(final URL url, final List<CustomAction> customActions) throws Exception {
      Configuration configuration = new Configuration(null, null, customActions);
      SCXML scxml = SCXMLReader.read(url, configuration);
      return scxml;
  }

  public SCXMLExecutor getExecutor(final SCXML scxml, final Evaluator evaluator, final EventDispatcher eventDispatcher) throws Exception {
      PerformanceListener trc = new PerformanceListener();
      SCXMLExecutor exec = new SCXMLExecutor(evaluator, eventDispatcher, null);
      exec.setStateMachine(scxml);
      exec.addListener(scxml, trc);
      return exec;
  }

    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public BenchmarkTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( BenchmarkTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        try {
            long started = System.currentTimeMillis();
            String fileName = System.getenv("USCXML_BENCHMARK");
            SCXML scxml = parse(new URL("file:" + fileName), null);
            SCXMLExecutor exec = getExecutor(scxml, null, new SimpleDispatcher());
            initMs = System.currentTimeMillis() - started;
            exec.go();
        } catch (Exception e) {
            System.err.println(e);
            assertTrue(false);
        }
    }
}