Load Tests as JavaScript Code
Load Testing with K6 Framework
Software University
https://softuni.bg
Svetlin Nakov, PhD
Technical Trainer and Inspirer
Co-Founder @ SoftUni
https://softuni.org
ο‚§ Software engineer, trainer, entrepreneur,
inspirer, PhD, author of 15+ technical books
ο‚§ 3 successful tech educational initiatives (150,000+ students)
About Dr. Svetlin Nakov
2
Overview
Performance Testing
3
ο‚§ Performance testing
ο‚§ Determine whether software meets speed, scalability and
stability requirements under expected workloads
ο‚§ Load testing & stress testing tools
ο‚§ Record scenarios (e. g. login οƒ  place an order οƒ  logout)
ο‚§ Execute the scenarios with multiple virtual users (e. g. 200 users)
ο‚§ Tools can be configuration-driven (JMeter, LoadRunner) or
code-driven (k6, Locust, Gatling)
Performance Testing: Overview
4
ο‚§ Load testing
ο‚§ Examine the performance for a specific expected load
ο‚§ E. g. run a scenario with 200 users in parallel for 5 minutes
ο‚§ Expect the average load time < 500 ms
ο‚§ Stress testing
ο‚§ Test app stability when running under extreme conditions
ο‚§ Goal: understand at which level the app can crash
ο‚§ E. g. 5K users for 3 minutes οƒ  check how many succeed
Performance Testing: Types
5
"K6" Load Testing Tool
Code-Based Performance Testing
ο‚§ k6 is modern, open-source load testing tool – https://k6.io
ο‚§ Testing framework based on JavaScript (very powerful)
ο‚§ Local & cloud script executor based on Go (high performance)
ο‚§ Script recorder (Chrome plugin) οƒ  generates JS code
ο‚§ Tests are plain JavaScript code
ο‚§ No XML configuration, no need for complex UI
ο‚§ Very powerful: JavaScript can test anything
ο‚§ Easy to use with continuous integration scripts
K6 Load Testing Tool
7
ο‚§ Installing k6 console app:
ο‚§ https://github.com/loadimpact/k6/releases
ο‚§ Simple testing script:
ο‚§ Running the script (just once)
K6: Install and Run
k6 run simple.js
import http from 'k6/http';
export default function () {
http.get('https://nakov-mvc-node-app.herokuapp.com');
}
simple.js
8
K6: Script Execution
9
ο‚§ Load test: 50 virtual users for 15 seconds:
ο‚§ Stress test: 3000 virtual users for 30 seconds:
ο‚§ Note this will flood most Web apps
ο‚§ Unless a DoS protection is implemented
ο‚§ Warning: flooding web apps (which you do not own) might be
illegal in some countries!
K6: Load Testing and Stress Testing
k6 --vus 50 --duration 15s run simple.js
k6 --vus 3000 --duration 30s run simple.js
10
ο‚§ k6 has test recorder (Chrome plugin)
ο‚§ Record testing scenarios (HTTP
requests) from the Web browser
ο‚§ Generates HTTP archives (HAR files)
ο‚§ Edit the recorded scripts at k6 cloud
ο‚§ Edit the scenarios in a visual editor
ο‚§ Or edit the JavaScript code behind
ο‚§ https://app.k6.io – free limited access
K6 Test Recorder
11
The K6 Cloud
12
ο‚§ Users in the real world behave like this:
ο‚§ Open a page οƒ  wait οƒ  open another page οƒ  wait οƒ  fill a form
οƒ  submit the form οƒ  wait οƒ  open another page οƒ  …
Writing K6 Scripts for Real-World Scenarios
http.get("https://nakov-mvc-node-app.herokuapp.com/");
sleep(2);
http.get("https://nakov-mvc-node-app.herokuapp.com/students");
sleep(3);
http.get("https://nakov-mvc-node-app.herokuapp.com/add-student");
sleep(3);
let body = { name: "New student" + uniqueNum, email: "new@gmail.com" };
http.post("https://nakov-mvc-node-app.herokuapp.com/add-student", body);
sleep(2);
13
ο‚§ Checks are like assertions in unit testing:
K6: Checks
import { check } from "k6";
import http from "k6/http";
export default function() {
let res = http.get("https://nakov-mvc-node-app.herokuapp.com/");
check(res, {
'HTTP status == 200': res => res.status == 200,
'Page heading': res =>
res.body.includes('<h1>Students Registry</h1>')
});
}
14
ο‚§ Thresholds define pass / fail criteria for the test
K6: Thresholds
export let options = {
thresholds: {
// 95% of requests must finish within 500 ms & 99% within 1500 ms
http_req_duration: ['p(95) < 500', 'p(99) < 1500'],
},
};
export default function performanceTest() {
…
}
15
ο‚§ Sample performance testing scenario for the "Students Registry"
app: https://nakov-mvc-node-app.herokuapp.com
1. Open the [Home] page & wait 2 secs
2. Open the [Students] page & wait 3 secs
3. Open the [Add Students] page & wait 3 secs
4. Submit the [Add Students] form & wait 2 secs
ο‚§ Code: https://gist.github.com/nakov/79f5e28a8b8c91f73c26eb55a89d80bc
K6: Testing Script with Waits Example
16
k6 --vus 200 --duration 10s run load-test-with-waits.js
ο‚§ Run k6 with
5000 virtual
users for 20
secs and see
the results:
K6: Full Performance Testing Script Example
17
Β© SoftUni – https://softuni.org

