1 | (function (factory) {
|
2 | if (typeof module === "object" && typeof module.exports === "object") {
|
3 | var v = factory(require, exports);
|
4 | if (v !== undefined) module.exports = v;
|
5 | }
|
6 | else if (typeof define === "function" && define.amd) {
|
7 | define(["require", "exports", "tslib", "@theintern/common", "lodash", "platform", "benchmark", "./Test"], factory);
|
8 | }
|
9 | })(function (require, exports) {
|
10 | "use strict";
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | var tslib_1 = require("tslib");
|
13 | var common_1 = require("@theintern/common");
|
14 | var _ = require("lodash");
|
15 | var platform = require("platform");
|
16 | var Benchmark = require("benchmark");
|
17 | var Test_1 = require("./Test");
|
18 | Benchmark.runInContext({ _: _, platform: platform });
|
19 | var BenchmarkTest = (function (_super) {
|
20 | tslib_1.__extends(BenchmarkTest, _super);
|
21 | function BenchmarkTest(descriptor) {
|
22 | var _this = this;
|
23 | var args = {};
|
24 | Object.keys(descriptor).forEach(function (descriptorKey) {
|
25 | var key = descriptorKey;
|
26 | if (key !== 'options') {
|
27 | args[key] = descriptor[key];
|
28 | }
|
29 | });
|
30 | var testArgs = args;
|
31 | testArgs.test = testArgs.test || function () { };
|
32 | _this = _super.call(this, testArgs) || this;
|
33 | var options = Object.assign({}, _this.test.options || {}, {
|
34 | async: true,
|
35 | setup: createLifecycle(true),
|
36 | teardown: createLifecycle(false)
|
37 | });
|
38 | if (options.defer) {
|
39 | _this.test = (function (testFunction) {
|
40 | return (function (deferred) {
|
41 | var dfd = createDeferred(this.benchmark, deferred, options.numCallsUntilResolution);
|
42 | testFunction.call(this, dfd);
|
43 | });
|
44 | })(_this.test);
|
45 | }
|
46 | _this.benchmark = new Benchmark(descriptor.name, options.defer
|
47 | ? 'this.benchmark.internTest.test(deferred);'
|
48 | : 'this.internTest.test();', options);
|
49 | Object.defineProperty(_this.benchmark, 'name', {
|
50 | get: function () {
|
51 | return _this.name;
|
52 | },
|
53 | set: function (name) {
|
54 | _this.name = name;
|
55 | }
|
56 | });
|
57 | _this.benchmark.internTest = _this;
|
58 | return _this;
|
59 | }
|
60 | Object.defineProperty(BenchmarkTest.prototype, "timeElapsed", {
|
61 | get: function () {
|
62 | if (this.benchmark && this.benchmark.times) {
|
63 | return this.benchmark.times.elapsed;
|
64 | }
|
65 | return 0;
|
66 | },
|
67 | set: function (_value) {
|
68 | },
|
69 | enumerable: true,
|
70 | configurable: true
|
71 | });
|
72 | BenchmarkTest.prototype.async = function (_timeout, _numCallsUntilResolution) {
|
73 | throw new Error('Benchmark tests must be marked as asynchronous and use the deferred ' +
|
74 | 'passed to them rather than call `this.async()`.');
|
75 | };
|
76 | BenchmarkTest.prototype.run = function () {
|
77 | var _this = this;
|
78 | this._hasPassed = false;
|
79 | this._usesRemote = false;
|
80 | var benchmark = this.benchmark;
|
81 | return new common_1.Task(function (resolve, reject) {
|
82 | benchmark.on('abort', function () {
|
83 | reject(benchmark.error);
|
84 | });
|
85 | benchmark.on('error', function () {
|
86 | if (benchmark.error === Test_1.SKIP) {
|
87 | resolve();
|
88 | }
|
89 | else {
|
90 | reject(benchmark.error);
|
91 | }
|
92 | });
|
93 | benchmark.on('complete', function () {
|
94 | resolve();
|
95 | });
|
96 | _this.executor.emit('testStart', _this).then(function () {
|
97 | benchmark.run();
|
98 | });
|
99 | }, function () {
|
100 | benchmark.abort();
|
101 | })
|
102 | .finally(function () {
|
103 | benchmark.off();
|
104 | })
|
105 | .then(function () {
|
106 | _this._hasPassed = true;
|
107 | }, function (error) {
|
108 | _this.error = error;
|
109 | throw error;
|
110 | })
|
111 | .finally(function () { return _this.executor.emit('testEnd', _this); });
|
112 | };
|
113 | BenchmarkTest.prototype.toJSON = function () {
|
114 | var json = _super.prototype.toJSON.call(this);
|
115 | var benchmark = this.benchmark;
|
116 | json.benchmark = {
|
117 | hz: benchmark.hz,
|
118 | times: benchmark.times,
|
119 | stats: benchmark.stats
|
120 | };
|
121 | return json;
|
122 | };
|
123 | BenchmarkTest.async = function (testFunction, numCallsUntilResolution) {
|
124 | testFunction.options = Object.assign({}, testFunction.options || {}, {
|
125 | defer: true,
|
126 | numCallsUntilResolution: numCallsUntilResolution
|
127 | });
|
128 | return testFunction;
|
129 | };
|
130 | return BenchmarkTest;
|
131 | }(Test_1.default));
|
132 | exports.default = BenchmarkTest;
|
133 | function isBenchmarkTest(value) {
|
134 | return value && value.benchmark != null && Test_1.isTest(value);
|
135 | }
|
136 | exports.isBenchmarkTest = isBenchmarkTest;
|
137 | var createLifecycle = function (before) {
|
138 | var queueName = before ? 'Before' : 'After';
|
139 | var queueMethod = before ? 'push' : 'unshift';
|
140 | var methodName = before ? 'before' : 'after';
|
141 | return [
|
142 | '(function (benchmark) {',
|
143 | "\tvar queue = benchmark.intern" + queueName + "EachLoopQueue;",
|
144 | ' var suite;',
|
145 | ' if (!queue) {',
|
146 | ' suite = benchmark.internTest;',
|
147 | "\t\tbenchmark.intern" + queueName + "EachLoopQueue = queue = [];",
|
148 | ' while ((suite = suite.parent)) {',
|
149 | "\t\t\tif (suite." + methodName + "EachLoop) {",
|
150 | "\t\t\t\tqueue." + queueMethod + "(suite);",
|
151 | ' }',
|
152 | ' }',
|
153 | ' }',
|
154 | ' var i = queue.length;',
|
155 | ' while((suite = queue[--i])) {',
|
156 | "\t\tsuite." + methodName + "EachLoop();",
|
157 | ' }',
|
158 | '})(this.benchmark || this);\n'
|
159 | ].join('\n');
|
160 | };
|
161 | function createDeferred(benchmark, deferred, numCallsUntilResolution) {
|
162 | var remainingCalls = numCallsUntilResolution || 1;
|
163 | return {
|
164 | resolve: function () {
|
165 | --remainingCalls;
|
166 | if (remainingCalls === 0) {
|
167 | deferred.resolve();
|
168 | }
|
169 | else if (remainingCalls < 0) {
|
170 | throw new Error('resolve called too many times');
|
171 | }
|
172 | },
|
173 | reject: function (error) {
|
174 | benchmark.error = error;
|
175 | benchmark.abort();
|
176 | deferred.resolve();
|
177 | },
|
178 | rejectOnError: function (callback) {
|
179 | var self = this;
|
180 | return function () {
|
181 | try {
|
182 | return callback.apply(this, arguments);
|
183 | }
|
184 | catch (error) {
|
185 | self.reject(error);
|
186 | }
|
187 | };
|
188 | },
|
189 | callback: function (callback) {
|
190 | var self = this;
|
191 | return this.rejectOnError(function () {
|
192 | var returnValue = callback.apply(this, arguments);
|
193 | self.resolve();
|
194 | return returnValue;
|
195 | });
|
196 | }
|
197 | };
|
198 | }
|
199 | });
|
200 |
|
\ | No newline at end of file |