Performance test for graphQL API

和自甴很熟 提交于 2020-01-01 05:49:08

问题


Today I'm doing my API automation testing and performance testing with Jmeter when the server is a REST API. Now the development changed to graphQL API, and I have two questions about it: 1. What is the best practice to preform the automation API and performance testing? 2. Does Jmeter support graphQL API?


回答1:


Disclaimer: I work for LoadImpact; the company behind k6.

If you are willing to consider an alternative, I've recently written a blog post about this topic: Load testing GraphQL with k6.

This is how a k6 example looks like:

let accessToken = "YOUR_GITHUB_ACCESS_TOKEN";

let query = `
 query FindFirstIssue {
   repository(owner:"loadimpact", name:"k6") {
     issues(first:1) {
       edges {
         node {
           id
           number
           title
         }
       }
     }
   }
 }`;

let headers = {
 'Authorization': `Bearer ${accessToken}`,
 "Content-Type": "application/json"
};

let res = http.post("https://api.github.com/graphql",
 JSON.stringify({ query: query }),
 {headers: headers}
);



回答2:


You can try using easygraphql-load-tester

How it works:

easygraphql-load-tester is a node library created to make load testing on GraphQL based on the schema; it'll create a bunch of queries, that are going to be the ones used to test your server.

Examples:

  • Artillery.io
  • K6

Result:

Using this package, it was possible to me, to identify a bad implementation using dataloaders on the server.

Results without dataloaders

All virtual users finished
Summary report @ 10:07:55(-0500) 2018-11-23
  Scenarios launched:  5
  Scenarios completed: 5
  Requests completed:  295
  RPS sent: 36.88
  Request latency:
    min: 1.6
    max: 470.9
    median: 32.9
    p95: 233.2
    p99: 410.8
  Scenario counts:
    GraphQL Query load test: 5 (100%)
  Codes:
    200: 295

Results with dataloaders

All virtual users finished
Summary report @ 10:09:09(-0500) 2018-11-23
  Scenarios launched:  5
  Scenarios completed: 5
  Requests completed:  295
  RPS sent: 65.85
  Request latency:
    min: 1.5
    max: 71.9
    median: 3.3
    p95: 19.4
    p99: 36.2
  Scenario counts:
    GraphQL Query load test: 5 (100%)
  Codes:
    200: 295



回答3:


I use Apollo to build the GraphQL server, and use JMeter to query the GraphQL API as below.

1. Set up HTTP Request

2. Set up HTTP Headers

Depending on your application, you might also need to set up HTTP header Authorization for JWT web tokens, such as:

Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxx

3. Set up HTTP Cookie if needed for your app

4. Run the test

Demo project: apollo-tutorial-kit




回答4:


Looking into Serving over HTTP section of the GraphQL documentation

When receiving an HTTP GET request, the GraphQL query should be specified in the "query" query string.

So you can just append your GraphQL query to your request URL.

With regards to "best practices" - you should follow "normal" recommendations for web applications and HTTP APIs testing, for example check out REST API Testing - How to Do it Right article.



来源:https://stackoverflow.com/questions/47009672/performance-test-for-graphql-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!