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