Guides

GraphQL Load Testing

How do you load test or stress test your new GraphQL API? Good news - load testing a GraphQL API is very easy with LoadForge.

How do you load test or stress test your new GraphQL API? Good news - load testing a GraphQL API is very easy with LoadForge.

So much so, that we decided to run a micro (so as to not abuse their kind open API) load test against the SpaceX GraphQL API as an example.

Building a locustfile

LoadForge uses python based locustfiles to define tests, and implementing a GraphQL test is quite simple. Before we write the test though we need to define our GraphQL query.

For the SpaceX example we'll keep things simple and ask for id and name from missions. That means our query looks this like:

{
  missions {
    id
    name
  }
}

Now that we know what query we will send, we can create our full test file as below:

import time
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(3, 5)

    @task(1)
    def index_page(self):
        query = '''
            {
              missions {
                id
                name
              }
            }
            '''

        response = self.client.post(
            "/graphql",
            name="GraphQL",            
            json={"query": query }
        )          

The only two variables here are your query and the wait_time - we are simulating a wait of between 3 and 5 seconds per virtual user of the API. In LoadForge the test now looks like the below screenshot.

setup-test.png

Getting more advanced

From here you can build your test out however you wish. You may add more tasks to query different results for example, or, a common task is adding authentication.

You can change your response line to look like this to include custom headers (e.g. authorization):

    response = self.client.post(
        "/graphql",
        name="GraphQL",
        headers={
            "Accept": "application/graphql",
            "Authorization": "<Authorization-Token>"
        },
        json={"query": query }
)

You can also automate receiving an authorization token by setting an on_start declaration, like so:

    def on_start(self):
      # Send login request
      response = requests.post("http://mysite.com/login", {"username": "user", "password": "pass"})
      
      # set "token" from response header
      self.client.headers.update({'Authorization': response.headers.get('token')})

There are more examples on this in our Locustfile Directory.

Running the Test

So, how does the SpaceX GraphQL API perform? Quite well! We actually generated a very small amount of load so as not to be abusive, but here you can see the view of the test running live: running-graphql-test.png

Once the test completed we are given the results automatically in a report. Reports include detailed tables of all requests, downloadable CSVs, summary stats and graphs as shown in the two screenshots below.

report1.png

graphs.png

Last words

If you'd like to load test a GraphQL API please do Sign Up and contact us at hello@loadforge.com (or comment on the blog) for any assistance.

Ready to run your test?
LoadForge is cloud-based locust.io testing.