Skip to content

Commit

Permalink
refactor(benchmark): add benchmark executor script
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Dec 9, 2024
1 parent 0ea925b commit 79ff9eb
Show file tree
Hide file tree
Showing 49 changed files with 489 additions and 570 deletions.
39 changes: 39 additions & 0 deletions packages/benchmark/src/bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import child from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';

const _dirname = path.dirname(url.fileURLToPath(import.meta.url));
const packageDirectory = path.resolve(_dirname, '..');

const distDirectory = path.join(packageDirectory, 'dist');
const srcDirectory = path.join(packageDirectory, 'src');

const benchTaskFolders = ['factories', 'methods'];

const taskPaths = benchTaskFolders
.map((folder) =>
fs
.readdirSync(path.join(distDirectory, folder), { withFileTypes: true })
.filter((dirent) => dirent.isFile())
.map((dirent) => path.join(folder, dirent.name)),
)
.flat();

console.log(`Found ${taskPaths.length} bench tasks`);
for (const taskPath of taskPaths) {
console.log(`- ${taskPath}`);
}

for (const taskPath of taskPaths) {
const jsFilePath = path.join(distDirectory, taskPath);
const logFileName = path.format({
...path.parse(taskPath),
base: undefined,
ext: '.log',
});
const logFilePath = path.join(srcDirectory, logFileName);

console.log(`node ${jsFilePath} > ${logFilePath}`);
child.execSync(`node ${jsFilePath} > ${logFilePath}`);
}
33 changes: 33 additions & 0 deletions packages/benchmark/src/factories/Err.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
> rustresult Err:
RustlikeResult {
_type: 'err',
_value: undefined,
_error: 'error message'
}

> neverthrow err:
Err { error: 'error message' }

> effect Effect.fail:
{
_id: 'Exit',
_tag: 'Failure',
cause: { _id: 'Cause', _tag: 'Fail', failure: 'error message' }
}

> effect Exit.fail:
{
_id: 'Exit',
_tag: 'Failure',
cause: { _id: 'Cause', _tag: 'Fail', failure: 'error message' }
}

Loop N: 100,000
┌─────────┬──────────────────────┬──────────────────────┬──────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼──────────────────────┼──────────────────────┼──────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Err' │ '396911.70 ± 0.56%' │ '373600.01' │ '2566 ± 0.50%' │ '2677' │ 2520 │
│ 1 │ 'neverthrow err' │ '272838.43 ± 0.53%' │ '252300.02' │ '3751 ± 0.46%' │ '3964' │ 3666 │
│ 2 │ 'effect Effect.fail' │ '1199478.54 ± 0.99%' │ '1157300.00' │ '845 ± 0.68%' │ '864' │ 834 │
│ 3 │ 'effect Exit.fail' │ '1149108.72 ± 0.57%' │ '1125799.89' │ '876 ± 0.52%' │ '888' │ 871 │
└─────────┴──────────────────────┴──────────────────────┴──────────────┴────────────────┴───────────────┴─────────┘
38 changes: 0 additions & 38 deletions packages/benchmark/src/factories/Err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,3 @@ bench
});
await bench.run();
console.table(bench.table(formatTinybenchTask));

/*
> rustresult Err:
RustlikeResult {
_type: 'err',
_value: undefined,
_error: 'error message'
}
> neverthrow err:
Err { error: 'error message' }
> effect Effect.fail:
{
_id: 'Exit',
_tag: 'Failure',
cause: { _id: 'Cause', _tag: 'Fail', failure: 'error message' }
}
> effect Exit.fail:
{
_id: 'Exit',
_tag: 'Failure',
cause: { _id: 'Cause', _tag: 'Fail', failure: 'error message' }
}
Loop N: 100,000
┌─────────┬──────────────────────┬──────────────────────┬──────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼──────────────────────┼──────────────────────┼──────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Err' │ '393274.20 ± 0.53%' │ '371400.00' │ '2585 ± 0.48%' │ '2693' │ 2543 │
│ 1 │ 'neverthrow err' │ '272867.02 ± 0.89%' │ '248600.01' │ '3781 ± 0.49%' │ '4023' │ 3666 │
│ 2 │ 'effect Effect.fail' │ '1162188.39 ± 0.46%' │ '1148000.00' │ '864 ± 0.44%' │ '871' │ 861 │
│ 3 │ 'effect Exit.fail' │ '1127193.58 ± 0.47%' │ '1110300.00' │ '891 ± 0.44%' │ '901' │ 888 │
└─────────┴──────────────────────┴──────────────────────┴──────────────┴────────────────┴───────────────┴─────────┘
*/
21 changes: 21 additions & 0 deletions packages/benchmark/src/factories/Ok.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
> rustresult Ok:
RustlikeResult { _type: 'ok', _value: 1, _error: undefined }

