-
Notifications
You must be signed in to change notification settings - Fork 1.8k
test(NODE-6723): isolate and warmup benchmarks #4398
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6c3e6d8
test(NODE-6705): isolate and warmup benchmarks
nbbeeken 5ebb72d
test: run new bench in CI
nbbeeken c1c9193
comments
nbbeeken 908647b
fixes
nbbeeken 4096256
chore: improve final print, sort, and predict finish time
nbbeeken 0709f10
chore: prints
nbbeeken a1a617b
chore: todos
nbbeeken bacb203
chore: no pkg
nbbeeken bbfbaaf
chore: no ns getter and util client closed
nbbeeken 50964d2
chore: simpler TS
nbbeeken c0e96ee
chore: await close
nbbeeken 50e008c
chore: go to jail
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
results.json | ||
results_*.json | ||
package-lock.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "driver_bench", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"prestart": "tsc", | ||
"start": "node lib/main.mjs" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.13.0" | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import child_process from 'node:child_process'; | ||
import fs from 'node:fs/promises'; | ||
import module from 'node:module'; | ||
import path from 'node:path'; | ||
import process from 'node:process'; | ||
|
||
const __dirname = import.meta.dirname; | ||
const require = module.createRequire(__dirname); | ||
|
||
/** | ||
* The path to the MongoDB Node.js driver. | ||
* This MUST be set to the directory the driver is installed in | ||
* NOT the file "lib/index.js" that is the driver's export. | ||
*/ | ||
export const MONGODB_DRIVER_PATH = (() => { | ||
let driverPath = process.env.MONGODB_DRIVER_PATH; | ||
if (!driverPath?.length) { | ||
driverPath = path.resolve(__dirname, '../../../..'); | ||
} | ||
return driverPath; | ||
})(); | ||
|
||
/** Grab the version from the package.json */ | ||
export const { version: MONGODB_DRIVER_VERSION } = require( | ||
path.join(MONGODB_DRIVER_PATH, 'package.json') | ||
); | ||
|
||
/** | ||
* Use git to optionally determine the git revision, | ||
* but the benchmarks could be run against an npm installed version so this should be allowed to fail | ||
*/ | ||
export const MONGODB_DRIVER_REVISION = (() => { | ||
try { | ||
return child_process | ||
.execSync('git rev-parse --short HEAD', { | ||
cwd: MONGODB_DRIVER_PATH, | ||
encoding: 'utf8' | ||
}) | ||
.trim(); | ||
} catch { | ||
return 'unknown revision'; | ||
} | ||
})(); | ||
|
||
/** | ||
* Find the BSON dependency inside the driver PATH given and grab the version from the package.json. | ||
*/ | ||
export const MONGODB_BSON_PATH = path.join(MONGODB_DRIVER_PATH, 'node_modules', 'bson'); | ||
export const { version: MONGODB_BSON_VERSION } = require( | ||
path.join(MONGODB_BSON_PATH, 'package.json') | ||
); | ||
|
||
/** | ||
* If you need to test BSON changes, you should clone, checkout and build BSON. | ||
* run: `npm link` with no arguments to register the link. | ||
* Then in the driver you are testing run `npm link bson` to use your local build. | ||
* | ||
* This will symlink the BSON into the driver's node_modules directory. So here | ||
* we can find the revision of the BSON we are testing against if .git exists. | ||
*/ | ||
export const MONGODB_BSON_REVISION = await (async () => { | ||
const bsonGitExists = await fs.access(path.join(MONGODB_BSON_PATH, '.git')).then( | ||
() => true, | ||
() => false | ||
); | ||
if (!bsonGitExists) { | ||
return 'installed from npm'; | ||
} | ||
try { | ||
return child_process | ||
.execSync('git rev-parse --short HEAD', { | ||
cwd: path.join(MONGODB_BSON_PATH), | ||
encoding: 'utf8' | ||
}) | ||
.trim(); | ||
} catch { | ||
return 'unknown revision'; | ||
} | ||
})(); | ||
|
||
export const MONGODB_CLIENT_OPTIONS = (() => { | ||
const optionsString = process.env.MONGODB_CLIENT_OPTIONS; | ||
let options = undefined; | ||
if (optionsString?.length) { | ||
options = JSON.parse(optionsString); | ||
} | ||
return { ...options }; | ||
})(); | ||
|
||
export const MONGODB_URI = (() => { | ||
if (process.env.MONGODB_URI?.length) return process.env.MONGODB_URI; | ||
return 'mongodb://127.0.0.1:27017'; | ||
})(); | ||
|
||
export function snakeToCamel(name: string) { | ||
return name | ||
.split('_') | ||
.map((s, i) => (i !== 0 ? s[0].toUpperCase() + s.slice(1) : s)) | ||
.join(''); | ||
} | ||
|
||
import type mongodb from '../../../../mongodb.js'; | ||
export type { mongodb }; | ||
|
||
const { MongoClient, GridFSBucket } = require(path.join(MONGODB_DRIVER_PATH)); | ||
|
||
const DB_NAME = 'perftest'; | ||
const COLLECTION_NAME = 'corpus'; | ||
|
||
const SPEC_DIRECTORY = path.resolve(__dirname, '..', '..', 'driverBench', 'spec'); | ||
|
||
export function metrics(test_name: string, result: number, count: number) { | ||
return { | ||
info: { | ||
test_name, | ||
// Args can only be a map of string -> int32. So if its a number leave it be, | ||
// if it is anything else test for truthiness and set to 1 or 0. | ||
args: Object.fromEntries( | ||
Object.entries(MONGODB_CLIENT_OPTIONS).map(([key, value]) => [ | ||
key, | ||
typeof value === 'number' ? value : value ? 1 : 0 | ||
]) | ||
) | ||
}, | ||
metrics: [ | ||
{ name: 'megabytes_per_second', value: result }, | ||
// Reporting the count so we can verify programmatically or in UI how many iterations we reached | ||
{ name: 'count', value: count } | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
} as const; | ||
} | ||
|
||
/** | ||
* This class exists to abstract some of the driver API so we can gloss over version differences. | ||
* For use in setup/teardown mostly. | ||
*/ | ||
export class DriverTester { | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private utilClient: mongodb.MongoClient; | ||
public client: mongodb.MongoClient; | ||
constructor() { | ||
this.utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS); | ||
this.client = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS); | ||
} | ||
|
||
private get ns() { | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return this.utilClient.db(DB_NAME).collection(COLLECTION_NAME); | ||
} | ||
|
||
public get db() { | ||
return this.client.db(DB_NAME); | ||
} | ||
|
||
public get collection() { | ||
return this.client.db(DB_NAME).collection(COLLECTION_NAME); | ||
} | ||
|
||
public get bucket(): mongodb.GridFSBucket { | ||
return new GridFSBucket(this.db); | ||
} | ||
|
||
async drop() { | ||
await this.ns.drop().catch(() => null); | ||
await this.utilClient | ||
.db(DB_NAME) | ||
.dropDatabase() | ||
.catch(() => null); | ||
} | ||
|
||
async create() { | ||
await this.utilClient.db(DB_NAME).createCollection(COLLECTION_NAME); | ||
} | ||
|
||
async load(filePath: string, type: 'json' | 'string' | 'buffer'): Promise<any> { | ||
const content = await fs.readFile(path.join(SPEC_DIRECTORY, filePath)); | ||
if (type === 'buffer') return content; | ||
const string = content.toString('utf8'); | ||
if (type === 'string') return string; | ||
if (type === 'json') return JSON.parse(string); | ||
throw new Error('unknown type: ' + type); | ||
} | ||
|
||
async insertManyOf(document: Record<string, any>, length: number, addId = false) { | ||
await this.ns.insertMany( | ||
Array.from({ length }, (_, _id) => ({ ...(addId ? { _id } : {}), ...document })) as any[] | ||
); | ||
} | ||
|
||
async close() { | ||
await this.client.close(); | ||
await this.utilClient.close(); | ||
} | ||
} | ||
|
||
export const driver = new DriverTester(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.