This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
273 lines (232 loc) · 9.47 KB
/
index.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* eslint no-extra-parens: 0, max-params: 0, max-statements: 0, no-magic-numbers: 0,
prefer-template: 0 */
"use strict";
const Q = require("q");
const prettyMs = require("pretty-ms");
const _ = require("lodash");
const clc = require("cli-color");
const timeline = [];
const hasMarker = (ev, markerName) =>
ev.markers.find((marker) => marker.name === markerName);
const firstMarker = (ev) => ev.markers[0];
const lastMarker = (ev) => ev.markers[ev.markers.length - 1];
const diffMarkerTimes = (startMarker, endMarker) => {
if (startMarker && endMarker) {
return endMarker.t - startMarker.t;
} else {
return 0;
}
};
const diffMarkers = (ev, startName, endName, alternateEndName) => {
startName = startName ? startName : "start";
endName = endName ? endName : "end";
const startMarker = ev.markers.find((marker) => marker.name === startName);
const endMarker = ev.markers.find((marker) =>
(alternateEndName && marker.name === alternateEndName) || marker.name === endName
);
return diffMarkerTimes(startMarker, endMarker);
};
class Reporter {
constructor(opts) {
this.options = {
console
};
/* istanbul ignore else */
if (opts && opts.console) {
this.options.console = opts.console;
}
}
initialize(magellanGlobals) {
const analytics = magellanGlobals.analytics;
const deferred = Q.defer();
deferred.resolve();
analytics.sync().forEach((message) => {
this._handleGlobalMessage(message);
});
// listen to global emitter
analytics.getEmitter().addListener("message", this._handleGlobalMessage.bind(this));
return deferred.promise;
}
// listen to a testRun's events on event emitter source.
listenTo(testRun, test, source) {
if (test && testRun) {
// Every time a message is received regarding this test, we also get the test object
// itself so that we're able to reason about retries, worker index, etc.
source.addListener("message", this._handleTestRunMessage.bind(this, testRun, test));
} else {
source.addListener("message", this._handleGlobalMessage.bind(this));
}
}
//
// Timeline marker: A timeline marker pertaining to a previously-received analytics event.
// Data structure for a timeline marker
// {
// name: string marker name (eg: "failed", "passed", "end")
// t: number timestamp
// }
//
// handle a message from a test
_handleTestRunMessage(testRun, test, message) {
if (message && message.type && message.data) {
if (message.type === "analytics-event") {
timeline.push(message.data);
} else if (message.type === "analytics-event-mark" && message.data) {
// Find a previously-received event in our timeline and amend it with this marker.
for (let i = timeline.length - 1; i >= 0; i--) {
if (timeline[i].name === message.eventName) {
timeline[i].markers.push(message.data);
break;
}
}
}
}
}
// handle a message from a global source
_handleGlobalMessage(message) {
if (message && message.type && message.data) {
if (message.type === "analytics-event") {
timeline.push(message.data);
} else if (message.type === "analytics-event-mark" && message.data) {
// Find a previously-received event in our timeline and amend it with this marker.
for (let i = timeline.length - 1; i >= 0; i--) {
if (timeline[i].name === message.eventName) {
timeline[i].markers.push(message.data);
break;
}
}
}
}
}
flush() {
let numFailedTests = 0;
let numPassedTests = 0;
let numRetries = 0;
const magellanRun = _.find(timeline, (item) => item.name === "magellan-run");
const magellanTime = diffMarkers(magellanRun, "start", "passed", "failed");
const testRuns = _.filter(timeline, (item) => _.startsWith(item.name, "test-run-"));
const notTestRuns = _.filter(timeline, (item) => !_.startsWith(item.name, "test-run-"));
const timeSpentPassing = _.reduce(testRuns, (result, testRun) => {
const startMarker = testRun.markers.find((marker) => marker.name === "start");
const endMarker = testRun.markers.find((marker) => marker.name === "passed");
if (startMarker && endMarker) {
numPassedTests++;
return result + endMarker.t - startMarker.t;
} else {
return result;
}
}, 0);
const timeSpentRetrying = _.reduce(testRuns, (result, testRun) => {
const startMarker = testRun.markers.find((marker) => marker.name === "start");
const endMarker = testRun.markers.find((marker) =>
marker.name === "passed" || marker.name === "failed"
);
if (startMarker && endMarker && testRun.metadata.attemptNumber > 1) {
numRetries++;
return result + endMarker.t - startMarker.t;
} else {
return result;
}
}, 0);
const timeSpentFailing = _.reduce(testRuns, (result, testRun) => {
const startMarker = testRun.markers.find((marker) => marker.name === "start");
const endMarker = testRun.markers.find((marker) => marker.name === "failed");
if (startMarker && endMarker) {
numFailedTests++;
return result + endMarker.t - startMarker.t;
} else {
return result;
}
}, 0);
const slowestFailingTest = _.chain(testRuns)
.filter((testRun) => hasMarker(testRun, "failed"))
.maxBy((testRun) => diffMarkers(testRun, "start", "failed"))
.value();
const slowestPassingTest = _.chain(testRuns)
.filter((testRun) => hasMarker(testRun, "passed"))
.maxBy(testRuns, (testRun) => diffMarkers(testRun, "start", "passed"))
.value();
this.options.console.log(clc.greenBright("\n============= Runtime Stats ==============\n"));
this.options.console.log("");
this.options.console.log(` # Test runs: ${testRuns.length}`);
this.options.console.log(` # Passed test runs: ${numPassedTests}`);
this.options.console.log(` # Failed test runs: ${numFailedTests}`);
this.options.console.log(` # Re-attempt runs: ${numRetries}`);
this.options.console.log("");
this.options.console.log(" Human time: " +
prettyMs(timeSpentFailing + timeSpentPassing));
this.options.console.log(" Magellan time: " + prettyMs(magellanTime));
if (magellanTime > 0) {
this.options.console.log("Human-to-Magellan multiplier: " +
((timeSpentFailing + timeSpentPassing) / magellanTime).toFixed(2) + "X");
} else {
this.options.console.log("Human-to-Magellan multiplier: N/A");
}
this.options.console.log(` Human time spent passing: ${prettyMs(timeSpentPassing)}`);
this.options.console.log(` Human time spent failing: ${prettyMs(timeSpentFailing)}`);
this.options.console.log("");
if (numRetries > 0 && magellanTime > 0) {
this.options.console.log(` Human time retrying: ${prettyMs(timeSpentRetrying)}`);
this.options.console.log("Retrying as % of total human: " +
(timeSpentRetrying / (timeSpentFailing + timeSpentPassing)).toFixed(1) + "%");
}
/* istanbul ignore else */
if (testRuns.length > 0) {
this.options.console.log(" Average test run time: " +
prettyMs((timeSpentFailing + timeSpentPassing) / testRuns.length));
} else {
this.options.console.log(" Average test run time: N/A");
}
/* istanbul ignore else */
if (numFailedTests > 0) {
this.options.console.log("Average failed test run time: " +
prettyMs(timeSpentFailing / numFailedTests));
} else {
this.options.console.log("Average failed test run time: N/A");
}
/* istanbul ignore else */
if (numPassedTests > 0) {
this.options.console.log("Average passed test run time: " +
prettyMs(timeSpentPassing / numPassedTests));
} else {
this.options.console.log("Average passed test run time: N/A");
}
/* istanbul ignore else */
if (slowestPassingTest) {
this.options.console.log("");
this.options.console.log("Slowest passing test:");
this.options.console.log(" test: \"" + slowestPassingTest.metadata.test +
"\" @: " + slowestPassingTest.metadata.profile + " ");
this.options.console.log(" attempt #: " + slowestPassingTest.metadata.attemptNumber);
}
/* istanbul ignore else */
if (slowestFailingTest) {
this.options.console.log("");
this.options.console.log(" test: \"" + slowestFailingTest.metadata.test +
"\" @: " + slowestFailingTest.metadata.profile + " ");
this.options.console.log(" attempt #: " + slowestFailingTest.metadata.attemptNumber);
}
/* istanbul ignore else */
if (notTestRuns.length > 0) {
const metrics = _.filter(notTestRuns, (metric) =>
metric.markers && metric.markers.length === 2
);
/* istanbul ignore else */
if (metrics.length) {
this.options.console.log("");
this.options.console.log("Other timing metrics: ");
metrics.forEach((metric) => {
const start = firstMarker(metric);
const end = lastMarker(metric);
const time = diffMarkerTimes(start, end);
this.options.console.log(" " + metric.name + " (" + start.name + " -> " +
end.name + ") " + prettyMs(time));
});
this.options.console.log("");
}
}
this.options.console.log("");
}
}
module.exports = Reporter;
module.exports.diffMarkerTimes = diffMarkerTimes;
module.exports.diffMarkers = diffMarkers;