> neverthrow ok:
Ok { value: 1 }

> effect Effect.succeed:
{ _id: 'Exit', _tag: 'Success', value: 1 }

> effect Exit.succeed:
{ _id: 'Exit', _tag: 'Success', value: 1 }

Loop N: 100,000
┌─────────┬─────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Ok' │ '405076.67 ± 0.54%' │ '381500.01' │ '2511 ± 0.49%' │ '2621' │ 2469 │
│ 1 │ 'neverthrow ok' │ '273384.09 ± 0.56%' │ '253700.02' │ '3752 ± 0.48%' │ '3942' │ 3658 │
│ 2 │ 'effect Effect.succeed' │ '698827.05 ± 0.52%' │ '700500.01' │ '1445 ± 0.49%' │ '1428' │ 1431 │
│ 3 │ 'effect Exit.succeed' │ '694094.11 ± 0.52%' │ '696500.06' │ '1454 ± 0.49%' │ '1436' │ 1441 │
└─────────┴─────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘
26 changes: 0 additions & 26 deletions packages/benchmark/src/factories/Ok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,3 @@ bench
});
await bench.run();
console.table(bench.table(formatTinybenchTask));

/*
> rustresult Ok:
RustlikeResult { _type: 'ok', _value: 1, _error: undefined }
> neverthrow ok:
Ok { value: 1 }
> effect Effect.succeed:
{ _id: 'Exit', _tag: 'Success', value: 1 }
> effect Exit.succeed:
{ _id: 'Exit', _tag: 'Success', value: 1 }
Loop N: 100,000
┌─────────┬─────────────────────────┬─────────────────────┬──────────────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────┼─────────────────────┼──────────────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Ok' │ '395020.66 ± 0.49%' │ '371800.01' │ '2568 ± 0.45%' │ '2690' │ 2532 │
│ 1 │ 'neverthrow ok' │ '269472.49 ± 0.52%' │ '248399.97' │ '3795 ± 0.44%' │ '4026' │ 3711 │
│ 2 │ 'effect Effect.succeed' │ '684643.12 ± 0.47%' │ '690699.99' │ '1472 ± 0.45%' │ '1448' │ 1461 │
│ 3 │ 'effect Exit.succeed' │ '678484.60 ± 0.45%' │ '684549.99 ± 150.02' │ '1485 ± 0.43%' │ '1461' │ 1474 │
└─────────┴─────────────────────────┴─────────────────────┴──────────────────────┴────────────────┴───────────────┴─────────┘
*/
20 changes: 20 additions & 0 deletions packages/benchmark/src/methods/and.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
> rustresult Result.and:
[
RustlikeResult { _type: 'ok', _value: 100, _error: undefined },
RustlikeResult {
_type: 'err',
_value: undefined,
_error: 'error message'
}
]

> neverthrow Result.and sim:
[ Ok { value: 100 }, Err { error: 'error message' } ]

Loop N: 100,000
┌─────────┬─────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Result.and' │ '291635.03 ± 0.36%' │ '277100.09' │ '3459 ± 0.29%' │ '3609' │ 3429 │
│ 1 │ 'neverthrow Result.and sim' │ '194534.53 ± 0.30%' │ '202000.14' │ '5209 ± 0.33%' │ '4950' │ 5141 │
└─────────┴─────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘
25 changes: 0 additions & 25 deletions packages/benchmark/src/methods/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,3 @@ bench
});
await bench.run();
console.table(bench.table(formatTinybenchTask));

