@@ -30,46 +30,41 @@ async function runBenchmark(argv) {
3030 // Create histogram for RTT recording
3131 const rttHistogram = argv [ 'measure-rtt-latency' ] ? createRttHistogram ( ) : null ;
3232
33+ const redisOptions = {
34+ socket : {
35+ host : argv . host ,
36+ port : argv . port
37+ } ,
38+ username : argv . user || undefined ,
39+ password : argv . a || undefined ,
40+ commandTimeout : argv [ 'redis-timeout' ]
41+ } ;
42+
3343 let client ;
3444 let nodeAddresses = [ ] ;
3545
36- if ( argv [ 'oss-cluster-api-distribute-subscribers' ] ) {
37- console . log ( 'Using Redis Cluster mode' ) ;
46+ console . log ( `Using ${ argv [ 'slot-refresh-interval' ] } slot-refresh-interval` ) ;
47+ console . log ( `Using ${ argv [ 'redis-timeout' ] } redis-timeout` ) ;
48+
49+ if ( argv [ 'oss-cluster-api-distribute-subscribers' ] === "true" ) {
50+ // Use createCluster for Redis Cluster mode
3851 client = createCluster ( {
39- rootNodes : [ {
40- socket : {
41- host : argv . host ,
42- port : argv . port
43- } ,
44- password : argv . a || undefined ,
45- username : argv . user || undefined
52+ rootNodes : [ {
53+ url : `redis://${ argv . host } :${ argv . port } `
4654 } ] ,
55+ useReplicas : false ,
4756 defaults : {
48- socket : {
49- connectTimeout : argv [ 'redis-timeout' ] ,
50- reconnectStrategy : false // disable auto-reconnect
51- }
57+ username : argv . user || undefined ,
58+ password : argv . a || undefined ,
59+ commandTimeout : argv [ 'redis-timeout' ]
5260 }
5361 } ) ;
54-
62+
5563 await client . connect ( ) ;
56- nodeAddresses = [ `${ argv . host } :${ argv . port } ` ] ;
57- console . log ( 'Cluster mode - connecting through cluster client' ) ;
5864 } else {
59- // Single node mode
60- client = createClient ( {
61- socket : {
62- host : argv . host ,
63- port : argv . port ,
64- connectTimeout : argv [ 'redis-timeout' ] ,
65- reconnectStrategy : false // disable auto-reconnect
66- } ,
67- password : argv . a || undefined ,
68- username : argv . user || undefined
69- } ) ;
70-
65+ // Standalone mode
66+ client = createClient ( redisOptions ) ;
7167 await client . connect ( ) ;
72- nodeAddresses = [ `${ argv . host } :${ argv . port } ` ] ;
7368 console . log ( 'Standalone mode - using single Redis instance' ) ;
7469 }
7570
@@ -80,31 +75,9 @@ async function runBenchmark(argv) {
8075 console . log ( `Will use a subscriber prefix of: ${ argv [ 'subscriber-prefix' ] } <channel id>` ) ;
8176 console . log ( `Total channels: ${ totalChannels } ` ) ;
8277 console . log ( 'Final setup used for benchmark:' ) ;
83- nodeAddresses . forEach ( ( addr , i ) => {
84- console . log ( `Node #${ i } : Address: ${ addr } ` ) ;
85- } ) ;
8678
8779 const promises = [ ] ;
8880
89- function randomInt ( min , max ) {
90- if ( min === max ) return min ;
91- return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
92- }
93-
94- function pickChannelCount ( argv ) {
95- return randomInt (
96- argv [ 'min-number-channels-per-subscriber' ] ,
97- argv [ 'max-number-channels-per-subscriber' ]
98- ) ;
99- }
100-
101- function randomChannel ( argv ) {
102- return (
103- Math . floor ( Math . random ( ) * ( argv [ 'channel-maximum' ] - argv [ 'channel-minimum' ] + 1 ) ) +
104- argv [ 'channel-minimum' ]
105- ) ;
106- }
107-
10881 if ( argv . mode . includes ( 'publish' ) ) {
10982 // Run publishers
11083 totalPublishersRef . value = argv . clients ;
@@ -159,7 +132,6 @@ async function runBenchmark(argv) {
159132 }
160133
161134 const subscriberName = `subscriber#${ clientId } ` ;
162-
163135 const reconnectInterval = randomInt (
164136 argv [ 'min-reconnect-interval' ] ,
165137 argv [ 'max-reconnect-interval' ]
@@ -213,15 +185,14 @@ async function runBenchmark(argv) {
213185 totalPublishersRef ,
214186 messageRateTs ,
215187 rttAccumulator ,
216- rttHistogram ,
217- ( ) => { } // no-op, outputResults is handled after await
188+ rttHistogram
218189 ) ;
219190
220191 // Wait for all routines to finish
221192 console . log ( 'Waiting for all clients to shut down cleanly...' ) ;
222193 await Promise . all ( promises ) ;
223194
224- // Output final results
195+ // THEN output final results
225196 writeFinalResults (
226197 startTime ,
227198 now ,
@@ -234,19 +205,38 @@ async function runBenchmark(argv) {
234205 rttHistogram ,
235206 perSecondStats
236207 ) ;
237- } catch ( err ) {
238- console . error ( 'Benchmark error:' , err ) ;
239- }
240-
241- // Clean up and disconnect the main client
242- try {
243- await client . quit ( ) ;
244- } catch ( err ) {
245- console . error ( 'Error disconnecting main client:' , err ) ;
208+ } finally {
209+ // Clean shutdown of Redis connection
210+ console . log ( 'Shutting down Redis connection...' ) ;
211+ try {
212+ await client . quit ( ) ;
213+ console . log ( 'Redis connection closed successfully' ) ;
214+ } catch ( err ) {
215+ console . error ( 'Error disconnecting Redis client:' , err ) ;
216+ }
246217 }
247218
248- // Clean exit
219+ // cleanly exit the process once done
249220 process . exit ( 0 ) ;
250221}
251222
223+ function randomInt ( min , max ) {
224+ if ( min === max ) return min ;
225+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
226+ }
227+
228+ function pickChannelCount ( argv ) {
229+ return randomInt (
230+ argv [ 'min-number-channels-per-subscriber' ] ,
231+ argv [ 'max-number-channels-per-subscriber' ]
232+ ) ;
233+ }
234+
235+ function randomChannel ( argv ) {
236+ return (
237+ Math . floor ( Math . random ( ) * ( argv [ 'channel-maximum' ] - argv [ 'channel-minimum' ] + 1 ) ) +
238+ argv [ 'channel-minimum' ]
239+ ) ;
240+ }
241+
252242module . exports = { runBenchmark } ;
0 commit comments