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