SlideShare a Scribd company logo
4
Most read
7
Most read
8
Most read
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

More Related Content

PDF
Introduction to K6
PDF
Running distributed tests with k6.pdf
PDF
테스트자동화 성공전략
PDF
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
PPT
Performance testing and reporting with JMeter
PPTX
Service mesh
PDF
소프트웨어 테스팅
PPTX
Mind Management & Mind Management Tools and Techniques
Introduction to K6
Running distributed tests with k6.pdf
테스트자동화 성공전략
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
Performance testing and reporting with JMeter
Service mesh
소프트웨어 테스팅
Mind Management & Mind Management Tools and Techniques

What's hot (20)

PDF
Test Automation - Keytorc Approach
PPT
Postman.ppt
PPTX
Jenkins CI
PPSX
CI-CD Jenkins, GitHub Actions, Tekton
PPTX
SonarQube: Continuous Code Inspection
PPT
Performance testing with Jmeter
PDF
Distributed Load Testing with k6 - DevOps Barcelona
PDF
Performance testing presentation
PPTX
Api testing
PPT
Code coverage
PPT
Jenkins Overview
PDF
API TESTING
PDF
Angular Observables & RxJS Introduction
PDF
Introduction to jest
PPTX
REST & RESTful Web Services
PDF
Jenkins tutorial
PDF
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
PDF
PPTX
What is an API Gateway?
PPTX
Rest assured
Test Automation - Keytorc Approach
Postman.ppt
Jenkins CI
CI-CD Jenkins, GitHub Actions, Tekton
SonarQube: Continuous Code Inspection
Performance testing with Jmeter
Distributed Load Testing with k6 - DevOps Barcelona
Performance testing presentation
Api testing
Code coverage
Jenkins Overview
API TESTING
Angular Observables & RxJS Introduction
Introduction to jest
REST & RESTful Web Services
Jenkins tutorial
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
What is an API Gateway?
Rest assured
Ad

Similar to Load Testing with k6 framework (20)

PDF
Integration tests: use the containers, Luke!
PDF
Serverless Java on Kubernetes
PDF
Release with confidence
PDF
Maxim Salnikov - Service Worker: taking the best from the past experience for...
PDF
Create a Full-Stack Web App with React & Node.js in 2024
PDF
Highlights from Java 10-13 and Future of Java at JCON 2019 by Alukhanov and K...
ODP
RichFaces - Testing on Mobile Devices
PPTX
OWASP ZAP Workshop for QA Testers
PPTX
LoadUI web performance testing tool
PPTX
Load ui web Load Testing Open source Performance Testing tool Learn Load UI W...
PDF
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
PPTX
Jenkins days workshop pipelines - Eric Long
PDF
JCConf 2016 - Dataflow Workshop Labs
PDF
PDF
Selenium RC: Automated Testing of Modern Web Applications
PPTX
Oracle appsloadtestbestpractices
PPT
Java 6 [Mustang] - Features and Enchantments
PDF
Using JHipster for generating Angular/Spring Boot apps
PDF
Parse cloud code
PDF
Load impact insights webinar
Integration tests: use the containers, Luke!
Serverless Java on Kubernetes
Release with confidence
Maxim Salnikov - Service Worker: taking the best from the past experience for...
Create a Full-Stack Web App with React & Node.js in 2024
Highlights from Java 10-13 and Future of Java at JCON 2019 by Alukhanov and K...
RichFaces - Testing on Mobile Devices
OWASP ZAP Workshop for QA Testers
LoadUI web performance testing tool
Load ui web Load Testing Open source Performance Testing tool Learn Load UI W...
Highlights from Java 10, 11 and 12 and Future of Java at Javaland 2019 By Vad...
Jenkins days workshop pipelines - Eric Long
JCConf 2016 - Dataflow Workshop Labs
Selenium RC: Automated Testing of Modern Web Applications
Oracle appsloadtestbestpractices
Java 6 [Mustang] - Features and Enchantments
Using JHipster for generating Angular/Spring Boot apps
Parse cloud code
Load impact insights webinar
Ad

More from Svetlin Nakov (20)

PPTX
AI and the Future of Devs: Nakov @ Techniverse (Nov 2024)
PPTX
AI за ежедневието - Наков @ Techniverse (Nov 2024)
PPTX
AI инструменти за бизнеса - Наков - Nov 2024
PPTX
AI Adoption in Business - Nakov at Forbes HR Forum - Sept 2024
PPTX
Software Engineers in the AI Era - Sept 2024
PPTX
Най-търсените направления в ИТ сферата за 2024
PPTX
BG-IT-Edu: отворено учебно съдържание за ИТ учители
PPTX
Programming World in 2024
PDF
AI Tools for Business and Startups
PPTX
AI Tools for Scientists - Nakov (Oct 2023)
PPTX
AI Tools for Entrepreneurs
PPTX
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
PPTX
AI Tools for Business and Personal Life
PDF
Дипломна работа: учебно съдържание по ООП - Светлин Наков
PPTX
Дипломна работа: учебно съдържание по ООП
PPTX
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
PPTX
AI and the Professions of the Future
PPTX
Programming Languages Trends for 2023
PPTX
IT Professions and How to Become a Developer
PPTX
GitHub Actions (Nakov at RuseConf, Sept 2022)
AI and the Future of Devs: Nakov @ Techniverse (Nov 2024)
AI за ежедневието - Наков @ Techniverse (Nov 2024)
AI инструменти за бизнеса - Наков - Nov 2024
AI Adoption in Business - Nakov at Forbes HR Forum - Sept 2024
Software Engineers in the AI Era - Sept 2024
Най-търсените направления в ИТ сферата за 2024
BG-IT-Edu: отворено учебно съдържание за ИТ учители
Programming World in 2024
AI Tools for Business and Startups
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Entrepreneurs
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
AI Tools for Business and Personal Life
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
AI and the Professions of the Future
Programming Languages Trends for 2023
IT Professions and How to Become a Developer
GitHub Actions (Nakov at RuseConf, Sept 2022)

Recently uploaded (20)

DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Pre independence Education in Inndia.pdf
PDF
From loneliness to social connection charting
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Module 3: Health Systems Tutorial Slides S2 2025
UPPER GASTRO INTESTINAL DISORDER.docx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Cardiovascular Pharmacology for pharmacy students.pptx
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Pre independence Education in Inndia.pdf
From loneliness to social connection charting
How to Manage Starshipit in Odoo 18 - Odoo Slides
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Introduction and Scope of Bichemistry.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Insiders guide to clinical Medicine.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Module 3: Health Systems Tutorial Slides S2 2025

Load Testing with k6 framework

  • 1. 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
  • 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
  • 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 Testing Tool Code-Based Performance Testing
  • 7.  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
  • 8.  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
  • 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 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
  • 13.  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: "[email protected]" }; http.post("https://nakov-mvc-node-app.herokuapp.com/add-student", body); sleep(2); 13
  • 14.  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
  • 15.  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
  • 16.  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
  • 17.  Run k6 with 5000 virtual users for 20 secs and see the results: K6: Full Performance Testing Script Example 17
  • 18. © SoftUni – https://softuni.org