Skip to content

Instantly share code, notes, and snippets.

@hexfusion
Created March 16, 2022 18:26
Show Gist options
  • Select an option

  • Save hexfusion/70d75ef1f1572546e09de74d8bde0429 to your computer and use it in GitHub Desktop.

Select an option

Save hexfusion/70d75ef1f1572546e09de74d8bde0429 to your computer and use it in GitHub Desktop.
import ws from 'k6/ws';
import { check } from 'k6';
export let options = {
stages: [
{ duration: '2m', target: 500 },
{ duration: '30s', target: 0 },
{ duration: '2m', target: 500 },
{ duration: '30s', target: 0 },
],
};
export default function () {
const text = '{"id": 1, "method": "eth_subscribe", "params": ["newHeads"]}';
const resp = '{"jsonrpc":"2.0","id":1,"result":"0x2b67"}'
const url = 'wss://1.2.3.4:9650/ext/bc/2ebCneCbwthjQ1rYT41nhd7M76Hc6YmosMAQrTFhBq8qeqh6tt/ws';
const res = ws.connect(url, null, function (socket) {
socket.on('open', function open() {
console.log('connected');
socket.setInterval(function interval() {
socket.send(text);
console.log('Message sent: ', text);
}, 1000);
});
socket.on('message', function message(data) {
console.log('Message received: ', data);
check(data, { 'data is correct': (r) => r && r != '' });
});
socket.on('close', () => console.log('disconnected'));
socket.setTimeout(function () {
console.log('10 seconds passed, closing the socket');
socket.close();
}, 10000);
});
check(res, { 'status is 101': (r) => r && r.status === 101 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment