Skip to content

Commit da1b889

Browse files
committed
chore: update sdam_viz to include a write workload
1 parent 3d8ac55 commit da1b889

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/tools/sdam_viz

+40
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
const { MongoClient } = require('../../src');
55
const visualizeMonitoringEvents = require('./utils').visualizeMonitoringEvents;
6+
const { now, calculateDurationInMs } = require('../../lib/utils');
67
const chalk = require('chalk');
78
const argv = require('yargs')
89
.usage('Usage: $0 [options] <connection string>')
910
.demandCommand(1)
1011
.help('h')
1112
.describe('workload', 'Simulate a read workload')
13+
.describe('writeWorkload', 'Simulate a write workload')
14+
.describe('writeWorkloadInterval', 'Time interval between write workload write attempts')
15+
.describe('writeWorkloadSampleSize', 'Sample size between status display for write workload')
16+
.describe('legacy', 'Use the legacy topology types')
17+
.alias('l', 'legacy')
1218
.alias('w', 'workload')
1319
.alias('h', 'help').argv;
1420

@@ -28,6 +34,10 @@ async function run() {
2834
if (argv.workload) {
2935
scheduleWorkload(client);
3036
}
37+
38+
if (argv.writeWorkload) {
39+
scheduleWriteWorkload(client);
40+
}
3141
}
3242

3343
let workloadTimer;
@@ -58,6 +68,35 @@ async function scheduleWorkload(client) {
5868
}
5969
}
6070

71+
let writeWorkloadTimer;
72+
let writeWorkloadCounter = 0;
73+
let averageWriteMS = 0;
74+
let completedWriteWorkloads = 0;
75+
const writeWorkloadSampleSize = argv.writeWorkloadSampleSize || 100;
76+
const writeWorkloadInterval = argv.writeWorkloadInterval || 100;
77+
async function scheduleWriteWorkload(client) {
78+
if (!workloadInterrupt) {
79+
// immediately reschedule work
80+
writeWorkloadTimer = setTimeout(() => scheduleWriteWorkload(client), writeWorkloadInterval);
81+
}
82+
83+
const currentWriteWorkload = writeWorkloadCounter++;
84+
85+
try {
86+
const start = now();
87+
const result = await client.db('test').collection('test').insertOne({ a: 42 });
88+
averageWriteMS = 0.2 * calculateDurationInMs(start) + 0.8 * averageWriteMS;
89+
90+
completedWriteWorkloads++;
91+
if (completedWriteWorkloads % writeWorkloadSampleSize === 0) {
92+
print(`${chalk.yellow(`workload#${currentWriteWorkload}`)} completed ${completedWriteWorkloads} writes with average time: ${averageWriteMS}`);
93+
}
94+
95+
} catch (e) {
96+
print(`${chalk.yellow(`workload#${currentWriteWorkload}`)} write failed: ${e.message}`);
97+
}
98+
}
99+
61100
let exitRequestCount = 0;
62101
process.on('SIGINT', async function() {
63102
exitRequestCount++;
@@ -68,6 +107,7 @@ process.on('SIGINT', async function() {
68107

69108
workloadInterrupt = true;
70109
clearTimeout(workloadTimer);
110+
clearTimeout(writeWorkloadTimer);
71111
await client.close();
72112
});
73113

0 commit comments

Comments
 (0)