-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_base.js
88 lines (80 loc) · 2.65 KB
/
_base.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
const memwatch = require("@airbnb/node-memwatch")
const Benchmark = require("benchmark")
const bytes = require("pretty-bytes")
const { format } = require("util")
const { Francis, Bacon, Kefir } = require("./_libs")
const MEMORY = !!process.env.SHOW_MEMORY_USAGE
let heapStats = []
let gcTime = 0
const withFrancis = f => (...args) => f(Francis, ...args)
const withBacon = f => (...args) => f(Bacon, ...args)
const withKefir = f => (...args) => f(Kefir, ...args)
const resetHeapStats = () => {
gcTime = 0
heapStats = []
}
const getAvgMemoryUsage = stats =>
stats.length ? bytes(Math.floor(stats.reduce((a, s) => a + s, 0) / stats.length)) : "unknown"
const getMaxMemoryUsage = stats =>
stats.length ? bytes(Math.floor(stats.reduce((a, s) => Math.max(a, s), 0))) : "unknown"
if (MEMORY) {
memwatch.on("stats", stats => {
heapStats.push(stats.used_heap_size)
gcTime += stats.gc_time / 1000000.0
})
}
const testCases = (label, fn, combinations = []) => {
const accepted = (testFn, args) => !testFn.accept || testFn.accept(...args)
const cases = (name, testFn) =>
combinations
.map(
args =>
accepted(testFn, args) && {
description:
`${name} ${format(label + "|||", ...args).split("|||")[0]}` +
(testFn.note ? ` (${testFn.note})` : ""),
defer: true,
fn: d => {
const done = () => d.resolve()
testFn(...args, done)
},
},
)
.filter(x => x)
return [
...(process.env.NO_FRANCIS ? [] : cases("francis", withFrancis(fn))),
...(process.env.NO_BACON ? [] : cases("bacon ", withBacon(fn))),
...(process.env.NO_KEFIR || !fn.kefir ? [] : cases("kefir ", withKefir(fn.kefir))),
]
}
const runTests = (suiteName, testCases) => {
return new Promise(resolve => {
console.log(`Running suite ${suiteName}...`)
console.log("")
const maxLen = testCases.reduce((m, t) => Math.max(t.description.length, m), 0)
resetHeapStats()
testCases
.reduce(
(suite, test) => suite.add(test.description.padEnd(maxLen + 1), test),
new Benchmark.Suite(),
)
.on("complete", function() {
if (MEMORY) {
const stats = [...heapStats]
console.log("Memory statistics:")
console.log(" > Avg mem:", getAvgMemoryUsage(stats))
console.log(" > Max mem:", getMaxMemoryUsage(stats))
console.log(" > GC time:", gcTime.toFixed(2), "ms")
} else {
this.forEach(test => console.log(test.toString()))
}
console.log("")
resolve()
})
.run({ async: true })
})
}
module.exports = {
testCases,
runTests,
}