Skip to content

Instantly share code, notes, and snippets.

@pyropy
Created March 15, 2024 17:25
Show Gist options
  • Save pyropy/0f3f2f41055f5c778ca56986f9cfa2dc to your computer and use it in GitHub Desktop.
Save pyropy/0f3f2f41055f5c778ca56986f9cfa2dc to your computer and use it in GitHub Desktop.

Revisions

  1. pyropy created this gist Mar 15, 2024.
    71 changes: 71 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    import { check } from "k6";
    import ws from "k6/ws";

    export const options = {
    // Key configurations for avg load test in this section
    stages: [
    { duration: "30s", target: 200 }, // traffic ramp-up from 1 to 100 users over 30 seconds.
    { duration: "1m", target: 200 }, // keep traffic at 100 users over 1 minute.
    { duration: "30s", target: 0 }, // traffic ramp-up from 1 to 100 users over 30 seconds.
    ],
    };

    export default function () {
    const url = "ws://localhost:8080/subscriptions";
    const token = null; // replace with your auth token
    const operation = `
    subscription {
    EVM(network: eth, mempool: true) {
    Transactions {
    count
    }
    }
    }`;
    const headers = {
    "Sec-WebSocket-Protocol": "graphql-ws",
    };

    if (token != null)
    Object.assign(headers, { Authorization: `Bearer ${token}` });

    const res = ws.connect(
    url,
    {
    headers,
    },
    (socket) => {
    socket.on("message", (msg) => {
    const message = JSON.parse(msg);
    if (message.type == "connection_ack")
    console.log("Connection Established with WebSocket");
    if (message.type == "data") console.log(`Message Received: ${message}`);
    });

    socket.on("error", (e) => {
    console.error(e);
    });

    socket.on("open", () => {
    socket.send(
    JSON.stringify({
    type: "connection_init",
    payload: {},
    })
    );
    socket.send(
    JSON.stringify({
    type: "start",
    payload: {
    query: operation,
    variables: {},
    extensions: {},
    operationName: null,
    },
    })
    );
    });
    }
    );

    check(res, { "Connected successfully": (r) => r && r.status === 101 });
    }