Skip to content

Commit 0ccddec

Browse files
Claude Codeclaude
andcommitted
Address Gemini code review comments on PR #494
High Priority: - Separate setup logic from measured operations in connection benchmarks - Create shared connection once, reuse across all benchmark iterations - Dramatically improved benchmark accuracy (2.4M ops/sec for ping vs 33K before) Medium Priority: - Lazily initialize suite results in track-performance.mjs to prevent empty entries - Remove redundant test.include from vitest.bench.config.mjs - Add bench:compare script to package.json to match README documentation Updated baseline.json with new accurate performance measurements: - Connection operations now measure actual send performance (not setup) - Frame serialization remains consistent at 4M+ ops/sec - Ping/pong operations: 2.4M / 1.9M ops/sec (previously 33K) - Message sending: 900K / 220K / 108K ops/sec (previously 25-28K) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 0480dca commit 0ccddec

File tree

5 files changed

+36
-45
lines changed

5 files changed

+36
-45
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"test:autobahn": "cd test/autobahn && ./run-wstest.js",
5858
"bench": "vitest bench --run --config vitest.bench.config.mjs",
5959
"bench:baseline": "node test/benchmark/track-performance.mjs save",
60+
"bench:compare": "node test/benchmark/track-performance.mjs compare",
6061
"bench:check": "node test/benchmark/track-performance.mjs check",
6162
"lint": "eslint lib/**/*.js test/**/*.js",
6263
"lint:fix": "eslint lib/**/*.js test/**/*.js --fix"

test/benchmark/baseline.json

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
{
2-
"timestamp": "2025-10-06T16:43:17.731Z",
2+
"timestamp": "2025-10-06T17:27:59.641Z",
33
"results": {
4-
"WebSocketConnection Performance 3866ms": {
5-
"create connection instance": 28323.39,
6-
"send small UTF-8 message": 28201.35,
7-
"send medium UTF-8 message (1KB)": 24615.97,
8-
"send binary message (1KB)": 24889.31,
9-
"send ping frame": 31562.37,
10-
"send pong frame": 32393.72
4+
"WebSocketConnection Performance 7359ms": {
5+
"create connection instance": 28847.63,
6+
"send small UTF-8 message": 914160.4,
7+
"send medium UTF-8 message (1KB)": 108086.21,
8+
"send binary message (1KB)": 222918.81,
9+
"send ping frame": 2375454.67,
10+
"send pong frame": 1935975.19
1111
},
12-
"WebSocketFrame Performance 10001ms": {
13-
"serialize small text frame (17 bytes, unmasked)": 4427042.23,
14-
"serialize small text frame (17 bytes, masked)": 3005215.87,
15-
"serialize medium binary frame (1KB)": 4239270.36,
16-
"serialize large binary frame (64KB)": 4024552.34
17-
},
18-
"WebSocketConnection Performance": {},
19-
"WebSocketFrame Performance": {}
12+
"WebSocketFrame Performance 9745ms": {
13+
"serialize small text frame (17 bytes, unmasked)": 4240401.67,
14+
"serialize small text frame (17 bytes, masked)": 3070532.44,
15+
"serialize medium binary frame (1KB)": 4418217.61,
16+
"serialize large binary frame (64KB)": 3918678.38
17+
}
2018
}
2119
}

test/benchmark/connection-operations.bench.mjs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,40 @@ import WebSocketConnection from '../../lib/WebSocketConnection.js';
33
import { MockSocket } from '../helpers/mocks.mjs';
44

55
describe('WebSocketConnection Performance', () => {
6+
// Pre-allocate messages and buffers outside benchmarks
7+
const smallMessage = 'Hello, WebSocket!';
8+
const mediumMessage = 'x'.repeat(1024);
9+
const binaryBuffer = Buffer.alloc(1024);
10+
11+
// Pre-create connection for send operations (created once, reused across all iterations)
12+
const sharedSocket = new MockSocket();
13+
const sharedConnection = new WebSocketConnection(sharedSocket, [], 'echo-protocol', false, {});
14+
sharedConnection._addSocketEventListeners();
15+
sharedConnection.state = 'open';
16+
617
bench('create connection instance', () => {
718
const socket = new MockSocket();
819
const connection = new WebSocketConnection(socket, [], 'echo-protocol', false, {});
920
connection._addSocketEventListeners();
1021
});
1122

1223
bench('send small UTF-8 message', () => {
13-
const socket = new MockSocket();
14-
const connection = new WebSocketConnection(socket, [], 'echo-protocol', false, {});
15-
connection._addSocketEventListeners();
16-
connection.state = 'open';
17-
connection.sendUTF('Hello, WebSocket!');
24+
sharedConnection.sendUTF(smallMessage);
1825
});
1926

2027
bench('send medium UTF-8 message (1KB)', () => {
21-
const socket = new MockSocket();
22-
const connection = new WebSocketConnection(socket, [], 'echo-protocol', false, {});
23-
connection._addSocketEventListeners();
24-
connection.state = 'open';
25-
const message = 'x'.repeat(1024);
26-
connection.sendUTF(message);
28+
sharedConnection.sendUTF(mediumMessage);
2729
});
2830

2931
bench('send binary message (1KB)', () => {
30-
const socket = new MockSocket();
31-
const connection = new WebSocketConnection(socket, [], 'echo-protocol', false, {});
32-
connection._addSocketEventListeners();
33-
connection.state = 'open';
34-
const buffer = Buffer.alloc(1024);
35-
connection.sendBytes(buffer);
32+
sharedConnection.sendBytes(binaryBuffer);
3633
});
3734

3835
bench('send ping frame', () => {
39-
const socket = new MockSocket();
40-
const connection = new WebSocketConnection(socket, [], 'echo-protocol', false, {});
41-
connection._addSocketEventListeners();
42-
connection.state = 'open';
43-
connection.ping();
36+
sharedConnection.ping();
4437
});
4538

4639
bench('send pong frame', () => {
47-
const socket = new MockSocket();
48-
const connection = new WebSocketConnection(socket, [], 'echo-protocol', false, {});
49-
connection._addSocketEventListeners();
50-
connection.state = 'open';
51-
connection.pong();
40+
sharedConnection.pong();
5241
});
5342
});

test/benchmark/track-performance.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ function parseBenchmarkOutput(output) {
3535
// Detect suite name
3636
if (line.includes('> WebSocket')) {
3737
currentSuite = line.match(/> (.*)/)[1].trim();
38-
results[currentSuite] = {};
38+
// Don't initialize suite here - wait until first benchmark is found
3939
}
4040

4141
// Parse benchmark results
4242
const benchMatch = line.match(/^\s*[·]\s+(.+?)\s+(\d+(?:,\d+)*(?:\.\d+)?)\s/);
4343
if (benchMatch && currentSuite) {
4444
const [, name, hz] = benchMatch;
45+
// Lazily initialize suite only when first benchmark is found
46+
if (!results[currentSuite]) {
47+
results[currentSuite] = {};
48+
}
4549
results[currentSuite][name.trim()] = parseFloat(hz.replace(/,/g, ''));
4650
}
4751
}

vitest.bench.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { defineConfig } from 'vitest/config';
22

33
export default defineConfig({
44
test: {
5-
include: ['test/benchmark/**/*.bench.mjs'],
65
benchmark: {
76
include: ['test/benchmark/**/*.bench.mjs'],
87
exclude: ['node_modules/', 'test/unit/', 'test/integration/'],

0 commit comments

Comments
 (0)