-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
107 lines (86 loc) · 2.36 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
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
const mutexify = require('mutexify')
const prettyHrtime = require('pretty-hrtime')
const path = require('path')
const lock = mutexify()
let one = false
let cur = null
let runs = 0
const total = [0, 0]
module.exports = global.__NANOBENCH__ ? require(global.__NANOBENCH__) : benchmark
benchmark.only = function (name, fn) {
if (one) throw new Error('Only a single "only" benchmark can be specified')
one = true
benchmark(name, fn, true)
}
benchmark.skip = function (name, fn) {}
function rawTime (hr) {
return '(' + hr[0] + ' s + ' + hr[1] + ' ns)'
}
function toMillis (hr) {
return hr[0] * 1e3 + hr[1] / 1e6
}
function benchmark (name, fn, only) {
process.nextTick(function () {
if (one && !only) return
if (runs === 0) {
console.log('NANOBENCH version 2\n> ' + command() + '\n')
}
runs++
lock(function (release) {
console.log('# ' + name)
const b = cur = {}
let begin = process.hrtime()
b.start = function () {
begin = process.hrtime()
}
b.error = function (err) {
cur = null
console.log('fail ' + err.message + '\n')
release()
}
b.log = function (msg) {
console.log('# ' + msg)
}
b.elapsed = function () {
return toMillis(process.hrtime(begin))
}
b.end = function (msg) {
if (msg) b.log(msg)
cur = null
const elapsed = process.hrtime(begin)
total[0] += elapsed[0]
total[1] += elapsed[1]
while (total[1] >= 1e9) {
total[1] -= 1e9
total[0]++
}
console.log('ok ~' + prettyHrtime(elapsed) + ' ' + rawTime(elapsed) + '\n')
release()
return toMillis(elapsed)
}
fn(b)
})
})
}
process.on('exit', function () {
if (cur) {
cur.error(new Error('bench was never ended'))
console.log('fail\n')
return
}
console.log('all benchmarks completed')
console.log('ok ~' + prettyHrtime(total) + ' ' + rawTime(total) + '\n')
})
function command () {
let argv = process.argv.slice(0)
if (argv[0] === '/usr/local/bin/node') argv[0] = 'node'
if (argv[1] === path.join(__dirname, 'run.js')) {
argv.shift()
argv[0] = 'nanobench'
}
argv = argv.map(function (name) {
const cwd = process.cwd() + path.sep
return name.indexOf(cwd) === 0 ? name.slice(cwd.length) : name
})
return argv.join(' ')
}