-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (53 loc) · 1.46 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
var util = require('util');
var _ = require('lodash');
var Promise = require('bluebird');
Promise.longStackTraces();
var stats = require('measured').createCollection();
var async = require('async');
var ProgressBar = require('progress');
var bar; // progress bar
function runIteration(task, done) {
var timer = stats.timer('iteration').start();
task.env._plugin.doIteration(task)
.then(function () {
timer.end();
bar.tick();
done();
});
}
function run(plugin, options) {
options = options || {};
if (!options.concurrency) options.concurrency = 2;
if (!options.iterations) options.iterations = 10;
bar = new ProgressBar('###progress [:bar] :percent :total :etas', {
complete: '=',
incomplete: ' ',
width: 60,
total: options.iterations
});
return plugin.before()
.then(function (env) {
env._plugin = plugin;
return new Promise(function (resolve, reject) {
var queue = async.queue(runIteration, options.concurrency);
queue.drain = function () {
console.timeEnd('iterations');
stats.end();
bar.update(1);
resolve(env);
};
for (var i = 0; i < options.iterations; i++) {
var task = {env: env, index: i};
plugin.generateData(task);
queue.push(task);
}
// Start timing
console.time('iterations');
});
})
.then(plugin.after);
}
process.on('exit', function () {
console.log(stats.toJSON());
});
module.exports = {run: run};