Skip to content

Commit a256485

Browse files
committed
chore: update sdam_viz to include a write workload
1 parent 9f0b7ab commit a256485

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/tools/sdam_viz

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
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')
14+
.describe('writeWorkloadInterval', 'Time interval between write workload write attempts')
15+
.describe('writeWorkloadSampleSize', 'Sample size between status display for write workload')
1216
.describe('legacy', 'Use the legacy topology types')
1317
.alias('l', 'legacy')
1418
.alias('w', 'workload')
@@ -37,6 +41,10 @@ async function run() {
3741
if (argv.workload) {
3842
scheduleWorkload(client);
3943
}
44+
45+
if (argv.writeWorkload) {
46+
scheduleWriteWorkload(client);
47+
}
4048
}
4149

4250
let workloadTimer;
@@ -67,6 +75,35 @@ async function scheduleWorkload(client) {
6775
}
6876
}
6977

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

78115
workloadInterrupt = true;
79116
clearTimeout(workloadTimer);
117+
clearTimeout(writeWorkloadTimer);
80118
await client.close();
81119
});
82120

0 commit comments

Comments
 (0)