Skip to content
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

Replace legacy testing services from README and examples #4517

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use tabs
  • Loading branch information
federicotdn committed Feb 6, 2025
commit af00d3bcc8900fde4ff32b14dd804cb9b0853b49
96 changes: 48 additions & 48 deletions examples/experimental/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@ import { WebSocket } from 'k6/experimental/websockets';
const sessionDuration = randomIntBetween(1000, 3000); // user session between 1s and 3s

export default function () {
for (let i = 0; i < 4; i++) {
startWSWorker(i);
}
for (let i = 0; i < 4; i++) {
startWSWorker(i);
}
}

function startWSWorker(id) {
// create a new websocket connection
const ws = new WebSocket(`wss://quickpizza.grafana.com/ws`);
ws.binaryType = 'arraybuffer';

ws.addEventListener('open', () => {
// change the user name
ws.send(JSON.stringify({ event: 'SET_NAME', new_name: `VU ${__VU}:${id}` }));

// listen for messages/errors and log them into console
ws.addEventListener('message', (e) => {
const 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}`);
}
});

// send a message every 2-8 seconds
const intervalId = setInterval(() => {
ws.send(JSON.stringify({ event: 'SAY', message: `I'm saying ${randomString(5)}` }));
}, randomIntBetween(2000, 8000)); // say something every 2-8 seconds

// after a sessionDuration stop sending messages and leave the room
const timeout1id = setTimeout(function () {
clearInterval(intervalId);
console.log(`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`);
ws.send(JSON.stringify({ event: 'LEAVE' }));
}, sessionDuration);

// after a sessionDuration + 3s close the connection
const timeout2id = setTimeout(function () {
console.log(`Closing the socket forcefully 3s after graceful LEAVE`);
ws.close();
}, sessionDuration + 3000);

// when connection is closing, clean up the previously created timers
ws.addEventListener('close', () => {
clearTimeout(timeout1id);
clearTimeout(timeout2id);
console.log(`VU ${__VU}:${id}: disconnected`);
});
});
// create a new websocket connection
const ws = new WebSocket(`wss://quickpizza.grafana.com/ws`);
ws.binaryType = 'arraybuffer';

ws.addEventListener('open', () => {
// change the user name
ws.send(JSON.stringify({ event: 'SET_NAME', new_name: `VU ${__VU}:${id}` }));

// listen for messages/errors and log them into console
ws.addEventListener('message', (e) => {
const 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}`);
}
});

// send a message every 2-8 seconds
const intervalId = setInterval(() => {
ws.send(JSON.stringify({ event: 'SAY', message: `I'm saying ${randomString(5)}` }));
}, randomIntBetween(2000, 8000)); // say something every 2-8 seconds

// after a sessionDuration stop sending messages and leave the room
const timeout1id = setTimeout(function () {
clearInterval(intervalId);
console.log(`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`);
ws.send(JSON.stringify({ event: 'LEAVE' }));
}, sessionDuration);

// after a sessionDuration + 3s close the connection
const timeout2id = setTimeout(function () {
console.log(`Closing the socket forcefully 3s after graceful LEAVE`);
ws.close();
}, sessionDuration + 3000);

// when connection is closing, clean up the previously created timers
ws.addEventListener('close', () => {
clearTimeout(timeout1id);
clearTimeout(timeout2id);
console.log(`VU ${__VU}:${id}: disconnected`);
});
});
}
Loading