Skip to content

Commit c810ca6

Browse files
committed
trying to debug node-redis bench
1 parent 98f90fa commit c810ca6

File tree

10 files changed

+164
-321
lines changed

10 files changed

+164
-321
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,4 @@ coverage.txt
9292
# Profiler Results #
9393
####################
9494
*.pprof
95+
/js/node-redis/node_modules/

js/node-redis/.gitignore

Lines changed: 0 additions & 34 deletions
This file was deleted.

js/node-redis/README.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

js/node-redis/bin/pubsub-sub-bench.js

100755100644
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env node
2-
// filepath: /Users/hristo.temelski/code/etc/pubsub-sub-bench/js/node-redis/bin/pubsub-sub-bench.js
32

43
const { parseArgs } = require('../lib/config');
54
const { runBenchmark } = require('../lib/redisManager');
65

7-
// Parse command line arguments
8-
const argv = parseArgs();
6+
(async () => {
7+
const argv = parseArgs();
98

10-
// Run the benchmark
11-
runBenchmark(argv).catch(err => {
12-
console.error('Benchmark execution error:', err);
13-
process.exit(1);
14-
});
9+
try {
10+
await runBenchmark(argv);
11+
} catch (err) {
12+
console.error('Error in main execution:', err);
13+
process.exit(1);
14+
}
15+
})();

js/node-redis/lib/config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// filepath: /Users/hristo.temelski/code/etc/pubsub-sub-bench/js/node-redis/lib/config.js
21
const yargs = require('yargs');
32

43
function parseArgs() {
@@ -40,4 +39,4 @@ function parseArgs() {
4039
.help().argv;
4140
}
4241

43-
module.exports = { parseArgs };
42+
module.exports = { parseArgs };

js/node-redis/lib/metrics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,4 @@ module.exports = {
212212
writeFinalResults,
213213
createRttHistogram,
214214
RttAccumulator
215-
};
215+
};

js/node-redis/lib/publisher.js

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
// filepath: /Users/hristo.temelski/code/etc/pubsub-sub-bench/js/node-redis/lib/publisher.js
2-
const { createClient } = require('redis');
3-
41
async function publisherRoutine(
52
clientName,
63
channels,
74
mode,
85
measureRTT,
96
verbose,
107
dataSize,
11-
redisOptions,
8+
client,
129
isRunningRef,
13-
totalMessagesRef,
14-
rateLimiter
10+
totalMessagesRef
1511
) {
1612
if (verbose) {
1713
console.log(
@@ -22,27 +18,11 @@ async function publisherRoutine(
2218
}
2319

2420
const payload = !measureRTT ? 'A'.repeat(dataSize) : '';
25-
26-
// Create a new Redis client
27-
const client = createClient(redisOptions);
28-
29-
// Set up error handling
30-
client.on('error', (err) => {
31-
console.error(`[${clientName}] Redis error: ${err.message}`);
32-
});
33-
21+
3422
try {
35-
// Connect to Redis
36-
await client.connect();
37-
3823
while (isRunningRef.value) {
3924
for (const channel of channels) {
4025
try {
41-
// Apply rate limiting if configured
42-
if (rateLimiter) {
43-
await rateLimiter.removeTokens(1);
44-
}
45-
4626
let msg = payload;
4727
if (measureRTT) {
4828
msg = Date.now().toString();
@@ -55,25 +35,15 @@ async function publisherRoutine(
5535
}
5636
totalMessagesRef.value++;
5737
} catch (err) {
58-
console.error(`[${clientName}] Error publishing to channel ${channel}:`, err);
38+
console.error(`Error publishing to channel ${channel}:`, err);
5939
}
6040
}
6141
}
62-
} catch (err) {
63-
console.error(`[${clientName}] Redis connection error:`, err);
6442
} finally {
65-
// Clean shutdown - disconnect the client
43+
// Clean shutdown - client is managed by redisManager
6644
if (verbose) {
6745
console.log(`Publisher ${clientName} shutting down...`);
6846
}
69-
try {
70-
await client.quit();
71-
if (verbose) {
72-
console.log(`Publisher ${clientName} disconnected successfully`);
73-
}
74-
} catch (err) {
75-
console.error(`Error disconnecting publisher ${clientName}:`, err);
76-
}
7747
}
7848
}
7949

js/node-redis/lib/redisManager.js

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
252242
module.exports = { runBenchmark };

0 commit comments

Comments
 (0)