-
Notifications
You must be signed in to change notification settings - Fork 232
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
Changes from all commits
de504f8
449acf6
40e41d1
796f1ca
0a33009
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 performance tests for connection and message 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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { check, sleep } from 'k6'; | ||
|
saniddhyaDubey marked this conversation as resolved.
|
||
| import { Counter } from 'k6/metrics'; | ||
| import ws from 'k6/ws'; | ||
|
|
||
| const relayUrl = __ENV.RELAY_URL || '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 () { | ||
|
|
||
| const res = ws.connect(relayUrl, {}, function (socket) { | ||
| let intentionalClose = false | ||
| socket.on('close', () => { | ||
| if(!intentionalClose) { | ||
| connectionRateLimited.add(1); | ||
| } | ||
| }); | ||
|
|
||
| socket.on('open', () => { | ||
| connectionSuccess.add(1); | ||
| }); | ||
|
|
||
| socket.setTimeout(() => { | ||
| intentionalClose = true; | ||
| socket.close(); | ||
| }, 3000); | ||
|
Comment on lines
+24
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 = __ENV.RELAY_URL || '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, {}, function (socket) { | ||
| socket.on('open', function () { | ||
| let msgCount = 0; | ||
| socket.setInterval(function () { | ||
| msgCount++; | ||
| const text = JSON.stringify(['REQ', `sub-${Date.now()}-${msgCount}`, {limit: 10}]); | ||
| socket.send(text); | ||
| }, 1000); | ||
| }); | ||
|
|
||
| socket.on('message', function (data) { | ||
| try { | ||
| const parsed = JSON.parse(data); | ||
| const msgType = parsed[0]; | ||
|
|
||
| if (msgType === 'NOTICE') { | ||
| noticeCounter.add(1); | ||
| } else if (msgType === 'EOSE') { | ||
| eoseCounter.add(1); | ||
| } else if (msgType === 'EVENT') { | ||
| eventCounter.add(1); | ||
| } | ||
| } catch (e: any) { | ||
| errorCounter.add(1); | ||
| console.error('Failed to parse message:', e.message); | ||
| } | ||
| }); | ||
|
|
||
| socket.setTimeout(function () { | ||
| socket.close(); | ||
| }, 9000); | ||
| }); | ||
|
|
||
| check(res, { | ||
| 'status 101': (r) => r && r.status === 101, | ||
| }); | ||
| } | ||
|
|
||
| export function handleSummary(data: any) { | ||
| const notices = data.metrics?.notice_messages?.values?.count || 0; | ||
| const eoses = data.metrics?.eose_messages?.values?.count || 0; | ||
| const events = data.metrics?.event_messages?.values?.count || 0; | ||
| const iterations = data.metrics?.iterations?.values?.count || 0; | ||
| const wsSessions = data.metrics?.ws_sessions?.values?.count || 0; | ||
| const msgsSent = data.metrics?.ws_msgs_sent?.values?.count || 0; | ||
| const msgsReceived = data.metrics?.ws_msgs_received?.values?.count || 0; | ||
| const dataReceived = data.metrics?.data_received?.values?.count || 0; | ||
| const checks = data.metrics?.checks?.values?.passes || 0; | ||
|
|
||
| const totalMessages = notices + eoses + events; | ||
| const successRate = totalMessages > 0 ? ((eoses + events) / totalMessages * 100).toFixed(2) : 0; | ||
|
|
||
| const rate = parseFloat(successRate as string); | ||
| const successStatus = rate >= 80 ? '✓ GOOD' : rate >= 50 ? '⚠ MODERATE' : '✗ POOR'; | ||
| const rateLimitStatus = notices > 0 ? '⚠ ACTIVE' : '✓ INACTIVE'; | ||
|
Comment on lines
+43
to
+75
|
||
|
|
||
| console.log(` | ||
| ╔════════════════════════════════════════════════════════════════╗ | ||
| ║ MESSAGE RATE LIMITER TEST RESULTS ║ | ||
| ╚════════════════════════════════════════════════════════════════╝ | ||
|
|
||
| EXECUTION: | ||
| Iterations: ${iterations} | ||
| WebSocket Sessions: ${wsSessions} | ||
| Checks Passed: ${checks} | ||
|
|
||
| MESSAGES: | ||
| Sent: ${msgsSent} | ||
| Received: ${msgsReceived} | ||
|
|
||
| MESSAGE TYPES: | ||
| ✗ NOTICE (rate limited): ${notices} | ||
| ✓ EOSE (query complete): ${eoses} | ||
| ◆ EVENT (results): ${events} | ||
| ───────────────────── | ||
| Total: ${totalMessages} | ||
|
|
||
| PERFORMANCE: | ||
| Success Rate: ${successStatus} ${successRate}% | ||
| Data Received: ${dataReceived} bytes | ||
| Rate Limiter: ${rateLimitStatus} | ||
|
|
||
| ═══════════════════════════════════════════════════════════════════ | ||
| `); | ||
| return {}; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.