Skip to content

Commit 0ebf370

Browse files
committed
chore(NODE-5771): benchmark new connection
1 parent a3c0298 commit 0ebf370

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

modern.cjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const totalStart = performance.now();
2+
/* eslint-disable no-console */
3+
/* eslint-disable @typescript-eslint/no-var-requires */
4+
const process = require('node:process');
5+
const { MongoClient } = require('./lib/index.js');
6+
const { ModernConnection } = require('./lib/cmap/connection.js');
7+
8+
const tweet = require('./test/benchmarks/driverBench/spec/single_and_multi_document/tweet.json');
9+
10+
const client = new MongoClient(process.env.MONGODB_URI, { connectionType: ModernConnection });
11+
12+
async function main() {
13+
console.log('modern connection');
14+
15+
const db = client.db('test');
16+
let collection = db.collection('test');
17+
await collection.drop().catch(() => null);
18+
collection = await db.createCollection('test');
19+
await collection.insertOne(tweet);
20+
21+
const total = 10_000;
22+
23+
for (let i = 0; i < total; i++) {
24+
await collection.findOne();
25+
}
26+
27+
const start = performance.now() - totalStart;
28+
for (let i = 0; i < total; i++) {
29+
await collection.findOne();
30+
}
31+
const end = performance.now() - totalStart;
32+
33+
console.log(
34+
`end - start = ms time for 10k findOne calls (script boot: ${totalStart.toFixed(3)})`
35+
);
36+
console.log(`${end.toFixed(3)} - ${start.toFixed(3)} = ${(end - start).toFixed(4)}`);
37+
console.log(`avg findOne: ${((end - start) / total).toFixed(3)} ms`);
38+
39+
await client.close();
40+
}
41+
42+
main().catch(console.error);

old.cjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const totalStart = performance.now();
2+
/* eslint-disable no-console */
3+
/* eslint-disable @typescript-eslint/no-var-requires */
4+
const process = require('node:process');
5+
const { MongoClient } = require('./lib/index.js');
6+
const { Connection } = require('./lib/cmap/connection.js');
7+
8+
const tweet = require('./test/benchmarks/driverBench/spec/single_and_multi_document/tweet.json');
9+
10+
const client = new MongoClient(process.env.MONGODB_URI, { connectionType: Connection });
11+
12+
async function main() {
13+
console.log('old connection');
14+
15+
const db = client.db('test');
16+
let collection = db.collection('test');
17+
await collection.drop().catch(() => null);
18+
collection = await db.createCollection('test');
19+
await collection.insertOne(tweet);
20+
21+
const total = 10_000;
22+
23+
for (let i = 0; i < total; i++) {
24+
await collection.findOne();
25+
}
26+
27+
const start = performance.now() - totalStart;
28+
for (let i = 0; i < total; i++) {
29+
await collection.findOne();
30+
}
31+
const end = performance.now() - totalStart;
32+
33+
console.log(
34+
`end - start = ms time for 10k findOne calls (script boot: ${totalStart.toFixed(3)})`
35+
);
36+
console.log(`${end.toFixed(3)} - ${start.toFixed(3)} = ${(end - start).toFixed(4)}`);
37+
console.log(`avg findOne: ${((end - start) / total).toFixed(3)} ms`);
38+
39+
await client.close();
40+
}
41+
42+
main().catch(console.error);

test/benchmarks/driverBench/common.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { Readable } = require('stream');
66
const { pipeline } = require('stream/promises');
77
const { MongoClient } = require('../../..');
88
const { GridFSBucket } = require('../../..');
9+
// eslint-disable-next-line no-restricted-modules
10+
const { ModernConnection, Connection } = require('../../../lib/cmap/connection');
911

1012
// eslint-disable-next-line no-restricted-modules
1113
const { MONGODB_ERROR_CODES } = require('../../../lib/error');
@@ -25,7 +27,9 @@ function loadSpecString(filePath) {
2527
}
2628

2729
function makeClient() {
28-
this.client = new MongoClient(process.env.MONGODB_URI || 'mongodb://localhost:27017');
30+
this.client = new MongoClient(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017', {
31+
connectionType: Connection
32+
});
2933
}
3034

3135
function connectClient() {

test/benchmarks/driverBench/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const MongoBench = require('../mongoBench');
4+
const os = require('node:os');
45

56
const Runner = MongoBench.Runner;
67

@@ -11,6 +12,21 @@ const { inspect } = require('util');
1112
const { writeFile } = require('fs/promises');
1213
const { makeParallelBenchmarks, makeSingleBench, makeMultiBench } = require('../mongoBench/suites');
1314

15+
const hw = os.cpus();
16+
const ram = os.totalmem() / 1024 ** 3;
17+
const platform = { name: hw[0].model, cores: hw.length, ram: `${ram}GB` };
18+
19+
const systemInfo = () =>
20+
[
21+
`Connection`,
22+
`\n- cpu: ${platform.name}`,
23+
`- cores: ${platform.cores}`,
24+
`- arch: ${os.arch()}`,
25+
`- os: ${process.platform} (${os.release()})`,
26+
`- ram: ${platform.ram}`
27+
].join('\n');
28+
console.log(systemInfo());
29+
1430
function average(arr) {
1531
return arr.reduce((x, y) => x + y, 0) / arr.length;
1632
}

0 commit comments

Comments
 (0)