-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix k6/experimental/websockets after merge of code
- Loading branch information
Showing
41 changed files
with
759 additions
and
3,323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { randomString, randomIntBetween } from "https://jslib.k6.io/k6-utils/1.1.0/index.js"; | ||
import { WebSocket } from "k6/experimental/websockets" | ||
|
||
let chatRoomName = 'publicRoom'; // choose your chat room name | ||
let sessionDuration = randomIntBetween(5000, 60000); // user session between 5s and 1m | ||
|
||
|
||
export default function() { | ||
for (let i = 0; i < 4; i++) { | ||
startWSWorker(i) | ||
} | ||
} | ||
|
||
function startWSWorker(id) { | ||
let url = `wss://test-api.k6.io/ws/crocochat/${chatRoomName}/`; | ||
let ws = new WebSocket(url); | ||
ws.binaryType = "arraybuffer"; | ||
ws.addEventListener("open", () => { | ||
ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}:${id}` })); | ||
|
||
ws.addEventListener("message", (e) => { | ||
let msg = JSON.parse(e.data); | ||
if (msg.event === 'CHAT_MSG') { | ||
console.log(`VU ${__VU}:${id} received: ${msg.user} says: ${msg.message}`) | ||
} | ||
else if (msg.event === 'ERROR') { | ||
console.error(`VU ${__VU}:${id} received:: ${msg.message}`) | ||
} | ||
else { | ||
console.log(`VU ${__VU}:${id} received unhandled message: ${msg.message}`) | ||
} | ||
}) | ||
|
||
|
||
let intervalId = setInterval(() => { | ||
ws.send(JSON.stringify({ 'event': 'SAY', 'message': `I'm saying ${randomString(5)}` })); | ||
}, randomIntBetween(2000, 8000)); // say something every 2-8seconds | ||
|
||
|
||
let timeout1id = setTimeout(function() { | ||
clearInterval(intervalId) | ||
console.log(`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`); | ||
ws.send(JSON.stringify({ 'event': 'LEAVE' })); | ||
}, sessionDuration); | ||
|
||
let timeout2id = setTimeout(function() { | ||
console.log(`Closing the socket forcefully 3s after graceful LEAVE`); | ||
ws.close(); | ||
}, sessionDuration + 3000); | ||
|
||
ws.addEventListener("close", () => { | ||
clearTimeout(timeout1id); | ||
clearTimeout(timeout2id); | ||
console.log(`VU ${__VU}:${id}: disconnected`); | ||
}) | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { WebSocket } from "k6/experimental/websockets" | ||
|
||
const CLOSED_STATE = 3 | ||
|
||
export default function() { | ||
// local echo server should be launched with `make ws-echo-server-run` | ||
var url = "wss://echo.websocket.org/" | ||
var params = { "tags": { "my_tag": "hello" } }; | ||
|
||
let ws = new WebSocket(url, null, params) | ||
ws.binaryType = "arraybuffer"; | ||
ws.onopen = () => { | ||
console.log('connected') | ||
ws.send(Date.now().toString()) | ||
} | ||
|
||
let intervalId = setInterval(() => { | ||
ws.ping(); | ||
console.log("Pinging every 1 sec (setInterval test)") | ||
}, 1000); | ||
|
||
let timeout1id = setTimeout(function() { | ||
console.log('2 seconds passed, closing the socket') | ||
clearInterval(intervalId) | ||
ws.close() | ||
|
||
}, 2000); | ||
|
||
ws.onclose = () => { | ||
clearTimeout(timeout1id); | ||
|
||
console.log('disconnected') | ||
} | ||
|
||
|
||
ws.onping = () => { | ||
console.log("PING!") | ||
} | ||
|
||
ws.onpong = () => { | ||
console.log("PONG!") | ||
} | ||
|
||
// Multiple event handlers on the same event | ||
ws.addEventListener("pong", () => { | ||
console.log("OTHER PONG!") | ||
}) | ||
|
||
ws.onmessage = (m) => { | ||
let parsed = parseInt(m.data, 10) | ||
if (Number.isNaN(parsed)) { | ||
console.log('Not a number received: ', m.data) | ||
|
||
return | ||
} | ||
|
||
console.log(`Roundtrip time: ${Date.now() - parsed} ms`); | ||
|
||
let timeoutId = setTimeout(function() { | ||
if (ws.readyState == CLOSED_STATE) { | ||
console.log("Socket closed, not sending anything"); | ||
|
||
clearTimeout(timeoutId); | ||
return; | ||
} | ||
|
||
ws.send(Date.now().toString()) | ||
}, 500); | ||
} | ||
|
||
ws.onerror = (e) => { | ||
if (e.error != "websocket: close sent") { | ||
console.log('An unexpected error occurred: ', e.error); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
js/modules/k6/experimental/websockets/.github/pull_request_template.md
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
js/modules/k6/experimental/websockets/.github/workflows/all.yml
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
js/modules/k6/experimental/websockets/.github/workflows/issue-auto-assign.yml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.