-
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.
Merge pull request #4131 from grafana/websocketsMerge
Experimental Websockets merge
- Loading branch information
Showing
21 changed files
with
2,679 additions
and
671 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
25 changes: 25 additions & 0 deletions
25
js/modules/k6/experimental/websockets/autobahn_tests/README.md
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,25 @@ | ||
# Autobahn test suite | ||
|
||
Docs: https://github.com/crossbario/autobahn-testsuite | ||
|
||
## Usage | ||
|
||
Run the WebSocket server. | ||
|
||
```sh | ||
$ docker run -it --rm \ | ||
-v ${PWD}/config:/config \ | ||
-v ${PWD}/reports:/reports \ | ||
-p 9001:9001 \ | ||
-p 8080:8080 \ | ||
--name fuzzingserver \ | ||
crossbario/autobahn-testsuite | ||
``` | ||
|
||
Run the autobahn client test with k6. | ||
|
||
```sh | ||
./k6 run ./script.js | ||
``` | ||
|
||
Open the browser to `http://localhost:8080` for checking the report. |
11 changes: 11 additions & 0 deletions
11
js/modules/k6/experimental/websockets/autobahn_tests/config/fuzzingserver.json
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,11 @@ | ||
{ | ||
"url": "ws://127.0.0.1:9001", | ||
"outdir": "./reports/clients", | ||
"cases": ["*"], | ||
"exclude-cases": [ | ||
"9.*", | ||
"12.*", | ||
"13.*" | ||
], | ||
"exclude-agent-cases": {} | ||
} |
Empty file.
46 changes: 46 additions & 0 deletions
46
js/modules/k6/experimental/websockets/autobahn_tests/script.js
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,46 @@ | ||
import { WebSocket } from "k6/x/websockets" | ||
import { sleep, check } from "k6" | ||
import exec from 'k6/execution' | ||
|
||
export let options = { | ||
iterations: 247, // get this value from the Autobahn server | ||
vus: 3, | ||
} | ||
|
||
const base = `ws://127.0.0.1:9001` | ||
const agent = "k6v383" | ||
|
||
export default function() { | ||
let testCase = exec.scenario.iterationInTest+1 | ||
let url = `${base}/runCase?case=${testCase}&agent=${agent}`; | ||
let ws = new WebSocket(url); | ||
|
||
ws.addEventListener("open", () => { | ||
console.log(`Testing case #${testCase}`) | ||
}); | ||
|
||
ws.addEventListener("message", (e) => { | ||
if (e.event === 'ERROR') { | ||
console.log(`VU ${__VU}: test: #${testCase} error:`, e.data, `and message:`, e.message) | ||
return | ||
} | ||
ws.send(e.data) | ||
}) | ||
|
||
ws.addEventListener("error", (e) => { | ||
console.error(`test: #${testCase} error:`, e) | ||
ws.close() | ||
}) | ||
} | ||
|
||
export function teardown() { | ||
let ws = new WebSocket(`${base}/updateReports?agent=${agent}`) | ||
ws.addEventListener("open", (e) => { | ||
console.log("Updating the report") | ||
}); | ||
|
||
ws.addEventListener("error", (e) => { | ||
console.error("Updating the report failed:", e) | ||
ws.close() | ||
}); | ||
} |
File renamed without changes.
Oops, something went wrong.