Skip to content

Commit

Permalink
chore: update sdam_viz to include a write workload
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Jul 15, 2020
1 parent f7a50e2 commit eeea8ac
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/tools/sdam_viz
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

const { MongoClient } = require('../..');
const visualizeMonitoringEvents = require('./utils').visualizeMonitoringEvents;
const { now, calculateDurationInMs } = require('../../lib/utils');
const chalk = require('chalk');
const argv = require('yargs')
.usage('Usage: $0 [options] <connection string>')
.demandCommand(1)
.help('h')
.describe('workload', 'Simulate a read workload')
.describe('writeWorkload', 'Simulate a write workload')
.describe('legacy', 'Use the legacy topology types')
.alias('l', 'legacy')
.alias('w', 'workload')
Expand Down Expand Up @@ -37,6 +39,10 @@ async function run() {
if (argv.workload) {
scheduleWorkload(client);
}

if (argv.writeWorkload) {
scheduleWriteWorkload(client);
}
}

let workloadTimer;
Expand Down Expand Up @@ -67,6 +73,31 @@ async function scheduleWorkload(client) {
}
}

let writeWorkloadTimer;
let writeWorkloadCounter = 0;
let averageWriteMS = 0;
async function scheduleWriteWorkload(client) {
if (!workloadInterrupt) {
// immediately reschedule work
writeWorkloadTimer = setTimeout(() => scheduleWriteWorkload(client), 200);
}

const currentWriteWorkload = writeWorkloadCounter++;

try {
const start = now();
const result = await client.db('test').collection('test').insertOne({ a: 42 });
averageWriteMS = 0.2 * calculateDurationInMs(start) + 0.8 * averageWriteMS;

if (currentWriteWorkload % 100) {
print(`${chalk.yellow(`workload#${currentWriteWorkload}`)} completed 100 writes with average time: ${averageWriteMS}`);
}

} catch (e) {
print(`${chalk.yellow(`workload#${currentWriteWorkload}`)} write failed: ${e.message}`);
}
}

let exitRequestCount = 0;
process.on('SIGINT', async function() {
exitRequestCount++;
Expand All @@ -77,6 +108,7 @@ process.on('SIGINT', async function() {

workloadInterrupt = true;
clearTimeout(workloadTimer);
clearTimeout(writeWorkloadTimer);
await client.close();
});

Expand Down

0 comments on commit eeea8ac

Please sign in to comment.