-
Notifications
You must be signed in to change notification settings - Fork 229
feat: Add k6 connection and message rate limiter tests #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| perf: added k6 testing for redis on connection and message service rate limiting | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -55,6 +55,8 @@ | |||||
| "test:load": "node -r ts-node/register ./scripts/security-load-test.ts", | ||||||
| "smoke:nip03": "node -r ts-node/register scripts/smoke-nip03.ts", | ||||||
| "test:integration": "cucumber-js", | ||||||
| "test:connection": "docker ps | grep nostream > /dev/null && k6 run test/integration/performance/connection-limiting-k6.ts || echo 'Error: nostream container not running'", | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "test:message": "docker ps | grep nostream > /dev/null && k6 run test/integration/performance/message-limiting-k6.ts || echo 'Error: nostream container not running'", | ||||||
|
cameri marked this conversation as resolved.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "cover:integration": "nyc --report-dir .coverage/integration npm run test:integration -- -p cover", | ||||||
| "export": "node --env-file-if-exists=.env -r ts-node/register src/scripts/export-events.ts", | ||||||
| "docker:compose:start": "./scripts/start", | ||||||
|
|
@@ -100,6 +102,7 @@ | |||||
| "@types/chai-as-promised": "^7.1.5", | ||||||
| "@types/express": "4.17.21", | ||||||
| "@types/js-yaml": "4.0.5", | ||||||
| "@types/k6": "^1.7.0", | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we not need k6 as well? or is it installed globally? looks like we need to update the docs as well to document this. we can add it to CONTRIBUTING.md |
||||||
| "@types/mocha": "^9.1.1", | ||||||
| "@types/node": "^24.12.2", | ||||||
| "@types/pg": "^8.6.5", | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { check, sleep } from 'k6'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's move both files to test/performance |
||
| import { Counter } from 'k6/metrics'; | ||
| import ws from 'k6/ws'; | ||
|
|
||
| const relayUrl = 'ws://127.0.0.1:8008'; | ||
| const connectionSuccess = new Counter('connection_success'); | ||
| const connectionRateLimited = new Counter('connection_rate_limited'); | ||
|
|
||
| export const options = { | ||
| stages: [ | ||
| { duration: '10s', target: 3 }, | ||
| { duration: '10s', target: 6 }, | ||
| { duration: '10s', target: 12 }, | ||
| { duration: '10s', target: 18 }, | ||
| { duration: '5s', target: 0 }, | ||
| ], | ||
| thresholds: { | ||
| 'ws_connecting': ['p(95)<2000'], | ||
| }, | ||
| }; | ||
|
|
||
| export default function () { | ||
| let socketClosed = false; | ||
|
|
||
| const res = ws.connect(relayUrl, {}, function (socket) { | ||
| socket.on('close', () => { | ||
| socketClosed = true; | ||
| connectionRateLimited.add(1); | ||
| }); | ||
|
|
||
| socket.on('open', () => { | ||
| connectionSuccess.add(1); | ||
| }); | ||
|
|
||
| socket.setTimeout(() => { | ||
| if (!socketClosed) { | ||
| socket.close(); | ||
| } | ||
| }, 3000); | ||
|
Comment on lines
+25
to
+39
|
||
| }); | ||
|
|
||
| check(res, { | ||
| 'status is 101': (r) => r && r.status === 101, | ||
| }); | ||
|
|
||
| sleep(0.5); | ||
| } | ||
|
|
||
| export function handleSummary(data: any) { | ||
| const connSuccess = data.metrics?.connection_success?.values?.count || 0; | ||
| const connRateLimited = data.metrics?.connection_rate_limited?.values?.count || 0; | ||
| const iterations = data.metrics?.iterations?.values?.count || 0; | ||
| const checks = data.metrics?.checks?.values?.passes || 0; | ||
| const wsSessions = data.metrics?.ws_sessions?.values?.count || 0; | ||
|
|
||
| const totalConnections = connSuccess + connRateLimited; | ||
| const successRate = totalConnections > 0 ? ((connSuccess / totalConnections) * 100).toFixed(2) : 0; | ||
| const rate = parseFloat(successRate as string); | ||
| const successStatus = rate >= 80 ? '✓ GOOD' : rate >= 50 ? '⚠ MODERATE' : '✗ POOR'; | ||
|
|
||
|
cameri marked this conversation as resolved.
|
||
| console.log(` | ||
| ╔════════════════════════════════════════════════════════════════╗ | ||
| ║ CONNECTION RATE LIMITER TEST RESULTS ║ | ||
| ╚════════════════════════════════════════════════════════════════╝ | ||
|
|
||
| EXECUTION: | ||
| Iterations: ${iterations} | ||
| WebSocket Sessions: ${wsSessions} | ||
| Checks Passed: ${checks} | ||
|
|
||
| CONNECTIONS: | ||
| ✓ Success (stayed open): ${connSuccess} | ||
| ✗ Rate Limited (closed): ${connRateLimited} | ||
| ───────────────────── | ||
| Total: ${totalConnections} | ||
|
|
||
| PERFORMANCE: | ||
| Success Rate: ${successStatus} ${successRate}% | ||
|
|
||
| ═══════════════════════════════════════════════════════════════════ | ||
| `); | ||
| return {}; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,106 @@ | ||||||
| import { check } from 'k6'; | ||||||
| import { Counter } from 'k6/metrics'; | ||||||
| import ws from 'k6/ws'; | ||||||
|
|
||||||
| const relayUrl = 'ws://127.0.0.1:8008'; | ||||||
| const noticeCounter = new Counter('notice_messages'); | ||||||
| const eoseCounter = new Counter('eose_messages'); | ||||||
| const eventCounter = new Counter('event_messages'); | ||||||
| const errorCounter = new Counter('error_messages'); | ||||||
|
|
||||||
| export const options = { | ||||||
| stages: [ | ||||||
| { duration: '10s', target: 1 }, | ||||||
| { duration: '10s', target: 2 }, | ||||||
| { duration: '10s', target: 4 }, | ||||||
| { duration: '5s', target: 0 }, | ||||||
| ], | ||||||
| }; | ||||||
|
|
||||||
| export default function () { | ||||||
| const res = ws.connect(relayUrl, null, function (socket) { | ||||||
|
||||||
| const res = ws.connect(relayUrl, null, function (socket) { | |
| const res = ws.connect(relayUrl, {}, function (socket) { |
Copilot
AI
Apr 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains TypeScript-only syntax (catch (e: any), data: any, successRate as string). k6 run executes JavaScript and will fail to parse TS annotations unless you add a transpilation step. Either remove TS-only syntax / use JSDoc types and rename to .js, or update the npm scripts to transpile before running k6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changeset text says this adds k6 testing "for redis". The added scripts exercise relay rate limiting over WebSocket; they don’t directly test Redis behavior, so the release note is misleading. Consider rewording to describe rate limiter load/performance tests without tying it to a specific storage backend.