summaryrefslogtreecommitdiffstats
path: root/test/src/test-w3c.cpp
blob: 2c2f4da6e2d59e152689a1c9cf738add52498f93 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// I feel dirty, but we need to access the datamodel timer
// #define protected public

#include "uscxml/config.h"
#include "uscxml/Common.h"
#include "uscxml/Convenience.h"
#include "uscxml/Interpreter.h"
#include "uscxml/DOMUtils.h"

#include "uscxml/Factory.h"
#include "uscxml/server/HTTPServer.h"

#include "uscxml/transform/ChartToFlatSCXML.h"
#include <glog/logging.h>
#include <boost/algorithm/string.hpp>

#ifdef HAS_SIGNAL_H
#include <signal.h>
#endif

#ifdef BUILD_PROFILING
#   include "uscxml/plugins/DataModel.h"
# endif

#ifdef _WIN32
#include "XGetopt.h"
#include "XGetopt.cpp"
#endif

static bool withFlattening = false;
static double delayFactor = 1;
static size_t benchmarkRuns = 0;
static std::string documentURI;

int retCode = EXIT_FAILURE;
uscxml::Interpreter interpreter;

void printUsageAndExit(const char* progName) {
    // remove path from program name
    std::string progStr(progName);
    if (progStr.find_last_of(PATH_SEPERATOR) != std::string::npos) {
        progStr = progStr.substr(progStr.find_last_of(PATH_SEPERATOR) + 1, progStr.length() - (progStr.find_last_of(PATH_SEPERATOR) + 1));
    }
    
    printf("%s version " USCXML_VERSION " (" CMAKE_BUILD_TYPE " build - " CMAKE_COMPILER_STRING ")\n", progStr.c_str());
    printf("Usage\n");
    printf("\t%s", progStr.c_str());
    printf(" [-f] [-dN] [-bN]");
#ifdef BUILD_AS_PLUGINS
    printf(" [-p pluginPath]");
#endif
    printf(" URL");
    printf("\n");
    printf("Options\n");
    printf("\t-f             : flatten to SCXML state-machine\n");
    printf("\t-d FACTOR      : delay factor\n");
    printf("\t-b ITERATIONS  : benchmark with number of runs\n");
    printf("\n");
    exit(1);
}

class W3CStatusMonitor : public uscxml::InterpreterMonitor {

void beforeCompletion(uscxml::Interpreter tmp) {
	if (interpreter.getConfiguration().size() == 1 && interpreter.isInState("pass")) {
		std::cout << "TEST SUCCEEDED" << std::endl;
		retCode = EXIT_SUCCESS;
		return;
	}
	std::cout << "TEST FAILED" << std::endl;
}
};

