Skip to content

Commit adffe64

Browse files
committed
chore: move benchmarks to file
1 parent 7ea383c commit adffe64

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

test/bench/custom/benchmarks.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable strict */
2+
import { BSON } from '../../../lib/bson.mjs';
3+
4+
const ObjectId_isValid = [
5+
function objectid_isvalid_strlen() {
6+
BSON.ObjectId.isValid('a');
7+
},
8+
function objectid_isvalid_bestcase_false() {
9+
BSON.ObjectId.isValid('g6e84ebdc96f4c0772f0cbbf');
10+
},
11+
function objectid_isvalid_worstcase_false() {
12+
BSON.ObjectId.isValid('66e84ebdc96f4c0772f0cbbg');
13+
},
14+
function objectid_isvalid_true() {
15+
BSON.ObjectId.isValid('66e84ebdc96f4c0772f0cbbf');
16+
}
17+
];
18+
19+
// Add benchmarks here:
20+
export const benchmarks = [...ObjectId_isValid];

test/bench/custom/main.mjs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import util from 'node:util';
44
import fs from 'node:fs';
55
import os from 'node:os';
66
import benchmark from 'benchmark';
7-
import { BSON } from '../../../lib/bson.mjs';
7+
import { benchmarks } from './benchmarks.mjs';
88

99
const hw = os.cpus();
1010
const ram = os.totalmem() / 1024 ** 3;
@@ -22,26 +22,9 @@ console.log(systemInfo());
2222

2323
const suite = new benchmark.Suite();
2424

25-
benchmark.options = {
26-
async: false,
27-
defer: false,
28-
initCount: 1000,
29-
minSamples: 1000
30-
};
25+
for (const bench of benchmarks) suite.add(bench.name, bench);
3126

3227
suite
33-
.add('objectid_isvalid_strlen', function objectid_isvalid_strlen() {
34-
BSON.ObjectId.isValid('a');
35-
})
36-
.add('objectid_isvalid_bestcase_false', function objectid_isvalid_bestcase_false() {
37-
BSON.ObjectId.isValid('g6e84ebdc96f4c0772f0cbbf');
38-
})
39-
.add('objectid_isvalid_worstcase_false', function objectid_isvalid_worstcase_false() {
40-
BSON.ObjectId.isValid('66e84ebdc96f4c0772f0cbbg');
41-
})
42-
.add('objectid_isvalid_true', function objectid_isvalid_true() {
43-
BSON.ObjectId.isValid('66e84ebdc96f4c0772f0cbbf');
44-
})
4528
.on('cycle', function logBenchmark(event) {
4629
console.log(String(event.target));
4730
})

test/bench/custom/readme.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ In this directory are tests for code paths not covered by our spec or granular (
44

55
## How to write your own
66

7-
In `main.mjs` call the `.add` function and pass it an underscore concatenated descriptive title.
8-
Try to fit the title into the format of: "subject area", "method or function" "test case that is being covered" (Ex. `objectid_isvalid_bestcase_false`).
9-
Copy the title to the name of the function to assist with debugging and flamegraph capturing.
7+
In `benchmarks.mjs` add a new test to an existing array or make a new array for a new subject area.
8+
Try to fit the name of the function into the format of: "subject area", "method or function" "test case that is being covered" (Ex. `objectid_isvalid_bestcase_false`).
9+
Make sure your test is added to the `benchmarks` export.
1010

1111
### Example
1212

1313
```js
14-
.add('subject_function_testcase', function subject_function_testcase() {
15-
BSON.ObjectId.isValid('g6e84ebdc96f4c0772f0cbbf');
16-
})
14+
const ObjectId_isValid = [
15+
function objectid_isvalid_strlen() {
16+
BSON.ObjectId.isValid('a');
17+
},
18+
// ...
19+
];
20+
21+
export const benchmarks = [...ObjectId_isValid];
1722
```
1823

1924
## Output

0 commit comments

Comments
 (0)