3
3
4
4
const { MongoClient } = require ( '../..' ) ;
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' )
12
16
. describe ( 'legacy' , 'Use the legacy topology types' )
13
17
. alias ( 'l' , 'legacy' )
14
18
. alias ( 'w' , 'workload' )
@@ -37,6 +41,10 @@ async function run() {
37
41
if ( argv . workload ) {
38
42
scheduleWorkload ( client ) ;
39
43
}
44
+
45
+ if ( argv . writeWorkload ) {
46
+ scheduleWriteWorkload ( client ) ;
47
+ }
40
48
}
41
49
42
50
let workloadTimer ;
@@ -67,6 +75,35 @@ async function scheduleWorkload(client) {
67
75
}
68
76
}
69
77
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
+
70
107
let exitRequestCount = 0 ;
71
108
process . on ( 'SIGINT' , async function ( ) {
72
109
exitRequestCount ++ ;
@@ -77,6 +114,7 @@ process.on('SIGINT', async function() {
77
114
78
115
workloadInterrupt = true ;
79
116
clearTimeout ( workloadTimer ) ;
117
+ clearTimeout ( writeWorkloadTimer ) ;
80
118
await client . close ( ) ;
81
119
} ) ;
82
120
0 commit comments