Skip to content

Commit

Permalink
lib: replace benchmarkjs with tinybench (#75)
Browse files Browse the repository at this point in the history
* lib: use tinybench instead benchmarkjs

* fixup! lib: use tinybench instead benchmarkjs
  • Loading branch information
RafaelGSS authored May 9, 2024
1 parent 12a72a5 commit 276fa01
Show file tree
Hide file tree
Showing 38 changed files with 127 additions and 93 deletions.
3 changes: 2 additions & 1 deletion bench/add-property.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ suite
configurable: true,
})
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/array-creation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ suite
.add('Array.from', function () {
Array.from({ length: 1024 * 1024 })
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/async-function-vs-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ suite
},
defer: true,
})
.run({ async: false });

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/blob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ suite
.add('slice (0, 512)', function (deferred) {
blob1024.slice(0, 512)
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/compare-using-instanceof.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ suite
1 + 1
}
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/crypto-verify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ suite
.add(`crypto.verify('${algorithm}')`, function () {
verify(algorithm, thing, rsaPublicKey, Buffer.from(signature, 'base64'))
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/date-format-iso.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ suite
.add('fromUnixToISOString(new Date())', function () {
fromUnixToISOString(new Date())
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/date-format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ suite
.add('Format using date.get*', function () {
const date = new Date()`${date.getMonth() + 1}/${date.getUTCDate()}/${date.getUTCFullYear()}`
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/date-string-coersion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ suite
const date = new Date()
const value = date.toString()
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/deleting-properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ suite
data.y
data.z
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ suite
.add('NodeError Range', function () {
new RangeError('test')
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/function-return.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ suite
const test = new Function('test', 'return []')
const a = test()
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/includes-vs-raw-comparison.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ suite
const httpVersion = '2.0'
const exists = httpVersion === '2.0' || httpVersion === '1.0' || httpVersion === '1.1'
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/keys-vs-getownpropertynames.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ suite
}
const keys = Object.getOwnPropertyNames(object)
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/last-array-item.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ suite
.add('Length = 1_000_000 - Array[length - 1]', function () {
arr100_000_0[arr100_000_0.length - 1]
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/object-creation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ suite
function C() {}
new C()
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/optional-chain-vs-and-operator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ suite
.add('Using and operator (obj.field && obj.field.field2) (undefined)', function () {
nullObj.field && nullObj.field.field2
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/parse-int.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ suite
.add('Using + - big number (10 len)', function () {
;+'9999999999'
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/possible-undefined-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ suite
const emptyObject = Object.create({})
emptyObject.undefinedFunction?.()
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/private-property.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ suite
b.increaseSize()
b.dimension
})
.run({ async: true })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/property-access-transition.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ suite
const obj = new Klass()
console.assert(obj.d)
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/property-getter-access.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ suite
.add('DefineProperties (getter & enumerable=false & configurable=false)', function () {
const v = definePropertiesEnumerableFalseAndConfigFalseObj.test
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/property-setter-access.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,5 @@ suite
.add('DefineProperties (setter & enumerable=false & configurable=false)', function () {
definePropertiesEnumerableFalseAndConfigFalseObj.test = 'Hello'
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/replace-vs-replaceall-comparison.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ suite
const text = '1+2+3+4+5+6+7+8+9'
const replaced = text.replaceAll(/\+/g, ' ')
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/shallow-copy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ suite
{ newProp: true },
)
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/sort-map.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ suite
.add('Sort using localeCompare', function () {
new Map([...map].sort((a, b) => String(a[0]).localeCompare(b[0])))
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/spread-vs-object-assign.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ suite
.add('{ ...smallObject, ...anotherSmallObject }', function () {
const nextObject = { ...smallObject, ...anotherSmallObject }
})
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/stream-readable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ suite
}
},
})
.run()

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/stream-writable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ suite
++i
}
})
.run()

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/string-concat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ suite
.add('Using array.join', function () {
;[k, o, l].join('-')
})
.run()

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/string-endsWith.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ suite
longString.slice(-comparison2.length) === comparison2
}
)
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/string-searching.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ suite
.add('Using new RegExp.test with cached regex pattern', function () {
new RegExp(regex).test(text)
})
.run()

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/string-startsWith.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ suite
longString.slice(0, comparison2.length) === comparison2
}
)
.run({ async: false })

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/super-vs-this.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ suite
const cls = new ThisClass()
const value = cls.bar()
})
.run()

await suite.runAndPrintResults()
3 changes: 2 additions & 1 deletion bench/unix-time.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ suite
.add('Date.now()', function () {
Date.now()
})
.run({ async: false })

await suite.runAndPrintResults()
14 changes: 7 additions & 7 deletions bin.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('node:fs')
const path = require('node:path')
const os = require('node:os')
const { spawnSync } = require('node:child_process')
import { readdir } from 'node:fs'
import { join } from 'node:path'
import { platform, arch, cpus, totalmem } from 'node:os'
import { spawnSync } from 'node:child_process'

const machineInfo = `${os.platform()} ${os.arch()} | ${os.cpus().length} vCPUs | ${(os.totalmem() / (1024 ** 3)).toFixed(1)}GB Mem`
const machineInfo = `${platform()} ${arch()} | ${cpus().length} vCPUs | ${(totalmem() / (1024 ** 3)).toFixed(1)}GB Mem`

const writter = process.stdout

Expand All @@ -14,9 +14,9 @@ writter.write(`\n
* __Run:__ ${new Date()}
`)

fs.readdir(path.join(__dirname, './bench'), (_err, files) => {
readdir(join(import.meta.dirname, './bench'), (_err, files) => {
for (const file of files) {
const out = spawnSync(process.execPath, [path.join(__dirname, './bench', file)]).stdout
const out = spawnSync(process.execPath, [join(import.meta.dirname, './bench', file)]).stdout
writter.write('\n' + out.toString())
}
writter.end()
Expand Down
95 changes: 47 additions & 48 deletions common.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import Benchmark from 'benchmark'
import { createTableHeader, H2, eventToMdTable } from './markdown.mjs'
import { Bench } from 'tinybench'
import { createTableHeader, H2, taskToMdTable } from './markdown.mjs'
import { platform, arch, cpus, totalmem } from 'os'

export function installMarkdownEmitter(suite, name, tableHeaderColumns = ['name', 'ops/sec', 'samples']) {
function printMdHeader(name, tableHeaderColumns = ['name', 'ops/sec', 'samples']) {
const tableHeader = createTableHeader(tableHeaderColumns)
console.log(H2(name))
console.log(tableHeader)
}

suite
.on('start', function () {
console.log(H2(name))
console.log(tableHeader)
})
.on('cycle', function (event) {
console.log(eventToMdTable(event))
})
function printMarkdownResults(tasks) {
const cycleEvents = []
for (const task of tasks) {
if (process.env.CI) {
cycleEvents.push({
name: task.name,
opsSec: task.result.hz,
samples: task.result.samples.length,
})
}
console.log(taskToMdTable(task))
}
printMarkdownMachineInfo(cycleEvents)
printMarkdownHiddenDetailedInfo(cycleEvents)
}

function getMachineInfo() {
Expand All @@ -24,61 +33,51 @@ function getMachineInfo() {
}
}

export function installMarkdownMachineInfo(suite) {
function printMarkdownMachineInfo() {
if (!process.env.CI) return

const { platform, arch, cpus, totalMemory } = getMachineInfo()

const machineInfo = `${platform} ${arch} | ${cpus} vCPUs | ${totalMemory.toFixed(1)}GB Mem`

suite.on('complete', () => {
const writter = process.stdout
const writter = process.stdout

writter.write('\n\n')
writter.write('<details>\n')
writter.write('<summary>Environment</summary>')
writter.write(`\n
writter.write('\n\n')
writter.write('<details>\n')
writter.write('<summary>Environment</summary>')
writter.write(`\n
* __Machine:__ ${machineInfo}
* __Run:__ ${new Date()}
`)
writter.write('</details>')
writter.write('\n\n')
})
writter.write('</details>')
writter.write('\n\n')
}

export function installMarkdownHiddenDetailedInfo(suite) {
function printMarkdownHiddenDetailedInfo(cycleEvents) {
if (!process.env.CI) return

const cycleEvents = []
const writter = process.stdout

suite
.on('cycle', function (event) {
cycleEvents.push({
name: event.target.name,
opsSec: event.target.hz,
samples: event.target.cycles,
})
})
.on('complete', function () {
const writter = process.stdout
// We use it to check regressions.yml
writter.write('<!--\n')
writter.write(
JSON.stringify({
environment: getMachineInfo(),
benchmarks: cycleEvents,
}),
)
writter.write('-->\n')
}

writter.write('<!--\n')
writter.write(
JSON.stringify({
environment: getMachineInfo(),
benchmarks: cycleEvents,
}),
)
writter.write('-->\n')
})
Bench.prototype.runAndPrintResults = async function () {
await this.warmup()
await this.run()
printMarkdownResults(this.tasks)
}

export function createBenchmarkSuite(name, { tableHeaderColumns = ['name', 'ops/sec', 'samples'] } = {}) {
const suite = new Benchmark.Suite()

installMarkdownEmitter(suite, name, tableHeaderColumns)
installMarkdownMachineInfo(suite)
installMarkdownHiddenDetailedInfo(suite)

const suite = new Bench()
// TODO: move it to runAndPrintResults
printMdHeader(name, tableHeaderColumns)
return suite
}
Loading

0 comments on commit 276fa01

Please sign in to comment.