-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (147 loc) · 3.87 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
/* vim: set et sw=2 ts=2: */
/* jshint node: true */
"use strict";
require('nodash').install(GLOBAL);
var chalk = require('chalk');
var sprintf = require('sprintf').sprintf;
function timeExecution(func, args) {
args = args || [];
var error = null;
var start = process.hrtime();
try {
func.apply(null, args);
} catch (e) {
return { error: e };
}
var end = process.hrtime(start);
return end[0] * 10e9 + end[1];
}
function timeExecutions(options, func, callback) {
var results = [];
var min = +Infinity;
var max = -Infinity;
var avg = 0;
var i = 0;
function nextIteration() {
i += 1;
var args = map(invoke, options.args || []);
var result = timeExecution(func, args);
if (typeof result !== 'number') {
callback(null, result.error);
return;
}
min = Math.min(min, result);
max = Math.max(max, result);
avg += (result - avg) / i;
if (options.median) {
results.push(result);
}
if (i <= options.iterations) {
if (options.delay > 0) {
setTimeout(nextIteration, options.delay);
} else {
setImmediate(nextIteration);
}
} else {
if (options.median) {
results.sort(compare);
}
callback({
min: min,
max: max,
avg: avg,
median: results[floor(results.length / 2)]
});
}
}
nextIteration();
}
function groupResults(results) {
var groupedResults = {};
each(function (result, key) {
if (isInfixOf('\0', key)) {
key = key.split('\0');
if (!groupedResults[key[0]]) {
groupedResults[key[0]] = {};
}
groupedResults[key[0]][key[1]] = result;
} else {
groupedResults[key] = { '': result };
}
}, results);
return groupedResults;
}
function timeSuite(options, suite, callback) {
var plan = {};
each(function (spec, name) {
if (isFunction(spec)) {
plan[name] = function (done) {
timeExecutions(options, spec, done);
};
} else if (isObject(spec)) {
var args = [];
for (var i = 0 ;; i += 1) {
if (!isFunction(spec[i])) {
break;
}
args[i] = spec[i];
}
var groupOptions = clone(options);
groupOptions.args = args;
each(function (spec, group) {
if (/^[0-9]+$/.test(group)) {
return;
}
plan[name + '\0' + group] = function (done) {
timeExecutions(groupOptions, spec, done);
};
}, spec);
}
}, suite);
if (options.__warmUp === true) {
setImmediate(callback);
return;
}
timeSuite({
iterations: options.warmUp || 10,
delay: 1,
__warmUp: true
}, suite, function () {
run(plan, function (results) {
callback(groupResults(results));
});
});
}
function printResults(results) {
each(function (results, group) {
var averages = values(map(select('result.avg'), results));
var best = minimum(averages);
var worst = maximum(averages);
console.log('');
console.log(chalk.gray(sprintf(
"%-22s %6s %8s %8s %8s %8s", group, "%", "AVG", "MEDIAN", "MIN", "MAX")));
each(function (result, key) {
if (result.error) {
console.log(sprintf(" %s %s",
chalk.white(key), chalk.red("(errored)")));
console.log(' ' + chalk.gray(result.error));
} else {
result = result.result;
var format = chalk.white;
if (result.avg === best) {
format = chalk.green;
} else if (result.avg === worst) {
format = chalk.yellow;
}
console.log(format(sprintf(" %-20s %6.2f %8d %8d %8d %8d",
key, result.avg / best, result.avg, result.median, result.min, result.max)));
}
}, results);
}, results);
}
module.exports = {
timeExecution: timeExecution,
timeExecutions: timeExecutions,
timeSuite: timeSuite,
printResults: printResults
};