Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This package will create queries from your schema to use with your favorite load testing package.

License

NotificationsYou must be signed in to change notification settings

EasyGraphQL/easygraphql-load-tester

Repository files navigation

easygraphql-load-tester
easygraphql-load-tester

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

Installation

To install the package on your project just run on the root of your project

$ npm install easygraphql-load-tester --saved-dev$ yarn add easygraphql-load-tester -D

easygraphql-load-tester

Supported packages

  1. Using.artillery() with aartillery setup.
  2. Using.k6() with ak6 setup.
  3. Using.createQuery() that'll create the queries, so you can use with your favorite load tester.

How to use it?

  • Importeasygraphql-load-tester package.
  • Read the schema.
  • Initialize the tester, and pass the schema as the first argument.
    • If there are multiples schemas pass an array with the schemas an argument.
    • Note: In order to use multiples schema files, the queries and mutations must be extended. +
  • The second argument is the arguments on the queries,only if there are some of them.

One schema file

'use strict'constLoadTesting=require('easygraphql-load-tester')constfs=require('fs')constpath=require('path')constuserSchema=fs.readFileSync(path.join(__dirname,'schema','user.gql'),'utf8')constloadTester=newLoadTesting(userSchema)

Multiples schemas files

'use strict'constLoadTesting=require('easygraphql-load-tester')constfs=require('fs')constpath=require('path')constuserSchema=fs.readFileSync(path.join(__dirname,'schema','user.gql'),'utf8')constfamilySchema=fs.readFileSync(path.join(__dirname,'schema','family.gql'),'utf8')constloadTester=newLoadTesting([userSchema,familySchema])

Using GraphQL.js

'use strict'const{ GraphQLSchema, GraphQLObjectType, GraphQLString}=require('graphql')constLoadTesting=require('easygraphql-load-tester')constschema=newGraphQLSchema({query:newGraphQLObjectType({name:'RootQueryType',fields:{hello:{type:GraphQLString,resolve(){return'world'},},},}),})constloadTester=newLoadTesting(schema)

Artillery

To use withartillery, you must have it installed in your project,in case you don't have it just run:

$ npm install artillery --saved-dev

index.js

You should configure yourindex.js file:

'use strict'constfs=require('fs')constpath=require('path')constLoadTesting=require('../../lib')constfamilySchema=fs.readFileSync(path.join(__dirname,'schema.gql'),'utf8')constargs={getFamilyInfoByIsLocal:{isLocal:true,test:['a','b'],age:10,name:'test',},searchUser:{name:'demo',},createUser:{name:'demo',},createCity:{input:{name:'demo',country:'Demo',},},}consteasyGraphQLLoadTester=newLoadTesting(familySchema,args)constcustomQueries=[`    query SEARCH_USER($name: String!) {      searchUser(name: $name) {        name      }    }  `,]consttestCases=easyGraphQLLoadTester.artillery({  customQueries,onlyCustomQueries:true,queryFile:true,withMutations:true,})module.exports={  testCases,}

Artillery options

typeArtilleryOptions={customQueries?:string[]onlyCustomQueries?:booleanselectedQueries?:string[]queryFile?:booleanqueryFilePath?:stringwithMutations?:boolean}

This is optional, you can leave the second argument empty, if you don't want to pass any options

Custom queries

You can pass custom queries to test on your load test. To create them, createan array of strings (queries). You can use variables, and it's going to use thevariables defined on the arguments used to initializeLoadTesting or, you canpass the value of the argumentsearchUser(name: "demo") {...}

constcustomQueries=[`    query SEARCH_USER($name: String!) {      searchUser(name: $name) {        name      }    }  `,]

Only custom queries

If this is set totrue it's going to use the custom queries passed.

Selected queries

You can select a list of the queries you want to test, to do this, you must create anarray of strings with the name of the queries to test; this is optional, if you don'tcreate it, all the queries are going to be tested.

constselectedQueries=['getFamilyInfo','searchUser']

Query file

You can select, if you want to save ajson file with all the queries that where tested,to do it, on the options passqueryFile: true, if you don't pass anything it is not goingto be saved.

Query file path

You can select the path that you want to use to save the query file, if it's not setit'll use by defaultpath.resolve()

Mutations

You can useeasygraphql-load-tester to testyour mutations as well; to do it, on the options passwithMutations: true, if you don't pass anything it is onlygoing to test the queries.If you setwithMutations: true, don't forget to add the input values on the args

artillery.yml

The artillery file should have this minimum configuration, you can add yours in case it is needed:

config:target:'http://localhost:5000/'phases:    -duration:5arrivalRate:1processor:'./index.js'scenarios:  -name:'GraphQL Query load test'flow:      -function:'testCases'      -loop:          -post:url:'/'json:query:'{{ $loopElement.query }}'variables:'{{ $loopElement.variables }}'          -log:'----------------------------------'          -log:'Sent a request to the {{ $loopElement.operation }}: {{ $loopElement.name }}'          -log:'And variables {{ $loopElement.variables }}'over:cases

In this case the server is running onhttp://localhost:5000/

How to run it

To run your load test, add this script on yourpackage.json:

"scripts": {"easygraphql-load-tester":"artillery run artillery.yml"}

and then run on the terminal

$ npm run easygraphql-load-tester

