This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·71 lines (60 loc) · 2.5 KB
/
cli.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
#!/usr/bin/env node
const path = require('path')
const { existsSync, statSync } = require('fs')
const execBenchmarkPath = path.join(__dirname, 'exec-benchmark.js')
const reporter = require('./reporter')
// program cli
const { program } = require('commander')
const { version } = require('../package.json')
program.version(version)
program
// .option('-i, --ipfs <go or js>', 'ipfs implementation for orbitdb', 'js')
.option('-b, --benchmarks <path>', 'benchmark folder or file', './benchmarks')
.option('-o, --output <file path>', 'report output path (.html or .json)')
.option('-i, --baselines <path>', 'baselines to use for comparison (.json output)')
.option('--no-node', 'skip nodejs benchmarks')
.option('--no-browser', 'skip browser benchmarks')
program.parse()
let { benchmarks, output, node, browser, baselines } = program.opts()
benchmarks = path.resolve(benchmarks)
if (output) output = path.resolve(output)
if (baselines) baselines = path.resolve(baselines)
if (!existsSync(benchmarks)) throw new Error(`benchmarks path does not exist: ${benchmarks}`)
if (baselines && !existsSync(baselines)) {
throw new Error(`baselines path does not exist: ${baselines}`)
}
const { mkdtempSync } = require('fs')
const os = require('os')
const tmpdir = mkdtempSync(path.join(os.tmpdir(), 'odb-benchmark-runner_'))
// get benchmark file paths
const { execSync } = require('child_process')
const benchmarkPaths = statSync(benchmarks).isDirectory()
? execSync('ls -1 *.benchmark.js', { cwd: benchmarks })
.toString()
.split('\n')
.filter(a => a)
.map(p => path.join(benchmarks, p))
: [benchmarks]
// benchmarker server, collects logs and results
const benchmarkerServer = require('./benchmarker/server.js').create()
const { Worker } = require('worker_threads')
async function execBenchmarks ({ browser } = {}) {
const host = `127.0.0.1:${benchmarkerServer.address().port}`
const env = browser ? 'browser' : 'node'
console.log(`running ${env} benchmark/s`)
for (const p of benchmarkPaths) {
const data = { host, file: p, browser, dir: tmpdir }
const worker = new Worker(execBenchmarkPath, { workerData: data })
await new Promise((resolve, reject) => worker.on('exit', resolve))
}
console.log(`${env} benchmark/s complete`)
}
async function main () {
// run benchmarks
if (node) await execBenchmarks()
if (browser) await execBenchmarks({ browser })
// write report
const results = benchmarkerServer.results
await reporter(output, results, baselines)
}
main().then(() => process.exit(0))