Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix benchmarks: replace nanobench with tinybench #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions benchmark/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import './bencode.js'
import './buffer-vs-string.js'
import './compare-decode.js'
import './compare-encode.js'
import './encoding-length.js'
28 changes: 19 additions & 9 deletions benchmark/bencode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import fs from 'fs'
import path from 'path'
import bench from 'nanobench'
import { fileURLToPath } from 'url'
import { Bench } from 'tinybench'

import bencode from '../index.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
const object = bencode.decode(buffer)
const objectUtf8 = bencode.decode(buffer, 'utf8')
Expand All @@ -12,7 +16,9 @@ const objectBinary = bencode.decode(buffer, 'binary')

const ITERATIONS = 10000

bench(`bencode.encode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
const bench = new Bench({ time: 100 })

bench.add(`bencode.encode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -24,7 +30,7 @@ bench(`bencode.encode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.encode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.encode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -36,7 +42,7 @@ bench(`bencode.encode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.encode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.encode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -48,7 +54,7 @@ bench(`bencode.encode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.encode() [binary] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.encode() [binary] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -60,7 +66,7 @@ bench(`bencode.encode() [binary] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.decode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.decode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -72,7 +78,7 @@ bench(`bencode.decode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.decode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.decode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -84,7 +90,7 @@ bench(`bencode.decode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.decode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.decode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -96,7 +102,7 @@ bench(`bencode.decode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencode.decode() [binary] ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencode.decode() [binary] ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -107,3 +113,7 @@ bench(`bencode.decode() [binary] ⨉ ${ITERATIONS}`, function (run) {

return result
})

await bench.run()

console.table(bench.table())
17 changes: 14 additions & 3 deletions benchmark/buffer-vs-string.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import bencode from '../index.js'
import bench from 'nanobench'

import { Bench } from 'tinybench'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
const str = buffer.toString('ascii')

const ITERATIONS = 10000

bench(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
const bench = new Bench({ time: 100 })

bench.add(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -20,7 +27,7 @@ bench(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`decode string ⨉ ${ITERATIONS}`, function (run) {
bench.add(`decode string ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -31,3 +38,7 @@ bench(`decode string ⨉ ${ITERATIONS}`, function (run) {

return result
})

await bench.run()

console.table(bench.table())
27 changes: 19 additions & 8 deletions benchmark/compare-decode.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import fs from 'fs'
import path from 'path'
import bench from 'nanobench'
import { fileURLToPath } from 'url'
import { Bench } from 'tinybench'

import bencode from '../index.js'

import bencoding from 'bencoding'
import bncode from 'bncode'
import btparse from 'btparse'
import dht from 'dht.js/lib/dht/bencode'
import dht from 'dht.js/lib/dht/bencode.js'
import dhtBencode from 'dht-bencode'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))

const ITERATIONS = 10000

bench(`bencode.decode() ⨉ ${ITERATIONS}`, function (run) {
const bench = new Bench({ time: 100 })

bench.add(`bencode.decode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -25,7 +32,7 @@ bench(`bencode.decode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencoding.decode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencoding.decode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -37,7 +44,7 @@ bench(`bencoding.decode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bncode.decode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bncode.decode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -49,7 +56,7 @@ bench(`bncode.decode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`btparse() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`btparse() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -61,7 +68,7 @@ bench(`btparse() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`dht.decode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`dht.decode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -73,7 +80,7 @@ bench(`dht.decode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`dhtBencode.decode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`dhtBencode.decode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -84,3 +91,7 @@ bench(`dhtBencode.decode() ⨉ ${ITERATIONS}`, function (run) {

return result
})

await bench.run()

console.table(bench.table())
25 changes: 18 additions & 7 deletions benchmark/compare-encode.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import fs from 'fs'
import path from 'path'
import bench from 'nanobench'
import { fileURLToPath } from 'url'
import { Bench } from 'tinybench'

import bencode from '../index.js'

import bencoding from 'bencoding'
import bncode from 'bncode'
import dht from 'dht.js/lib/dht/bencode'
import dht from 'dht.js/lib/dht/bencode.js'
import dhtBencode from 'dht-bencode'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
const object = bencode.decode(buffer)

const ITERATIONS = 10000

bench(`bencode.encode() ⨉ ${ITERATIONS}`, function (run) {
const bench = new Bench({ time: 100 })

bench.add(`bencode.encode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -25,7 +32,7 @@ bench(`bencode.encode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bencoding.encode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bencoding.encode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -37,7 +44,7 @@ bench(`bencoding.encode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`bncode.encode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`bncode.encode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -49,7 +56,7 @@ bench(`bncode.encode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`dht.encode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`dht.encode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -61,7 +68,7 @@ bench(`dht.encode() ⨉ ${ITERATIONS}`, function (run) {
return result
})

bench(`dhtBencode.encode() ⨉ ${ITERATIONS}`, function (run) {
bench.add(`dhtBencode.encode() ⨉ ${ITERATIONS}`, function (run) {
let result = null

run.start()
Expand All @@ -72,3 +79,7 @@ bench(`dhtBencode.encode() ⨉ ${ITERATIONS}`, function (run) {

return result
})

await bench.run()

console.table(bench.table())
25 changes: 18 additions & 7 deletions benchmark/encoding-length.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import bencode from '../index.js'
import bench from 'nanobench'

import { Bench } from 'tinybench'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
const torrent = bencode.decode(buffer)

const ITERATIONS = 10000

bench('bencode.encodingLength(torrent)', function (run) {
const bench = new Bench({ time: 100 })

bench.add('bencode.encodingLength(torrent)', function (run) {
const result = null

run.start()
Expand All @@ -20,7 +27,7 @@ bench('bencode.encodingLength(torrent)', function (run) {
return result
})

bench('bencode.encodingLength(buffer)', function (run) {
bench.add('bencode.encodingLength(buffer)', function (run) {
const result = null

run.start()
Expand All @@ -32,7 +39,7 @@ bench('bencode.encodingLength(buffer)', function (run) {
return result
})

bench('bencode.encodingLength(string)', function (run) {
bench.add('bencode.encodingLength(string)', function (run) {
const result = null

run.start()
Expand All @@ -44,7 +51,7 @@ bench('bencode.encodingLength(string)', function (run) {
return result
})

bench('bencode.encodingLength(number)', function (run) {
bench.add('bencode.encodingLength(number)', function (run) {
const result = null

run.start()
Expand All @@ -56,7 +63,7 @@ bench('bencode.encodingLength(number)', function (run) {
return result
})

bench('bencode.encodingLength(array<number>)', function (run) {
bench.add('bencode.encodingLength(array<number>)', function (run) {
const result = null

run.start()
Expand All @@ -68,7 +75,7 @@ bench('bencode.encodingLength(array<number>)', function (run) {
return result
})

bench('bencode.encodingLength(small object)', function (run) {
bench.add('bencode.encodingLength(small object)', function (run) {
const result = null

run.start()
Expand All @@ -79,3 +86,7 @@ bench('bencode.encodingLength(small object)', function (run) {

return result
})

await bench.run()

console.table(bench.table())
Loading