/*
> rustresult Result.and:
[
RustlikeResult { _type: 'ok', _value: 100, _error: undefined },
RustlikeResult {
_type: 'err',
_value: undefined,
_error: 'error message'
}
]
> neverthrow Result.and sim:
[ Ok { value: 100 }, Err { error: 'error message' } ]
Loop N: 100,000
┌─────────┬─────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Result.and' │ '288184.39 ± 0.34%' │ '273200.04' │ '3499 ± 0.29%' │ '3660' │ 3471 │
│ 1 │ 'neverthrow Result.and sim' │ '192495.82 ± 0.31%' │ '199799.95' │ '5268 ± 0.34%' │ '5005' │ 5195 │
└─────────┴─────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘
*/
20 changes: 20 additions & 0 deletions packages/benchmark/src/methods/andThen.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
> rustresult Result.andThen:
[
RustlikeResult { _type: 'ok', _value: 2, _error: undefined },
RustlikeResult {
_type: 'err',
_value: undefined,
_error: 'error message'
}
]

> neverthrow Result.andThen:
[ Ok { value: 2 }, Err { error: 'error message' } ]

Loop N: 100,000
┌─────────┬─────────────────────────────┬─────────────────────┬─────────────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────────┼─────────────────────┼─────────────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Result.andThen' │ '688856.68 ± 0.14%' │ '682300.09' │ '1453 ± 0.12%' │ '1466' │ 1452 │
│ 1 │ 'neverthrow Result.andThen' │ '460866.04 ± 0.34%' │ '446949.96 ± 49.83' │ '2180 ± 0.27%' │ '2237' │ 2170 │
└─────────┴─────────────────────────────┴─────────────────────┴─────────────────────┴────────────────┴───────────────┴─────────┘
25 changes: 0 additions & 25 deletions packages/benchmark/src/methods/andThen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,3 @@ bench
});
await bench.run();
console.table(bench.table(formatTinybenchTask));

/*
> rustresult Result.andThen:
[
RustlikeResult { _type: 'ok', _value: 2, _error: undefined },
RustlikeResult {
_type: 'err',
_value: undefined,
_error: 'error message'
}
]
> neverthrow Result.andThen:
[ Ok { value: 2 }, Err { error: 'error message' } ]
Loop N: 100,000
┌─────────┬─────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Result.andThen' │ '685829.54 ± 0.14%' │ '679399.97' │ '1459 ± 0.12%' │ '1472' │ 1459 │
│ 1 │ 'neverthrow Result.andThen' │ '452314.34 ± 0.33%' │ '437399.98' │ '2221 ± 0.25%' │ '2286' │ 2211 │
└─────────┴─────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘
*/
17 changes: 17 additions & 0 deletions packages/benchmark/src/methods/err.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
> rustresult Result.err:
[ undefined, 'error message' ]

> neverthrow Result.err sim:
[ undefined, 'error message' ]

> effect Exit.err sim:
[ undefined, 'error message' ]

Loop N: 100,000
┌─────────┬─────────────────────────────┬──────────────────────┬───────────────────────┬─────────────────┬───────────────┬─────────┐
│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │
├─────────┼─────────────────────────────┼──────────────────────┼───────────────────────┼─────────────────┼───────────────┼─────────┤
│ 0 │ 'rustresult Result.err' │ '24746.12 ± 0.09%' │ '24499.89' │ '40601 ± 0.05%' │ '40817' │ 40411 │
│ 1 │ 'neverthrow Result.err sim' │ '73818.62 ± 0.04%' │ '73799.85' │ '13552 ± 0.03%' │ '13550' │ 13547 │
│ 2 │ 'effect Exit.err sim' │ '1941183.71 ± 0.67%' │ '1904399.99 ± 100.02' │ '518 ± 0.61%' │ '525' │ 516 │
└─────────┴─────────────────────────────┴──────────────────────┴───────────────────────┴─────────────────┴───────────────┴─────────┘
Loading

0 comments on commit 79ff9eb

Please sign in to comment.