|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +import { performance } from 'perf_hooks'; |
| 3 | +import { readFile } from 'fs/promises'; |
| 4 | +import { cpus, totalmem } from 'os'; |
| 5 | + |
| 6 | +const hw = cpus(); |
| 7 | +const ram = totalmem() / 1024 ** 3; |
| 8 | +const platform = { name: hw[0].model, cores: hw.length, ram: `${ram}GB` }; |
| 9 | +const ITERATIONS = 1_000_000; |
| 10 | + |
| 11 | +const systemInfo = [ |
| 12 | + `\n- cpu: ${platform.name}`, |
| 13 | + `- cores: ${platform.cores}`, |
| 14 | + `- os: ${process.platform}`, |
| 15 | + `- ram: ${platform.ram}`, |
| 16 | + `- iterations: ${ITERATIONS.toLocaleString()}` |
| 17 | +].join('\n'); |
| 18 | + |
| 19 | +const readJSONFile = async path => |
| 20 | + JSON.parse(await readFile(new URL(path, import.meta.url), { encoding: 'utf8' })); |
| 21 | + |
| 22 | +function average(array) { |
| 23 | + let sum = 0; |
| 24 | + for (const value of array) sum += value; |
| 25 | + return sum / array.length; |
| 26 | +} |
| 27 | + |
| 28 | +function testPerformance(fn, iterations = ITERATIONS) { |
| 29 | + let measurements = []; |
| 30 | + for (let i = 0; i < iterations; i++) { |
| 31 | + const start = performance.now(); |
| 32 | + fn(i); |
| 33 | + const end = performance.now(); |
| 34 | + measurements.push(end - start); |
| 35 | + } |
| 36 | + return average(measurements).toFixed(8); |
| 37 | +} |
| 38 | + |
| 39 | +async function main() { |
| 40 | + const [currentBSON, currentReleaseBSON, legacyBSONLib] = await Promise.all([ |
| 41 | + (async () => ({ |
| 42 | + lib: await import('../../lib/bson.js'), |
| 43 | + version: 'current local' |
| 44 | + }))(), |
| 45 | + (async () => ({ |
| 46 | + lib: await import('../../node_modules/bson_latest/lib/bson.js'), |
| 47 | + version: (await readJSONFile('../../node_modules/bson_latest/package.json')).version |
| 48 | + }))(), |
| 49 | + (async () => { |
| 50 | + const legacyBSON = (await import('../../node_modules/bson_legacy/index.js')).default; |
| 51 | + return { |
| 52 | + lib: { ...legacyBSON, ...legacyBSON.prototype }, |
| 53 | + version: (await readJSONFile('../../node_modules/bson_legacy/package.json')).version |
| 54 | + }; |
| 55 | + })() |
| 56 | + ]).catch(error => { |
| 57 | + console.error(error); |
| 58 | + console.error( |
| 59 | + `Please run:\n${[ |
| 60 | + 'npm run build', |
| 61 | + 'npm install --no-save bson_legacy@npm:bson@1 bson_latest@npm:bson@latest' |
| 62 | + ].join('\n')}` |
| 63 | + ); |
| 64 | + process.exit(1); |
| 65 | + }); |
| 66 | + |
| 67 | + const documents = Array.from({ length: ITERATIONS }, () => |
| 68 | + currentReleaseBSON.lib.serialize({ |
| 69 | + _id: new currentReleaseBSON.lib.ObjectId(), |
| 70 | + field1: 'value1' |
| 71 | + }) |
| 72 | + ); |
| 73 | + |
| 74 | + console.log(systemInfo); |
| 75 | + |
| 76 | + for (const bson of [currentBSON, currentReleaseBSON, legacyBSONLib]) { |
| 77 | + console.log(`\nBSON@${bson.version}`); |
| 78 | + console.log( |
| 79 | + `deserialize({ oid, string }, { validation: { utf8: false } }) takes ${testPerformance(i => |
| 80 | + bson.lib.deserialize(documents[i], { validation: { utf8: false } }) |
| 81 | + )}ms on average` |
| 82 | + ); |
| 83 | + |
| 84 | + const oidBuffer = Buffer.from('00'.repeat(12), 'hex'); |
| 85 | + console.log( |
| 86 | + `new Oid(buf) take ${testPerformance(() => new bson.lib.ObjectId(oidBuffer))}ms on average` |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + console.log(); |
| 91 | +} |
| 92 | + |
| 93 | +main() |
| 94 | + .then(() => null) |
| 95 | + .catch(error => console.error(error)); |
0 commit comments