← Load Test Directory

Load Testing GraphQL Subscriptions

Guide on load testing real-time GraphQL subscriptions with LoadForge using Locust.

You are now browsing the LoadForge locust test directory. You can use these tests as a starting point for your own tests, or use our AI wizard to generate one automatically.

World

Overview

GraphQL subscriptions provide real-time data streams over WebSockets. LoadForge supports subscription load testing by leveraging Locust and the WebSocketUser from locust-plugins.

Locust Test Script (locust.py)

# locust.py
import json
from locust import between, task
from locust_plugins.users import WebSocketUser

class GraphqlSubsUser(WebSocketUser):
    wait_time = between(1, 3)
    host = "wss://api.yourdomain.com/graphql"

    def on_start(self):
        # Open WebSocket connection
        self.connect("/graphql")

        # Initialize the subscription
        init_msg = {"type": "connection_init", "payload": {}}
        self.send(json.dumps(init_msg))
        self.receive()

        # Start subscription
        sub_msg = {
            "id": "1",
            "type": "start",
            "payload": {"query": "subscription { newMessage { content sender } }"}
        }
        self.send(json.dumps(sub_msg))

    @task
    def listen(self):
        # Receive a subscription event
        event = self.receive()
        data = json.loads(event)
        print(data)

Notes:

  • Install dependencies: pip install locust locust-plugins websockets if you wish to test locally.
  • Replace host and GraphQL query with your subscription endpoint.

Ready to run your test?
Start your first test within minutes.