-
Notifications
You must be signed in to change notification settings - Fork 21
/
common.mjs
83 lines (70 loc) · 2.06 KB
/
common.mjs
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
import { Suite } from 'bench-node'
import { createTableHeader, H2, taskToMdTable } from './markdown.mjs'
import { platform, arch, cpus, totalmem } from 'os'
function printMdHeader(name, tableHeaderColumns = ['name', 'ops/sec', 'samples']) {
const tableHeader = createTableHeader(tableHeaderColumns)
console.log(H2(name))
console.log(tableHeader)
}
function printMarkdownResults(results) {
const cycleEvents = []
for (const r of results) {
if (process.env.CI) {
cycleEvents.push({
name: r.name,
opsSec: r.opsSec,
samples: r.iterations,
})
}
console.log(taskToMdTable(r))
}
printMarkdownMachineInfo()
printMarkdownHiddenDetailedInfo(cycleEvents)
}
function getMachineInfo() {
return {
platform: platform(),
arch: arch(),
cpus: cpus().length,
totalMemory: totalmem() / 1024 ** 3,
}
}
function printMarkdownMachineInfo() {
if (!process.env.CI) return
const { platform, arch, cpus, totalMemory } = getMachineInfo()
const machineInfo = `${platform} ${arch} | ${cpus} vCPUs | ${totalMemory.toFixed(1)}GB Mem`
const writter = process.stdout
writter.write('\n\n')
writter.write('<details>\n')
writter.write('<summary>Environment</summary>')
writter.write(`\n
* __Machine:__ ${machineInfo}
* __Run:__ ${new Date()}
* __Node:__ \`${process.version}\`
`)
writter.write('</details>')
writter.write('\n\n')
}
function printMarkdownHiddenDetailedInfo(results) {
if (!process.env.CI) return
const writter = process.stdout
// We use it to check regressions.yml
writter.write('<!--\n')
writter.write(
JSON.stringify({
environment: getMachineInfo(),
benchmarks: results,
}),
)
writter.write('-->\n')
}
Suite.prototype.runAndPrintResults = async function () {
const results = await this.run()
printMarkdownResults(results)
}
export function createBenchmarkSuite(name, { tableHeaderColumns = ['name', 'ops/sec', 'samples'] } = {}) {
const suite = new Suite({ reporter: false })
// TODO: move it to runAndPrintResults
printMdHeader(name, tableHeaderColumns)
return suite
}