int main(int argc, char** argv) {
	using namespace uscxml;

	try {

#if defined(HAS_SIGNAL_H) && !defined(WIN32)
		signal(SIGPIPE, SIG_IGN);
#endif

		if (argc < 2) {
			exit(EXIT_FAILURE);
		}

		google::InitGoogleLogging(argv[0]);
		google::LogToStderr();

		HTTPServer::getInstance(32954, 32955, NULL); // bind to some random tcp sockets for ioprocessor tests

		char* dfEnv = getenv("USCXML_DELAY_FACTOR");
		if (dfEnv) {
			delayFactor = strTo<double>(dfEnv);
		}

		int option;
		while ((option = getopt(argc, argv, "fd:b:")) != -1) {
			switch(option) {
			case 'f':
				withFlattening = true;
				break;
			case 'd':
				delayFactor = strTo<double>(optarg);
				break;
            case 'b':
                benchmarkRuns = strTo<size_t>(optarg);
                break;
			default:
				break;
			}
		}
        
        const char* envBenchmarkRuns = getenv("USCXML_BENCHMARK_ITERATIONS");
        if (envBenchmarkRuns != NULL) {
            benchmarkRuns = strTo<size_t>(envBenchmarkRuns);
        }
        
		documentURI = argv[optind];

        LOG(INFO) << "Processing " << documentURI << (withFlattening ? " FSM converted" : "") << (delayFactor ? "" : " with delays *= " + toStr(delayFactor)) << (benchmarkRuns > 0 ? " for " + toStr(benchmarkRuns) + " benchmarks" : "");
		if (withFlattening) {
			interpreter = Interpreter::fromURL(documentURI);
			Transformer flattener = ChartToFlatSCXML::transform(interpreter);
			interpreter = flattener;
//			std::cout << interpreter.getDocument() << std::endl;
		} else {
			interpreter = Interpreter::fromURL(documentURI);
		}
		
		if (delayFactor != 1) {
			Arabica::DOM::Document<std::string> document = interpreter.getDocument();
			Arabica::DOM::Element<std::string> root = document.getDocumentElement();
			Arabica::XPath::NodeSet<std::string> sends = InterpreterImpl::filterChildElements(interpreter.getNameSpaceInfo().xmlNSPrefix + "send", root, true);

			for (int i = 0; i < sends.size(); i++) {
				Arabica::DOM::Element<std::string> send = Arabica::DOM::Element<std::string>(sends[i]);
				if (HAS_ATTR(send, "delay")) {
					NumAttr delay(ATTR(send, "delay"));
					int value = strTo<int>(delay.value);
					if (delay.unit == "s")
						value *= 1000;
					value *= delayFactor;
					send.setAttribute("delay", toStr(value) + "ms");
					std::cout << ATTR(send, "delay") << std::endl;
				} else if (HAS_ATTR(send, "delayexpr")) {
					std::string delayExpr = ATTR(send, "delayexpr");
					send.setAttribute("delayexpr",
					                  "(" + delayExpr + ".indexOf('ms', " + delayExpr + ".length - 2) !== -1 ? "
					                  "(" + delayExpr + ".slice(0,-2) * " + toStr(delayFactor) + ") + \"ms\" : "
					                  "(" + delayExpr + ".slice(0,-1) * 1000 * " + toStr(delayFactor) + ") + \"ms\")");
					std::cout << ATTR(send, "delayexpr") << std::endl;
				}
			}
			std::list<InterpreterIssue> issues = interpreter.validate();
			for (std::list<InterpreterIssue>::iterator issueIter = issues.begin(); issueIter != issues.end(); issueIter++) {
				std::cout << *issueIter << std::endl;
			}
		}

		if (interpreter) {
            W3CStatusMonitor* vm = new W3CStatusMonitor();
            interpreter.addMonitor(vm);

            if (benchmarkRuns > 0) {
                LOG(INFO) << "Benchmarking " << documentURI << (withFlattening ? " FSM converted" : "") << (delayFactor ? "" : " with delays *= " + toStr(delayFactor));

                InterpreterState state = interpreter.getState();

                double avg = 0;
#ifdef BUILD_PROFILING
                double avgDm = 0;
                double avgStep = 0;
#endif
                size_t remainingRuns = benchmarkRuns;
                uint64_t start = tthread::chrono::system_clock::now();

                while(remainingRuns-- > 0) {
                    Timer t;
                    t.start();
                    for(;;) {
                        state = interpreter.step(true);
                        if (state == USCXML_FINISHED) {
#ifdef BUILD_PROFILING
                            avgDm += interpreter.getDataModel().timer.elapsed;
                            interpreter.getDataModel().timer.reset();
                            avgStep += interpreter.timer.elapsed;
#endif
                        }
                        if (state < 0)
                            break;
                    }
                    t.stop();
                    avg += t.elapsed;
                    interpreter.reset();
                }
                uint64_t totalDuration = tthread::chrono::system_clock::now() - start;
                std::cout << benchmarkRuns << " iterations in " << totalDuration << " ms" << std::endl;
                std::cout << (avg * 1000.0)  / (double)benchmarkRuns << " ms on average" << std::endl;
#ifdef BUILD_PROFILING
                std::cout << (avgDm * 1000.0) / (double)benchmarkRuns << " ms in datamodel" << std::endl;
                std::cout << (avgStep * 1000.0) / (double)benchmarkRuns << " ms in microsteps" << std::endl;
#endif

            } else {
                interpreter.start();
                while(interpreter.runOnMainThread(25));
            }
		}
	} catch(Event e) {
		std::cout << e << std::endl;
	} catch(std::exception e) {
		std::cout << e.what() << std::endl;
	}
	return retCode;
}