Skip to content

Commit eeea8ac

Browse files
committed
chore: update sdam_viz to include a write workload
1 parent f7a50e2 commit eeea8ac

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/tools/sdam_viz

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
const { MongoClient } = require('../..');
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')
1214
.describe('legacy', 'Use the legacy topology types')
1315
.alias('l', 'legacy')
1416
.alias('w', 'workload')
@@ -37,6 +39,10 @@ async function run() {
3739
if (argv.workload) {
3840
scheduleWorkload(client);
3941
}
42+
43+
if (argv.writeWorkload) {
44+
scheduleWriteWorkload(client);
45+
}
4046
}
4147

4248
let workloadTimer;
@@ -67,6 +73,31 @@ async function scheduleWorkload(client) {
6773
}
6874
}
6975

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

78109
workloadInterrupt = true;
79110
clearTimeout(workloadTimer);
111+
clearTimeout(writeWorkloadTimer);
80112
await client.close();
81113
});
82114

0 commit comments

Comments
 (0)