What is Performance Testing?
Have you ever wanted to see the quality and load capacity of your products but you didn’t know how? Or do you have an API or a certain feature that when you request with Postman, it’s still good, but when you put it into operation, it crashes continuously or it takes a long time to load, making the user experience bad?
So let’s learn about Performance Testing and tools to do it.
Performance Testing is a testing technique to determine the bandwidth, processing capacity, scalability, or generally the performance of the system under the workload defined by the work. The results of performance testing serve to investigate, measure and evaluate the real performance of the system. There are common types such as:
Along with that, there are also many supporting tools such as Jmeter, Grinder, Artillery, LoadComplete, … But here, let’s learn about K6 .
Install K6?
K6 supports packages for Linux, Mac and Windows operating systems. Alternatively, you can use Docker.
Linux
Debian/Ubuntu
1 2 3 4 5 | sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69 echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list sudo apt-get update sudo apt-get install k6 |
Fedora/CentOS
Use dnf or yum on older versions
1 2 3 | sudo dnf install https://dl.k6.io/rpm/repo.rpm sudo dnf install k6 |
Mac
Using Homebrew
1 2 | brew install k6 |
Windows
Here I use Chocolatey package manager , you can install it in the following way:
1 2 | choco install k6 |
Docker
1 2 | docker pull grafana/k6 |
To check for success, run the command:
1 2 | k6 version |
Performance Testing with K6
With k6, you need to know 2 concepts: Virtual Users – VUs (you can set the number of virtual users to access simultaneously on your product) and Duration – a string that specifies the duration of the testing process. take place.
To create and Run a Test on K6 is quite simple because K6 uses Javascript to run the test, so it is quite familiar to us. A simple js file used in testing with k6 would look like this:
1 2 3 4 5 6 7 8 | import http from 'k6/http'; import { sleep } from 'k6'; export default function () { http.get('https://k6.io'); sleep(1); } |
Or you can add multiple VUs and Duration as follows:
1 2 3 4 5 6 7 8 9 10 11 | import http from 'k6/http'; import { sleep } from 'k6'; export const options = { vus: 10, duration: '30s', }; export default function () { http.get('http://k6.io'); sleep(1); } |
Or you can increase or decrease the number of VUs during the Test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import http from 'k6/http'; import { check, sleep } from 'k6'; export const options = { stages: [ { duration: '30s', target: 20 }, { duration: '1m30s', target: 40 }, { duration: '20s', target: 0 }, ], }; export default function () { const res = http.get('https://k6.io/'); check(res, { 'status was 200': (r) => r.status == 200 }); sleep(1); } |
Now let’s create the test.js file with the above Config and run it with this command to see the results (Here I use docker):
1 2 | docker run --rm -i grafana/k6 run - <test.js |
And here is the End of test result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ✗ status was 200 ↳ 96% — ✓ 565 / ✗ 21 checks.........................: 96.41% ✓ 565 ✗ 21 data_received..................: 307 MB 1.9 MB/s data_sent......................: 699 kB 4.2 kB/s http_req_blocked...............: avg=62.99ms min=178ns med=445ns max=30.02s p(90)=1.35µs p(95)=56.78ms http_req_connecting............: avg=1.61ms min=0s med=0s max=45.52ms p(90)=0s p(95)=18.94ms http_req_duration..............: avg=5.22s min=64.3ms med=719.46ms max=1m0s p(90)=13.47s p(95)=31.23s { expected_response:true }...: avg=3.29s min=64.3ms med=640.2ms max=55.45s p(90)=7.43s p(95)=17.15s http_req_failed................: 3.58% ✓ 21 ✗ 565 http_req_receiving.............: avg=3.85s min=0s med=517.01ms max=59.97s p(90)=6.62s p(95)=25.29s http_req_sending...............: avg=138.95µs min=24.4µs med=87.48µs max=2.49ms p(90)=218.9µs p(95)=306.93µs http_req_tls_handshaking.......: avg=61.17ms min=0s med=0s max=29.99s p(90)=0s p(95)=32.09ms http_req_waiting...............: avg=1.37s min=19.77ms med=29.22ms max=1m0s p(90)=1.05s p(95)=2.8s http_reqs......................: 586 3.54026/s iteration_duration.............: avg=6.19s min=1.06s med=1.74s max=1m1s p(90)=14.53s p(95)=31.85s iterations.....................: 585 3.534218/s vus............................: 1 min=1 max=40 vus_max........................: 40 min=40 max=40 |
The results are available and let’s analyze what the metrics in the End of test have:
Each http_req parameter has indexes avg, min, max, med, p(90), p(95).
- Check (check) is the successful check rate.
- Response time (http_req_duration) is the total time for the request. It is equal to http_req_sending + http_req_waiting + http_req_receiving.
- Request rate (http_reqs) is the total number of HTTP k6 requests made.
- Failed request (http_req_failed) is the percentage of failed requests according to setResponseCallback.
- Connecting (http_req_connecting) is the time it takes to establish a TCP connection with the server.
- TLS handshake (http_req_tls_handshaking) is the TLS session handshake time with the server.
- Data sent (data_sent) is the amount of data sent. Track data for an individual URL to track data for an individual URL.
- Data received (data_received) is the amount of data received.
- Receiving (http_req_receiving) is the time it takes to receive the response data from the server.
- Sending (http_req_sending) is the time it takes to send response data from the server.
- Block (http_req_blocked) time blocked (waiting for a free TCP connection) before starting the request.
- Waiting (http_req_waiting) is the time to wait for a response from the server.
- Iteration duration (iteration_duration) is the time it takes to complete a full iteration, including time spent on initialization and destruction.
- Iteration (iterations) is the total number of times VUs execute the default function.
- VUs (vus) is the number of active users.
- VUs max (vus_max) is the maximum number of VUs.
If you don’t want to manually code the Config file with Javascript as above, you can also use cloud K6 to test it, very convenient. Please click here to experience it. (Note that the Free version will have many different limitations, but is it better than not, guys :v)
Summary:
So I gave you a basic introduction to Performance Testing and how to use K6. Hope to help everyone in some way in the process of learning about Performance Testing. In the next part, I will continue to share more about K6.
References: https://k6.io/