Skip to content
Merged
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
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/bson-bench/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# bson-bench

This library provides functionality to install and run benchmarks against bson and bson-ext.
This library provides functionality to install and run benchmarks against bson.

> [!IMPORTANT]
> This library is **NOT** an official MongoDB product and is meant for internal use only
Expand Down
6 changes: 3 additions & 3 deletions packages/bson-bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"keywords": [],
"author": "The MongoDB NodeJS Team <dbx-node@mongodb.com>",
"license": "Apache-2.0",
"dependencies": {
"bson": "4.7"
},
"directories": {
"lib": "lib",
"test": "test"
},
"devDependencies": {
"bson": "4.7"
}
}
6 changes: 1 addition & 5 deletions packages/bson-bench/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as BSON from 'bson';
import { readFileSync } from 'fs';
import { join } from 'path';
import { performance } from 'perf_hooks';
Expand Down Expand Up @@ -56,10 +55,7 @@ function run(bson: BSONLib | ConstructibleBSON, config: BenchmarkSpecification)

try {
if (bson.EJSON) doc = bson.EJSON.parse(readFileSync(config.documentPath, 'utf8'));
// NOTE: The BSON version used here is bson@4. This is for compatibility with bson-ext as it is
// the only version of the js-bson library explicitly compatible with bson-ext and which does
// not result in bson-ext throwing an error when running deserialization tests.
else doc = BSON.EJSON.parse(readFileSync(config.documentPath, 'utf8'));
else throw new Error('No EJSON parser found');
} catch (cause) {
reportErrorAndQuit(new Error('Failed to read test document', { cause }));
return;
Expand Down
21 changes: 9 additions & 12 deletions packages/bson-bench/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { join } from 'path';
import { exists } from './utils';

// handle normal npm package regex
export const NPM_PACKAGE_REGEX = /(bson-ext|bson)@((\d+(\.\d+)?(\.\d+)?)|latest)/;
export const NPM_PACKAGE_REGEX = /(bson)@((\d+(\.\d+)?(\.\d+)?)|latest)/;
// handle git tags/commits
export const GIT_PACKAGE_REGEX = /(bson-ext|bson)#(.+)/;
export const GIT_PACKAGE_REGEX = /(bson)#(.+)/;
// handle local package
export const LOCAL_PACKAGE_REGEX = /(bson-ext|bson):(.+)/;
export const LOCAL_PACKAGE_REGEX = /(bson):(.+)/;

/**
* The Package class represents the bson or bson-ext package to be tested
* The Package class represents the bson package to be tested
* This package can be an npm package, a git repository or a local package
**/
export class Package {
type: 'npm' | 'git' | 'local';
// bson library to install
library: 'bson' | 'bson-ext';
library: 'bson';
computedModuleName: string;
// semver version specification
npmVersion?: string;
Expand All @@ -33,17 +33,17 @@ export class Package {
let match: RegExpExecArray | null;
if ((match = NPM_PACKAGE_REGEX.exec(libSpec))) {
this.type = 'npm';
this.library = match[1] as 'bson' | 'bson-ext';
this.library = match[1] as 'bson';
this.npmVersion = match[2];
this.computedModuleName = `${this.library}-${this.npmVersion}`;
} else if ((match = GIT_PACKAGE_REGEX.exec(libSpec))) {
this.type = 'git';
this.library = match[1] as 'bson' | 'bson-ext';
this.library = match[1] as 'bson';
this.gitCommitish = match[2];
this.computedModuleName = `${this.library}-git-${this.gitCommitish}`;
} else if ((match = LOCAL_PACKAGE_REGEX.exec(libSpec))) {
this.type = 'local';
this.library = match[1] as 'bson' | 'bson-ext';
this.library = match[1] as 'bson';

this.localPath = match[2];
const cleanedLocalPath = this.localPath.replaceAll('/', '_').replaceAll('\\', '_');
Expand Down Expand Up @@ -81,9 +81,6 @@ export class Package {
case 'bson':
source = `git://github.com/mongodb/js-bson#${this.gitCommitish}`;
break;
case 'bson-ext':
source = `git://github.com/mongodb-js/bson-ext#${this.gitCommitish}`;
break;
}
break;
case 'local':
Expand Down Expand Up @@ -130,7 +127,7 @@ export type BenchmarkSpecification = {
iterations: number;
/** Number of iterations that will be run to warm up the V8 engine */
warmup: number;
/** Specifier of the bson or bson-ext library to be used. Can be an npm package, git repository or
/** Specifier of the bson library to be used. Can be an npm package, git repository or
* local package */
library: string;
installLocation?: string;
Expand Down
9 changes: 2 additions & 7 deletions packages/bson-bench/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,8 @@ export class Task {
break;
// special cases
case 'validation':
// utf8 validation is always on for bson-ext
output['utf8Validation'] = /^bson-ext/.test(this.benchmark.library)
? 1
: // if value of boolean for js-bson, convert to number, otherwise assume true
typeof o[key]['utf8'] === 'boolean'
? Number(o[key]['utf8'])
: 1;
output.utf8Validation =
typeof o[key]['utf8'] === 'boolean' ? Number(o[key]['utf8']) : 1;
break;
default:
output[key] =
Expand Down
6 changes: 3 additions & 3 deletions packages/bson-bench/test/unit/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('common functionality', function () {
});
});

context('when trying to install an npm package apart from bson or bson-ext', function () {
context('when trying to install an npm package apart from bson', function () {
it('throws an error', function () {
expect(() => new Package('notBson@1.0.0', installDir)).to.throw(
Error,
Expand All @@ -53,7 +53,7 @@ describe('common functionality', function () {
});
});

context('when trying to install a git package apart from bson or bson-ext', function () {
context('when trying to install a git package apart from bson', function () {
it('throws an error', function () {
expect(() => new Package('notBson#abcdabcdabcd', installDir)).to.throw(
Error,
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('common functionality', function () {

context('#install()', function () {
context('when given a correctly formatted npm package that exists', function () {
for (const lib of ['bson@6.0.0', 'bson-ext@4.0.0', 'bson@latest', 'bson-ext@latest']) {
for (const lib of ['bson@6.0.0', 'bson@latest']) {
it(`installs ${lib} successfully to the specified install directory`, async function () {
const pack = new Package(lib, installDir);
await pack.install();
Expand Down
23 changes: 3 additions & 20 deletions packages/bson-bench/test/unit/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ describe('Task', function () {

const BSON_PATH = process.env.BSON_PATH;
const BSON_EXT_PATH = process.env.BSON_EXT_PATH;
const versions = [
'bson@6.2.0',
'bson@4.0.0',
'bson@1.1.6',
'bson@5.0.0',
'bson#v6.1.0',
`bson:${LOCAL_BSON}`,
'bson-ext@4.0.0',
'bson-ext#c1284d1'
];
const versions = ['bson@6.2.0', 'bson@4.0.0', 'bson@5.0.0', 'bson#v6.1.0', `bson:${LOCAL_BSON}`];
const operations: ('serialize' | 'deserialize')[] = ['serialize', 'deserialize'];
if (BSON_PATH) versions.push(`bson:${BSON_PATH}`);
if (BSON_EXT_PATH) versions.push(`bson:${BSON_EXT_PATH}`);
Expand Down Expand Up @@ -59,14 +50,6 @@ describe('Task', function () {
});

it('completes successfully', async function () {
if (
Number(process.versions.node.split('.')[0]) >= 20 &&
/bson-ext#.*/.test(test.library)
) {
console.log('Skipping installing bson-ext via git tag on Node 20');
this.skip();
}

await task.run();
for (const child of task.children) {
expect(child.exitCode).to.not.be.null;
Expand All @@ -76,7 +59,7 @@ describe('Task', function () {

it('strips the tag or commit from the test name', function () {
expect(task.testName).to.not.include(test.library);
expect(task.testName).to.match(/bson|bson-ext/);
expect(task.testName).to.match(/bson/);
});
});
}
Expand Down Expand Up @@ -323,7 +306,7 @@ describe('Task', function () {
before(async () => {
task = new Task({
documentPath: 'test/documents/long_largeArray.json',
library: 'bson-ext@4.0.0',
library: 'bson@4',
operation: 'deserialize',
warmup: 100,
iterations: 100,
Expand Down
4 changes: 2 additions & 2 deletions packages/bson-bench/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { exists } from '../src/utils';
export { exists } from '../src/utils';

/**
* Remove all installed bson and bson-ext versions that have been installed by tests
* Remove all installed bson versions that have been installed by tests
*/
export async function clearTestedDeps(installDir: string) {
const targetDir = path.join(installDir, 'node_modules');
if (await exists(targetDir))
for await (const dirent of await fs.opendir(targetDir)) {
if (/^(bson-ext|bson)-(git|local)?.*$/.test(dirent.name)) {
if (/^(bson)-(git|local)?.*$/.test(dirent.name)) {
await fs.rm(path.join(targetDir, dirent.name), {
recursive: true,
force: true
Expand Down