3
3
4
4
const { MongoClient } = require ( '../../src' ) ;
5
5
const visualizeMonitoringEvents = require ( './utils' ) . visualizeMonitoringEvents ;
6
+ const { now, calculateDurationInMs } = require ( '../../lib/utils' ) ;
6
7
const chalk = require ( 'chalk' ) ;
7
8
const argv = require ( 'yargs' )
8
9
. usage ( 'Usage: $0 [options] <connection string>' )
9
10
. demandCommand ( 1 )
10
11
. help ( 'h' )
11
12
. 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' )
12
18
. alias ( 'w' , 'workload' )
13
19
. alias ( 'h' , 'help' ) . argv ;
14
20
@@ -28,6 +34,10 @@ async function run() {
28
34
if ( argv . workload ) {
29
35
scheduleWorkload ( client ) ;
30
36
}
37
+
38
+ if ( argv . writeWorkload ) {
39
+ scheduleWriteWorkload ( client ) ;
40
+ }
31
41
}
32
42
33
43
let workloadTimer ;
@@ -58,6 +68,35 @@ async function scheduleWorkload(client) {
58
68
}
59
69
}
60
70
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
+
61
100
let exitRequestCount = 0 ;
62
101
process . on ( 'SIGINT' , async function ( ) {
63
102
exitRequestCount ++ ;
@@ -68,6 +107,7 @@ process.on('SIGINT', async function() {
68
107
69
108
workloadInterrupt = true ;
70
109
clearTimeout ( workloadTimer ) ;
110
+ clearTimeout ( writeWorkloadTimer ) ;
71
111
await client . close ( ) ;
72
112
} ) ;
73
113
0 commit comments