|
3 | 3 | require('colors')
|
4 | 4 | const mapSeries = require('async/mapSeries')
|
5 | 5 | const Benchmark = require('benchmark')
|
6 |
| -const Suite = Benchmark.Suite |
7 | 6 | const os = require('os')
|
8 | 7 |
|
9 | 8 | const suites = require('./suites')
|
@@ -37,49 +36,116 @@ function runOne (_suite, callback) {
|
37 | 36 | }
|
38 | 37 | process.stderr.write((suite.name + ' started\n').yellow)
|
39 | 38 |
|
40 |
| - const s = Suite(suite.name) |
41 |
| - let tests = suite.tests |
| 39 | + const benchmarks = [] |
| 40 | + let tests = suite.tests || suite.test |
42 | 41 | if (!Array.isArray(tests)) {
|
43 | 42 | tests = [tests]
|
44 | 43 | }
|
45 | 44 |
|
46 | 45 | tests.forEach((test, index) => {
|
47 | 46 | 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 | + } |
50 | 103 | })
|
51 | 104 | })
|
52 | 105 |
|
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 | + } |
57 | 120 |
|
58 |
| - s.run({ async: true }) |
| 121 | + console.error('benchmarks finished\n') |
| 122 | + callback(null, { |
| 123 | + suite: suite.name, |
| 124 | + results: results |
| 125 | + }) |
| 126 | + } |
| 127 | + ) |
59 | 128 | }
|
60 | 129 |
|
61 |
| -function result (suite) { |
62 |
| - // console.log(suite) |
| 130 | +function result (benchmark) { |
| 131 | + console.error(benchmark) |
63 | 132 | 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 | + } |
84 | 150 | }
|
85 | 151 | }
|
0 commit comments