Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit 49796bf

Browse files
committed
async setup and teardown
1 parent dd3913c commit 49796bf

File tree

4 files changed

+107
-59
lines changed

4 files changed

+107
-59
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"0x": "^2.4.0",
41+
"async": "^2.1.5",
4142
"benchmark": "^2.1.3",
4243
"ipfs": "^0.22.1",
4344
"ipfsd-ctl": "^0.20.0",

src/prepare/index.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,12 @@ const preparers = {
66
'js-http': require('./js-http')
77
}
88

9-
module.exports = function prepare (test, env) {
10-
return function (d) {
11-
const preparer = preparers[env]
12-
if (!preparer) {
13-
throw new Error('Unknown environment ' + env)
14-
}
15-
16-
console.error('preparing %s environment...', env)
17-
const shutdown = preparer((err, ipfs) => {
18-
if (err) {
19-
throw err
20-
}
21-
22-
console.error('prepared.')
23-
24-
test(ipfs, (err) => {
25-
if (err) {
26-
throw err
27-
}
28-
shutdown((_err) => {
29-
if (_err) {
30-
throw _err
31-
}
32-
d.resolve()
33-
})
34-
})
35-
})
9+
module.exports = function prepare (env, callback) {
10+
const preparer = preparers[env]
11+
if (!preparer) {
12+
callback(new Error('Unknown environment ' + env))
3613
}
14+
15+
console.error('preparing %s environment...', env)
16+
return preparer(callback)
3717
}

src/report.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mapSeries(
3030
// run suite
3131
const command = 'node ' + __dirname + ' ' + suite
3232
const child = exec(command, (err, stdout) => {
33+
console.log(stdout)
3334
if (err) {
3435
callback(err)
3536
} else {

src/runner.js

Lines changed: 98 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require('colors')
44
const mapSeries = require('async/mapSeries')
55
const Benchmark = require('benchmark')
6-
const Suite = Benchmark.Suite
76
const os = require('os')
87

98
const suites = require('./suites')
@@ -37,49 +36,116 @@ function runOne (_suite, callback) {
3736
}
3837
process.stderr.write((suite.name + ' started\n').yellow)
3938

40-
const s = Suite(suite.name)
41-
let tests = suite.tests
39+
const benchmarks = []
40+
let tests = suite.tests || suite.test
4241
if (!Array.isArray(tests)) {
4342
tests = [tests]
4443
}
4544

4645
tests.forEach((test, index) => {
4746
ENVIRONMENTS.forEach((env) => {
48-
const name = (test.name || (suite.name) + (tests.length > 1 ? '-' + (index + 1) : '')) + '-' + env
49-
s.add(name, prepare(test, env), { defer: true })
47+
let teardownFn
48+
let ipfs
49+
const name = [test.name || suite.name, tests.length > 1 && (index + 1), env].filter(Boolean).join('-')
50+
const options = {
51+
defer: true,
52+
setup: setup,
53+
teardown: teardown,
54+
}
55+
const benchmark = new Benchmark(name, wrapTest(test), options)
56+
benchmark.env = env
57+
benchmark.unwrappedFn = test
58+
benchmarks.push(benchmark)
59+
60+
function setup (deferred) {
61+
if (teardownFn) {
62+
deferred.suResolve()
63+
return // early
64+
}
65+
ipfs = null
66+
teardownFn = prepare(env, (err, _ipfs) => {
67+
if (err) {
68+
throw err
69+
}
70+
ipfs = _ipfs
71+
deferred.suResolve()
72+
})
73+
}
74+
75+
function teardown (deferred) {
76+
if (!teardownFn) {
77+
deferred.tdResolve()
78+
return // early
79+
}
80+
const tdfn = teardownFn
81+
teardownFn = null
82+
tdfn((err) => {
83+
if (err) {
84+
throw err
85+
}
86+
deferred.tdResolve()
87+
})
88+
}
89+
90+
function wrapTest (test) {
91+
return function (deferred) {
92+
if (!ipfs) {
93+
throw new Error('No ipfs')
94+
}
95+
test(ipfs, (err) => {
96+
if (err) {
97+
throw err
98+
}
99+
deferred.resolve()
100+
})
101+
}
102+
}
50103
})
51104
})
52105

53-
s.on('complete', () => {
54-
process.stderr.write(suite.name + ' finished\n')
55-
callback(null, result(s))
56-
})
106+
mapSeries(
107+
benchmarks,
108+
(benchmark, cb) => {
109+
console.error('RUNNING', benchmark.name)
110+
benchmark.on('complete', () => {
111+
cb(null, result(benchmark))
112+
})
113+
114+
benchmark.run({ async: true })
115+
},
116+
(err, results) => {
117+
if (err) {
118+
throw err
119+
}
57120

58-
s.run({ async: true })
121+
console.error('benchmarks finished\n')
122+
callback(null, {
123+
suite: suite.name,
124+
results: results
125+
})
126+
}
127+
)
59128
}
60129

61-
function result (suite) {
62-
// console.log(suite)
130+
function result (benchmark) {
131+
console.error(benchmark)
63132
return {
64-
name: suite.name,
65-
benchmarks: suite.map(benchmark => ({
66-
name: benchmark.name,
67-
suite: suite.name,
68-
code: benchmark.fn.toString(),
69-
platform: Benchmark.platform,
70-
cpus: os.cpus(),
71-
loadavg: os.loadavg(),
72-
count: benchmark.count,
73-
hz: benchmark.hz,
74-
now: Date.now(),
75-
stats: {
76-
moe: benchmark.stats.moe,
77-
rme: benchmark.stats.rme,
78-
sem: benchmark.stats.sem,
79-
deviation: benchmark.stats.deviation,
80-
mean: benchmark.stats.mean,
81-
variance: benchmark.stats.variance
82-
}
83-
}))
133+
name: benchmark.name,
134+
env: benchmark.env,
135+
code: benchmark.unwrappedFn.toString(),
136+
platform: Benchmark.platform,
137+
cpus: os.cpus(),
138+
loadavg: os.loadavg(),
139+
count: benchmark.count,
140+
hz: benchmark.hz,
141+
now: Date.now(),
142+
stats: {
143+
moe: benchmark.stats.moe,
144+
rme: benchmark.stats.rme,
145+
sem: benchmark.stats.sem,
146+
deviation: benchmark.stats.deviation,
147+
mean: benchmark.stats.mean,
148+
variance: benchmark.stats.variance
149+
}
84150
}
85151
}

0 commit comments

Comments
 (0)