Load Testing with k6 framework

  • 1.
    Load Tests asJavaScript Code Load Testing with K6 Framework Software University https://softuni.bg Svetlin Nakov, PhD Technical Trainer and Inspirer Co-Founder @ SoftUni https://softuni.org
  • 2.
    ο‚§ Software engineer,trainer, entrepreneur, inspirer, PhD, author of 15+ technical books ο‚§ 3 successful tech educational initiatives (150,000+ students) About Dr. Svetlin Nakov 2
  • 3.
  • 4.
    ο‚§ Performance testing ο‚§Determine whether software meets speed, scalability and stability requirements under expected workloads ο‚§ Load testing & stress testing tools ο‚§ Record scenarios (e. g. login οƒ  place an order οƒ  logout) ο‚§ Execute the scenarios with multiple virtual users (e. g. 200 users) ο‚§ Tools can be configuration-driven (JMeter, LoadRunner) or code-driven (k6, Locust, Gatling) Performance Testing: Overview 4
  • 5.
    ο‚§ Load testing ο‚§Examine the performance for a specific expected load ο‚§ E. g. run a scenario with 200 users in parallel for 5 minutes ο‚§ Expect the average load time < 500 ms ο‚§ Stress testing ο‚§ Test app stability when running under extreme conditions ο‚§ Goal: understand at which level the app can crash ο‚§ E. g. 5K users for 3 minutes οƒ  check how many succeed Performance Testing: Types 5
  • 6.
    "K6" Load TestingTool Code-Based Performance Testing
  • 7.
    ο‚§ k6 ismodern, open-source load testing tool – https://k6.io ο‚§ Testing framework based on JavaScript (very powerful) ο‚§ Local & cloud script executor based on Go (high performance) ο‚§ Script recorder (Chrome plugin) οƒ  generates JS code ο‚§ Tests are plain JavaScript code ο‚§ No XML configuration, no need for complex UI ο‚§ Very powerful: JavaScript can test anything ο‚§ Easy to use with continuous integration scripts K6 Load Testing Tool 7
  • 8.
    ο‚§ Installing k6console app: ο‚§ https://github.com/loadimpact/k6/releases ο‚§ Simple testing script: ο‚§ Running the script (just once) K6: Install and Run k6 run simple.js import http from 'k6/http'; export default function () { http.get('https://nakov-mvc-node-app.herokuapp.com'); } simple.js 8
  • 9.
  • 10.
    ο‚§ Load test:50 virtual users for 15 seconds: ο‚§ Stress test: 3000 virtual users for 30 seconds: ο‚§ Note this will flood most Web apps ο‚§ Unless a DoS protection is implemented ο‚§ Warning: flooding web apps (which you do not own) might be illegal in some countries! K6: Load Testing and Stress Testing k6 --vus 50 --duration 15s run simple.js k6 --vus 3000 --duration 30s run simple.js 10
  • 11.
    ο‚§ k6 hastest recorder (Chrome plugin) ο‚§ Record testing scenarios (HTTP requests) from the Web browser ο‚§ Generates HTTP archives (HAR files) ο‚§ Edit the recorded scripts at k6 cloud ο‚§ Edit the scenarios in a visual editor ο‚§ Or edit the JavaScript code behind ο‚§ https://app.k6.io – free limited access K6 Test Recorder 11
  • 12.
  • 13.
    ο‚§ Users inthe real world behave like this: ο‚§ Open a page οƒ  wait οƒ  open another page οƒ  wait οƒ  fill a form οƒ  submit the form οƒ  wait οƒ  open another page οƒ  … Writing K6 Scripts for Real-World Scenarios http.get("https://nakov-mvc-node-app.herokuapp.com/"); sleep(2); http.get("https://nakov-mvc-node-app.herokuapp.com/students"); sleep(3); http.get("https://nakov-mvc-node-app.herokuapp.com/add-student"); sleep(3); let body = { name: "New student" + uniqueNum, email: "new@gmail.com" }; http.post("https://nakov-mvc-node-app.herokuapp.com/add-student", body); sleep(2); 13
  • 14.
    ο‚§ Checks arelike assertions in unit testing: K6: Checks import { check } from "k6"; import http from "k6/http"; export default function() { let res = http.get("https://nakov-mvc-node-app.herokuapp.com/"); check(res, { 'HTTP status == 200': res => res.status == 200, 'Page heading': res => res.body.includes('<h1>Students Registry</h1>') }); } 14
  • 15.
    ο‚§ Thresholds definepass / fail criteria for the test K6: Thresholds export let options = { thresholds: { // 95% of requests must finish within 500 ms & 99% within 1500 ms http_req_duration: ['p(95) < 500', 'p(99) < 1500'], }, }; export default function performanceTest() { … } 15
  • 16.
    ο‚§ Sample performancetesting scenario for the "Students Registry" app: https://nakov-mvc-node-app.herokuapp.com 1. Open the [Home] page & wait 2 secs 2. Open the [Students] page & wait 3 secs 3. Open the [Add Students] page & wait 3 secs 4. Submit the [Add Students] form & wait 2 secs ο‚§ Code: https://gist.github.com/nakov/79f5e28a8b8c91f73c26eb55a89d80bc K6: Testing Script with Waits Example 16 k6 --vus 200 --duration 10s run load-test-with-waits.js
  • 17.
    ο‚§ Run k6with 5000 virtual users for 20 secs and see the results: K6: Full Performance Testing Script Example 17
  • 18.
    Β© SoftUni –https://softuni.org