-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathdriver.mts
218 lines (191 loc) · 6.33 KB
/
driver.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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 = (() => {
const connectionString = process.env.MONGODB_URI;
if (connectionString?.length) {
return connectionString;
}
return 'mongodb://localhost: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, BSON } = require(path.join(MONGODB_DRIVER_PATH));
export { BSON };
export const EJSON = BSON.EJSON;
const DB_NAME = 'perftest';
const COLLECTION_NAME = 'corpus';
// TODO(NODE-6729): move spec into this folder: export const SPEC_DIRECTORY = path.resolve(__dirname, '..', 'spec');
export const SPEC_DIRECTORY = path.resolve(__dirname, '..', '..', 'driverBench', 'spec');
export const PARALLEL_DIRECTORY = path.resolve(SPEC_DIRECTORY, 'parallel');
export const TEMP_DIRECTORY = path.resolve(SPEC_DIRECTORY, 'tmp');
export type Metric = {
name: 'megabytes_per_second';
value: number;
};
export type MetricInfo = {
info: {
test_name: string;
args: Record<string, number>;
};
metrics: Metric[];
};
export function metrics(test_name: string, result: number): MetricInfo {
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 }]
} 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 {
readonly DB_NAME = DB_NAME;
readonly COLLECTION_NAME = COLLECTION_NAME;
public client: mongodb.MongoClient;
constructor() {
this.client = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
}
bucket(db: mongodb.Db) {
return new GridFSBucket(db);
}
async drop() {
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
const db = utilClient.db(DB_NAME);
const collection = db.collection(COLLECTION_NAME);
await collection.drop().catch(() => null);
await db.dropDatabase().catch(() => null);
await utilClient.close();
}
async create() {
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
try {
await utilClient.db(DB_NAME).createCollection(COLLECTION_NAME);
} finally {
await utilClient.close();
}
}
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 resetTmpDir() {
await fs.rm(TEMP_DIRECTORY, { recursive: true, force: true });
await fs.mkdir(TEMP_DIRECTORY);
}
async insertManyOf(document: Record<string, any>, length: number, addId = false) {
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
const db = utilClient.db(DB_NAME);
const collection = db.collection(COLLECTION_NAME);
try {
await collection.insertMany(
Array.from({ length }, (_, _id) => ({ ...(addId ? { _id } : {}), ...document })) as any[]
);
} finally {
await utilClient.close();
}
}
async close() {
await this.client.close();
}
}
export const driver = new DriverTester();