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' )
12
14
. describe ( 'legacy' , 'Use the legacy topology types' )
13
15
. alias ( 'l' , 'legacy' )
14
16
. alias ( 'w' , 'workload' )
@@ -37,6 +39,10 @@ async function run() {
37
39
if ( argv . workload ) {
38
40
scheduleWorkload ( client ) ;
39
41
}
42
+
43
+ if ( argv . writeWorkload ) {
44
+ scheduleWriteWorkload ( client ) ;
45
+ }
40
46
}
41
47
42
48
let workloadTimer ;
@@ -67,6 +73,31 @@ async function scheduleWorkload(client) {
67
73
}
68
74
}
69
75
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
+
70
101
let exitRequestCount = 0 ;
71
102
process . on ( 'SIGINT' , async function ( ) {
72
103
exitRequestCount ++ ;
@@ -77,6 +108,7 @@ process.on('SIGINT', async function() {
77
108
78
109
workloadInterrupt = true ;
79
110
clearTimeout ( workloadTimer ) ;
111
+ clearTimeout ( writeWorkloadTimer ) ;
80
112
await client . close ( ) ;
81
113
} ) ;
82
114
0 commit comments