OLD | NEW |
| (Empty) |
1 typedef void Test(); | |
2 typedef void Operation(); | |
3 typedef void Reporter(Map<String, Result> results); | |
4 | |
5 class Suite { | |
6 /** | |
7 * Ctor. | |
8 * [:_window:] The window of the suite. | |
9 * [:_name:] The name of the suite. | |
10 */ | |
11 Suite(this._window, this._name) : | |
12 _operations = new List<Operation>(), | |
13 _nTests = 0, _nRanTests = 0 { | |
14 _window.addEventListener( | |
15 'message', | |
16 (MessageEvent event) { | |
17 String command = event.data; | |
18 switch (command) { | |
19 case 'start': | |
20 _run(); | |
21 return; | |
22 | |
23 default: | |
24 _window.alert('[${_name}]: unknown command ${command}'); | |
25 } | |
26 }, | |
27 false | |
28 ); | |
29 } | |
30 | |
31 /** | |
32 * Adds a preparation step to the suite. | |
33 * [:operation:] The operation to be performed. | |
34 */ | |
35 Suite prep(Operation operation){ | |
36 return _addOperation(operation); | |
37 } | |
38 | |
39 // How many times each individual test should be ran. | |
40 static final int _N_RUNS = 5; | |
41 | |
42 /** | |
43 * Adds another test to the suite. | |
44 * [:name:] The unique name of the test | |
45 * [:test:] A function holding the test to run | |
46 */ | |
47 Suite test(String name, Test test_) { | |
48 _nTests++; | |
49 // Don't execute the test immediately. | |
50 return _addOperation(() { | |
51 // List of number of runs in seconds. | |
52 List<double> runsPerSecond = new List<double>(); | |
53 | |
54 // Run the test several times. | |
55 try { | |
56 // TODO(antonm): use .setTimeout to schedule next run as JS | |
57 // version does. That allows to report the intermidiate results | |
58 // more smoothly as well. | |
59 for (int i = 0; i < _N_RUNS; i++) { | |
60 int runs = 0; | |
61 final int start = new Date.now().value; | |
62 | |
63 int cur = new Date.now().value; | |
64 while ((cur - start) < 1000) { | |
65 test_(); | |
66 cur = new Date.now().value; | |
67 runs++; | |
68 } | |
69 | |
70 runsPerSecond.add((runs * 1000.0) / (cur - start)); | |
71 } | |
72 } catch(var exception, var stacktrace) { | |
73 _window.alert('Exception ${exception}: ${stacktrace}'); | |
74 return; | |
75 } | |
76 _reportTestResults(name, new Result(runsPerSecond)); | |
77 }); | |
78 } | |
79 | |
80 /** | |
81 * Finalizes the suite. | |
82 * It might either run the tests immediately or schedule them to be ran later. | |
83 */ | |
84 void end() { | |
85 _postMessage('inited', { 'nTests': _nTests }); | |
86 | |
87 } | |
88 | |
89 _run() { | |
90 int currentOperation = 0; | |
91 handler() { | |
92 if (currentOperation < _operations.length) { | |
93 _operations[currentOperation](); | |
94 currentOperation++; | |
95 _window.setTimeout(handler, 1); | |
96 } else { | |
97 _postMessage('over'); | |
98 } | |
99 } | |
100 _window.setTimeout(handler, 0); | |
101 } | |
102 | |
103 _reportTestResults(String name, Result result) { | |
104 _nRanTests++; | |
105 _postMessage('result', { | |
106 'testName': name, | |
107 'mean': result.mean, | |
108 'error': result.error / result.mean, | |
109 'percent': (100.0 * _nRanTests / _nTests) | |
110 }); | |
111 } | |
112 | |
113 _postMessage(String command, [var data = null]) { | |
114 final payload = { 'command': command }; | |
115 if (data != null) { | |
116 payload['data'] = data; | |
117 } | |
118 // TODO(antonm): Remove dynamic below. | |
119 _window.top.dynamic.postMessage(JSON.stringify(payload), '*'); | |
120 } | |
121 | |
122 // Implementation. | |
123 | |
124 final Window _window; | |
125 final String _name; | |
126 | |
127 List<Operation> _operations; | |
128 int _nTests; | |
129 int _nRanTests; | |
130 | |
131 Suite _addOperation(Operation operation) { | |
132 _operations.add(operation); | |
133 return this; | |
134 } | |
135 } | |
OLD | NEW |