In this case the artillery file is called artillery, but you can name yours with your favorite name and runartillery run <MY_FILE_NAME>.yml

Result

The result is going to be something like this if you apply the basic configuration

 All virtual users finished Summary report @ 15:03:05(-0500) 2018-11-17   Scenarios launched:  5   Scenarios completed: 5   Requests completed:  40   RPS sent: 8.95   Request latency:     min: 1.2     max: 13     median: 2     p95: 6     p99: 13   Scenario counts:     GraphQL Query load test: 5 (100%)   Codes:     200: 40

k6

To use withk6, you must have it installed on your computer,in case you don't have it, visit theinstallation guide

index.js

You should configure yourindex.js file:

'use strict'constfs=require('fs')constpath=require('path')constLoadTesting=require('../../lib')constfamilySchema=fs.readFileSync(path.join(__dirname,'schema.gql'),'utf8')constargs={getFamilyInfoByIsLocal:{isLocal:true,test:['a','b'],age:10,name:'test',},searchUser:{name:'demo',},}consteasyGraphQLLoadTester=newLoadTesting(familySchema,args)constqueries=[`    query SEARCH_USER($name: String!) {      searchUser(name: $name) {        name      }    }  `,]easyGraphQLLoadTester.k6('k6.js',{customQueries:queries,selectedQueries:['getFamilyInfo','searchUser'],vus:10,duration:'10s',queryFile:true,out:['json=my_test_result.json'],})

The first argument is the name of the k6 configuration file

K6 options

typeK6Options={customQueries?:string[]onlyCustomQueries?:booleanselectedQueries?:string[]queryFile?:booleanqueryFilePath?:stringwithMutations?:booleanvus?:numberduration?:stringiterations?:numberout?:string[]}

This is optional, you can leave the second argument empty, if you don't want to pass any options

Custom queries

You can pass custom queries to test on your load test. To create them, createan array of strings (queries). You can use variables, and it's going to use thevariables defined on the arguments used to initializeLoadTesting or, you canpass the value of the argumentsearchUser(name: "demo") {...}

constcustomQueries=[`    query SEARCH_USER($name: String!) {      searchUser(name: $name) {        name      }    }  `,]

Only custom queries

If this is set totrue it's going to use the custom queries passed.

Selected queries

You can select a list of the queries you want to test, to do this, you must create anarray of strings with the name of the queries to test; this is optional, if you don'tcreate it, all the queries are going to be tested.

constselectedQueries=['getFamilyInfo','searchUser']

Query file

You can select, if you want to save ajson file with all the queries that where tested,to do it, on the options passqueryFile: true, if you don't pass anything it is not goingto be saved.

Mutations

You can useeasygraphql-load-tester to testyour mutations as well; to do it, on the options passwithMutations: true, if you don't pass anything it is onlygoing to test the queries.If you setwithMutations: true, don't forget to add the input values on the args

Virtual users

You can select how many virtual users do you want for your tests, just pass to the optionsvus: <NUMBER_OF_VUS>.

Duration

You can select the duration for your tests, just pass to the optionsduration: '<DURATION>s'. It should be a string with units of the time e.g.s

Iterations

You can select the number of iterations to run by passingiterations: <ITERATIONS>. It should be an integer.

Out

You can also make k6 output detailed statistics in JSON format by using the --out/-o option for k6 run.More info

k6.js

The k6 file should have this minimum configuration, you can add yours in case it is needed:

Note: dont' change the name and the route of the queries./easygraphql-load-tester-queries.json

importhttpfrom'k6/http'constqueries=JSON.parse(open('./easygraphql-load-tester-queries.json'))exportdefaultfunction(){for(constqueryofqueries){consturl='http://localhost:5000/'constpayload=JSON.stringify({query:query.query,variables:query.variables,})constparams={headers:{'Content-Type':'application/json'}}http.post(url,payload,params)}}

In this case the server is running onhttp://localhost:5000/

How to run it

To run your load test, add this script on yourpackage.json:

"scripts": {"easygraphql-load-tester":"node index.js"}

and then run on the terminal

$ npm run easygraphql-load-tester

How to use it with your actual queries

  1. Install in your projectmerge-graphql-schemas
  2. Import all your queries:
    constqueries=fileLoader(path.join(__dirname,'..','**/*.graphql'))
  3. Pass them ascustomQueries in the options, withonlyCustomQueries: true as well.
  4. Set in the args the values to used on the variables of the queries/mutations.

Success cases

If you want to share your success case usingeasygraphql-load-testerfeel free to create aPR so the communitycan learn from your story.

Importance of using dataloaders

Some time ago I was working on a GraphQL project that includes activities andeach activity can have some comments with the info of the user that created the comment.The first thing that you might think is that it is a problem of query n + 1 , and yes; it is!

I decided to implement dataloaders but for some reason, there was an error on theimplementation, so it wasn't caching the query and the result was a lot ofrequest to the database. After finding that issue I implemented it on the rightway reducing the queries to the database from 46 to 6.

Results without dataloaders

All virtual users finishedSummary 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 finishedSummary 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

Examples

You can check theexample

License

The MIT License

Copyright (c) 2018 EasyGraphQL

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.

About

This package will create queries from your schema to use with your favorite load testing package.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors9


[8]ページ先頭

©2009-2025 Movatter.jp