Skip to content

Commit

Permalink
test: add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 3, 2023
1 parent 379f182 commit 61bdb99
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ napi = { version = "2", features = ["anyhow", "napi6"] }
napi-derive = "2"
tar = "0.4"

[target.'cfg(not(target_os = "linux"))'.dependencies]
mimalloc = "0.1"

[target.'cfg(target_os = "linux")'.dependencies]
mimalloc = { version = "0.1", features = ["local_dynamic_tls"] }

[build-dependencies]
napi-build = "2"

Expand Down
30 changes: 21 additions & 9 deletions benchmark/bench.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { readFileSync } from 'node:fs'
import { join } from 'node:path'

import b from 'benny'
import { list } from 'tar'

import { plus100 } from '../index'
import { Archive } from '../index'

function add(a: number) {
return a + 100
}
const ARCHIVE_PATH = join(__dirname, '..', '__test__', 'src.tar')

async function run() {
await b.suite(
'Add 100',
'Read all entries',

b.add('Native a + 100', () => {
plus100(10)
b.add('@napi-rs/tar', () => {
const archiveBuffer = readFileSync(ARCHIVE_PATH)
const archive = new Archive(archiveBuffer)
for (const entry of archive.entries()) {
entry.path()
}
}),

b.add('JavaScript a + 100', () => {
add(10)
b.add('node-tar', () => {
list({
file: join(__dirname, '..', '__test__', 'src.tar'),
onentry: (entry) => {
entry.path
},
sync: true,
})
}),

b.cycle(),
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@swc/core": "^1.3.95",
"@taplo/cli": "^0.5.2",
"@types/node": "^20.10.2",
"@types/tar": "^6",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"ava": "^5.3.1",
Expand All @@ -75,6 +76,7 @@
"lint-staged": "^15.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"tar": "^6.2.0",
"typescript": "^5.2.2"
},
"lint-staged": {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
io::{Cursor, Read},
};

use mimalloc::MiMalloc;
use napi::{
bindgen_prelude::{Env, Reference},
Either, JsBuffer,
Expand All @@ -16,6 +17,9 @@ use crate::entry::Entries;
mod entry;
mod header;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

pub struct ArchiveSource {
inner: Either<File, Cursor<Vec<u8>>>,
}
Expand Down
23 changes: 21 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ __metadata:
"@swc/core": "npm:^1.3.95"
"@taplo/cli": "npm:^0.5.2"
"@types/node": "npm:^20.10.2"
"@types/tar": "npm:^6"
"@typescript-eslint/eslint-plugin": "npm:^6.9.1"
"@typescript-eslint/parser": "npm:^6.9.1"
ava: "npm:^5.3.1"
Expand All @@ -168,6 +169,7 @@ __metadata:
lint-staged: "npm:^15.0.2"
npm-run-all: "npm:^4.1.5"
prettier: "npm:^3.0.3"
tar: "npm:^6.2.0"
typescript: "npm:^5.2.2"
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -431,7 +433,7 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^20.10.2":
"@types/node@npm:*, @types/node@npm:^20.10.2":
version: 20.10.2
resolution: "@types/node@npm:20.10.2"
dependencies:
Expand All @@ -447,6 +449,16 @@ __metadata:
languageName: node
linkType: hard

"@types/tar@npm:^6":
version: 6.1.10
resolution: "@types/tar@npm:6.1.10"
dependencies:
"@types/node": "npm:*"
minipass: "npm:^4.0.0"
checksum: 76290848f9392ef0f27bcd7118565b39b60a43b3f51a17e1380f25c5a4c21f18c3361885240f13a15fe4f14894f055ee00e47a3369354bdb010ce7da445bb001
languageName: node
linkType: hard

"@typescript-eslint/eslint-plugin@npm:^6.9.1":
version: 6.9.1
resolution: "@typescript-eslint/eslint-plugin@npm:6.9.1"
Expand Down Expand Up @@ -3253,6 +3265,13 @@ __metadata:
languageName: node
linkType: hard

"minipass@npm:^4.0.0":
version: 4.2.8
resolution: "minipass@npm:4.2.8"
checksum: 4ea76b030d97079f4429d6e8a8affd90baf1b6a1898977c8ccce4701c5a2ba2792e033abc6709373f25c2c4d4d95440d9d5e9464b46b7b76ca44d2ce26d939ce
languageName: node
linkType: hard

"minipass@npm:^5.0.0":
version: 5.0.0
resolution: "minipass@npm:5.0.0"
Expand Down Expand Up @@ -4462,7 +4481,7 @@ __metadata:
languageName: node
linkType: hard

"tar@npm:^6.1.11, tar@npm:^6.1.2":
"tar@npm:^6.1.11, tar@npm:^6.1.2, tar@npm:^6.2.0":
version: 6.2.0
resolution: "tar@npm:6.2.0"
dependencies:
Expand Down

0 comments on commit 61bdb99

Please sign in to comment.