|
| 1 | +import child_process from 'node:child_process'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import module from 'node:module'; |
| 4 | +import path from 'node:path'; |
| 5 | +import process from 'node:process'; |
| 6 | + |
| 7 | +const __dirname = import.meta.dirname; |
| 8 | +const require = module.createRequire(__dirname); |
| 9 | + |
| 10 | +/** |
| 11 | + * The path to the MongoDB Node.js driver. |
| 12 | + * This MUST be set to the directory the driver is installed in |
| 13 | + * NOT the file "lib/index.js" that is the driver's export. |
| 14 | + */ |
| 15 | +export const MONGODB_DRIVER_PATH = (() => { |
| 16 | + let driverPath = process.env.MONGODB_DRIVER_PATH; |
| 17 | + if (!driverPath?.length) { |
| 18 | + driverPath = path.resolve(__dirname, '../../../..'); |
| 19 | + } |
| 20 | + return driverPath; |
| 21 | +})(); |
| 22 | + |
| 23 | +/** Grab the version from the package.json */ |
| 24 | +export const { version: MONGODB_DRIVER_VERSION } = require( |
| 25 | + path.join(MONGODB_DRIVER_PATH, 'package.json') |
| 26 | +); |
| 27 | + |
| 28 | +/** |
| 29 | + * Use git to optionally determine the git revision, |
| 30 | + * but the benchmarks could be run against an npm installed version so this should be allowed to fail |
| 31 | + */ |
| 32 | +export const MONGODB_DRIVER_REVISION = (() => { |
| 33 | + try { |
| 34 | + return child_process |
| 35 | + .execSync('git rev-parse --short HEAD', { |
| 36 | + cwd: MONGODB_DRIVER_PATH, |
| 37 | + encoding: 'utf8' |
| 38 | + }) |
| 39 | + .trim(); |
| 40 | + } catch { |
| 41 | + return 'unknown revision'; |
| 42 | + } |
| 43 | +})(); |
| 44 | + |
| 45 | +/** |
| 46 | + * Find the BSON dependency inside the driver PATH given and grab the version from the package.json. |
| 47 | + */ |
| 48 | +export const MONGODB_BSON_PATH = path.join(MONGODB_DRIVER_PATH, 'node_modules', 'bson'); |
| 49 | +export const { version: MONGODB_BSON_VERSION } = require( |
| 50 | + path.join(MONGODB_BSON_PATH, 'package.json') |
| 51 | +); |
| 52 | + |
| 53 | +/** |
| 54 | + * If you need to test BSON changes, you should clone, checkout and build BSON. |
| 55 | + * run: `npm link` with no arguments to register the link. |
| 56 | + * Then in the driver you are testing run `npm link bson` to use your local build. |
| 57 | + * |
| 58 | + * This will symlink the BSON into the driver's node_modules directory. So here |
| 59 | + * we can find the revision of the BSON we are testing against if .git exists. |
| 60 | + */ |
| 61 | +export const MONGODB_BSON_REVISION = await (async () => { |
| 62 | + const bsonGitExists = await fs.access(path.join(MONGODB_BSON_PATH, '.git')).then( |
| 63 | + () => true, |
| 64 | + () => false |
| 65 | + ); |
| 66 | + if (!bsonGitExists) { |
| 67 | + return 'installed from npm'; |
| 68 | + } |
| 69 | + try { |
| 70 | + return child_process |
| 71 | + .execSync('git rev-parse --short HEAD', { |
| 72 | + cwd: path.join(MONGODB_BSON_PATH), |
| 73 | + encoding: 'utf8' |
| 74 | + }) |
| 75 | + .trim(); |
| 76 | + } catch { |
| 77 | + return 'unknown revision'; |
| 78 | + } |
| 79 | +})(); |
| 80 | + |
| 81 | +export const MONGODB_CLIENT_OPTIONS = (() => { |
| 82 | + const optionsString = process.env.MONGODB_CLIENT_OPTIONS; |
| 83 | + let options = undefined; |
| 84 | + if (optionsString?.length) { |
| 85 | + options = JSON.parse(optionsString); |
| 86 | + } |
| 87 | + return { ...options }; |
| 88 | +})(); |
| 89 | + |
| 90 | +export const MONGODB_URI = (() => { |
| 91 | + if (process.env.MONGODB_URI?.length) return process.env.MONGODB_URI; |
| 92 | + return 'mongodb://127.0.0.1:27017'; |
| 93 | +})(); |
| 94 | + |
| 95 | +export function snakeToCamel(name: string) { |
| 96 | + return name |
| 97 | + .split('_') |
| 98 | + .map((s, i) => (i !== 0 ? s[0].toUpperCase() + s.slice(1) : s)) |
| 99 | + .join(''); |
| 100 | +} |
| 101 | + |
| 102 | +import type mongodb from '../../../../mongodb.js'; |
| 103 | +export type { mongodb }; |
| 104 | + |
| 105 | +const { MongoClient, GridFSBucket } = require(path.join(MONGODB_DRIVER_PATH)); |
| 106 | + |
| 107 | +const DB_NAME = 'perftest'; |
| 108 | +const COLLECTION_NAME = 'corpus'; |
| 109 | + |
| 110 | +const SPEC_DIRECTORY = path.resolve(__dirname, '..', '..', 'driverBench', 'spec'); |
| 111 | + |
| 112 | +export function metrics(test_name: string, result: number, count: number) { |
| 113 | + return { |
| 114 | + info: { |
| 115 | + test_name, |
| 116 | + // Args can only be a map of string -> int32. So if its a number leave it be, |
| 117 | + // if it is anything else test for truthiness and set to 1 or 0. |
| 118 | + args: Object.fromEntries( |
| 119 | + Object.entries(MONGODB_CLIENT_OPTIONS).map(([key, value]) => [ |
| 120 | + key, |
| 121 | + typeof value === 'number' ? value : value ? 1 : 0 |
| 122 | + ]) |
| 123 | + ) |
| 124 | + }, |
| 125 | + metrics: [ |
| 126 | + { name: 'megabytes_per_second', value: result }, |
| 127 | + // Reporting the count so we can verify programmatically or in UI how many iterations we reached |
| 128 | + { name: 'count', value: count } |
| 129 | + ] |
| 130 | + } as const; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * This class exists to abstract some of the driver API so we can gloss over version differences. |
| 135 | + * For use in setup/teardown mostly. |
| 136 | + */ |
| 137 | +export class DriverTester { |
| 138 | + public client: mongodb.MongoClient; |
| 139 | + constructor() { |
| 140 | + this.client = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS); |
| 141 | + } |
| 142 | + |
| 143 | + public get db() { |
| 144 | + return this.client.db(DB_NAME); |
| 145 | + } |
| 146 | + |
| 147 | + public get collection() { |
| 148 | + return this.client.db(DB_NAME).collection(COLLECTION_NAME); |
| 149 | + } |
| 150 | + |
| 151 | + public get bucket(): mongodb.GridFSBucket { |
| 152 | + return new GridFSBucket(this.db); |
| 153 | + } |
| 154 | + |
| 155 | + async drop() { |
| 156 | + const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS); |
| 157 | + const db = utilClient.db(DB_NAME); |
| 158 | + const collection = db.collection(COLLECTION_NAME); |
| 159 | + await collection.drop().catch(() => null); |
| 160 | + await db.dropDatabase().catch(() => null); |
| 161 | + await utilClient.close(); |
| 162 | + } |
| 163 | + |
| 164 | + async create() { |
| 165 | + const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS); |
| 166 | + try { |
| 167 | + await utilClient.db(DB_NAME).createCollection(COLLECTION_NAME); |
| 168 | + } finally { |
| 169 | + await utilClient.close(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + async load(filePath: string, type: 'json' | 'string' | 'buffer'): Promise<any> { |
| 174 | + const content = await fs.readFile(path.join(SPEC_DIRECTORY, filePath)); |
| 175 | + if (type === 'buffer') return content; |
| 176 | + const string = content.toString('utf8'); |
| 177 | + if (type === 'string') return string; |
| 178 | + if (type === 'json') return JSON.parse(string); |
| 179 | + throw new Error('unknown type: ' + type); |
| 180 | + } |
| 181 | + |
| 182 | + async insertManyOf(document: Record<string, any>, length: number, addId = false) { |
| 183 | + const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS); |
| 184 | + const db = utilClient.db(DB_NAME); |
| 185 | + const collection = db.collection(COLLECTION_NAME); |
| 186 | + try { |
| 187 | + await collection.insertMany( |
| 188 | + Array.from({ length }, (_, _id) => ({ ...(addId ? { _id } : {}), ...document })) as any[] |
| 189 | + ); |
| 190 | + } finally { |
| 191 | + await utilClient.close(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + async close() { |
| 196 | + await this.client.close(); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +export const driver = new DriverTester(); |
0 commit comments