forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails.load.test.js
243 lines (185 loc) · 4.97 KB
/
sails.load.test.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
var _ = require('lodash');
var portfinder = require('portfinder');
portfinder.basePort = 2001;
var SHOW_VERBOSE_BENCHMARK_REPORT = _.any(process.argv, function(arg) {
return arg.match(/-v/);
});
describe('benchmarks', function () {
describe('sails.load()', function() {
before(setupBenchmarks);
after(reportBenchmarks);
//
// Instantiate
//
benchmark('require("sails")', function(cb) {
var Sails = require('../../lib/app');
var sails = new Sails();
return cb();
});
//
// Load
//
benchmark('sails.load [first time, no hooks]', function(cb) {
var Sails = require('../../lib/app');
var sails = new Sails();
sails.load({
log: { level: 'error' },
globals: false,
loadHooks: []
}, cb);
});
benchmark('sails.load [again, no hooks]', function(cb) {
this.expected = 25;
this.comment = 'faster b/c of require cache';
var Sails = require('../../lib/app');
var sails = new Sails();
sails.load({
log: { level: 'error' },
globals: false,
loadHooks: []
}, cb);
});
benchmark('sails.load [with moduleloader hook]', function(cb) {
this.expected = 25;
this.comment = 'faster b/c of require cache';
var Sails = require('../../lib/app');
var sails = new Sails();
sails.load({
log: { level: 'error' },
globals: false,
loadHooks: ['moduleloader']
}, cb);
});
benchmark('sails.load [all core hooks]', function(cb) {
this.expected = 3000;
var Sails = require('../../lib/app');
var sails = new Sails();
sails.load({
log: { level: 'error' },
globals: false
}, cb);
});
benchmark('sails.load [again, all core hooks]', function(cb) {
this.expected = 3000;
var Sails = require('../../lib/app');
var sails = new Sails();
sails.load({
log: { level: 'error' },
globals: false
}, cb);
});
//
// Lift
//
benchmark('sails.lift [w/ a hot require cache]', function(cb) {
this.expected = 3000;
var Sails = require('../../lib/app');
var sails = new Sails();
portfinder.getPort(function (err, port) {
if (err) throw err;
sails.lift({
log: { level: 'error' },
port: port,
globals: false
}, cb);
});
});
benchmark('sails.lift [again, w/ a hot require cache]', function(cb) {
this.expected = 3000;
var Sails = require('../../lib/app');
var sails = new Sails();
portfinder.getPort(function (err, port) {
if (err) throw err;
sails.lift({
log: { level: 'error' },
port: port,
globals: false
}, cb);
});
});
});
});
/**
* Run the specified function, capturing time elapsed.
*
* @param {[type]} description [description]
* @param {Function} fn [description]
* @return {[type]} [description]
*/
function benchmark (description, fn) {
it (description, function (cb) {
var self = this;
var startedAt = self.microtime.now();
// console.time(description);
fn.apply(this, [function _callback () {
var _result = {};
// If a `comment` or `expected` was provided, harvest it
_result.expected = self.expected;
self.expected = null;
_result.comment = self.comment;
self.comment = null;
var finishedAt = self.microtime.now();
_result.duration = finishedAt - startedAt;
_result.benchmark = description;
// console.log('finished ',_result);
self.benchmarks.push(_result);
cb.apply(Array.prototype.slice.call(arguments));
}]);
});
}
/**
* Use in mocha's `before`
*
* @this {Array} benchmarks
* @this {Object} microtime
*/
function setupBenchmarks() {
this.microtime = require('microtime');
this.benchmarks = [];
}
/**
* Use in mocha's `after`
*
* @this {Array} benchmarks
* @this {Object} microtime
*/
function reportBenchmarks () {
var _ = require('lodash');
require('colors');
var output = '\n\nBenchmark Report ::\n';
output += _.reduce(this.benchmarks, function (memo, result) {
// Convert to ms-
var ms = (result.duration / 1000.0);
// round to 0 decimal places
function _roundDecimalTo (num, numPlaces) {
return +(Math.round(num + ('e+'+numPlaces)) + ('e-'+numPlaces));
}
ms = _roundDecimalTo(ms, 2);
var expected = result.expected || 1000;
// threshold: the "failure" threshold
var threshold = result.expected;
var color =
(ms < 1*expected/10) ? 'green' :
(ms < 3*expected/10) ? 'green' :
(ms < 6*expected/10) ? 'cyan' :
(ms < threshold) ? 'yellow' :
'red';
ms += 'ms';
ms = ms[color];
// Whether to show expected ms
var showExpected = true; // ms >= threshold;
return memo + '\n ' +
(result.benchmark+'') + ' :: '.grey + ms +
// Expected ms provided, and the test took quite a while
(result.expected && showExpected ? '\n (expected '+expected+'ms' +
(result.comment ? ' --' + result.comment : '') +
')' :
// Comment provided - but no expected ms
(result.comment ? '\n (' + result.comment +')\n' : '')
).grey;
},'');
// Log output (optional)
if (SHOW_VERBOSE_BENCHMARK_REPORT) {
console.log( output );
}
}