Skip to content

Instantly share code, notes, and snippets.

@mehran-prs
Last active April 13, 2022 18:43
Show Gist options
  • Save mehran-prs/63135c6c6090e26191f91e70f29f0297 to your computer and use it in GitHub Desktop.
Save mehran-prs/63135c6c6090e26191f91e70f29f0297 to your computer and use it in GitHub Desktop.
K6 tests
  • Install k6
  • Write following js file (for example):
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
 thresholds: {
       http_req_duration: ['p(99)<1500'], // 99% of requests must complete below 1.5s
 },
};

export default function () {
 http.get('http://test.k6.io');
 // Run other requests and check responses (or just response coce) using check function.
 // You can add sleep even between your requests if needed.

 sleep(1);
}
  • Run your tests:
# For first time run with log to check requests and responses
k6 --http-debug my-script.js

# or even log request/response body
k6 --http-debug=full my-script.js

# Then if everything is ok, so run with more virtual-users and a duration
k6 --http-debug --vus 100 --duration 120s  my-script.js
  • Finally run with stages config (add stages section to options section of your script)
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
  thresholds: {
        http_req_duration: ['p(99)<1500'], // 99% of requests must complete below 1.5s
  },
   stages: [
                {duration: '1m', target: 100},
                {duration: '2m', target: 150},
                {duration: '1m', target: 150},
                {duration: '2m', target: 250},
                {duration: '1m', target: 100},
                {duration: '1m', target: 0},
   ],
};

export default function () {
  http.get('http://test.k6.io');
  // Run other requests and check responses (or just response coce) using check functions.
  // You can add sleep even between each request if needed.

  sleep(1);
}
  • Run your test to run with the specified stages:
k6 my-script.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment