Performance testing with k6 - GeeksforGeeks

01 Dec, 2020
What is Performance and Load Testing?
Performance Testing is the process of analyzing the quality and capability of a product. Load testing is a subset of performance testing where we analyze the behavior of the application under normal or peak load conditions.
What is k6?
k6 is an open-source load testing tool for testing the performance of APIs, microservices, and websites.
Prerequisites: Install the k6 tool.
Write Your Performance Test Script: Scripts must contain, at the very least, a default function — this defines the entry point for your VUs, similar to the main() function in many other languages. The code inside the default function is called “VU code”, and is run over and over for as long as the test is running.
k6 works with the concept of virtual users (VUs), which run scripts. Duration is a string specifying the total duration a test should be run.
When creating a new load test, the first thing that you’ll often do is define the HTTP requests that will be used to test your system. A simple example that just performs a GET request looks like this:
Javascript
import{ sleep } from 'k6';import http from 'k6/http';  export let options = {    duration : '1m',    vus : 50,};  export default function() {    http.get('http://test.k6.io/contacts.php');    sleep(3);}
You can run the test locally using the following command. Just make sure to install k6 first.
$ k6 run performance-test.js

This produces the following output:
          /\      | ̅ ̅|  / ̅ ̅/  / ̅/
     /\  /  \     |  |_/  /  / /
    /  \/    \    |      |  /   ̅ ̅\
   /          \   |  | ̅\  \ | (_) |
  / __________ \  |__|  \__\ \___/ .io

  execution: local
     script: performance-test.js
     output: -

  scenarios: (100.00%) 1 executors, 50 max VUs, 1m30s max duration (incl. graceful stop):
           * default: 50 looping VUs for 1m0s (gracefulStop: 30s)


running (1m02.5s), 00/50 VUs, 1000 complete and 0 interrupted iterations
default ✓ [======================================] 50 VUs  1m0s


    data_received..............: 711 kB 11 kB/s
    data_sent..................: 88 kB  1.4 kB/s
    http_req_blocked...........: avg=8.97ms   min=1.37μs   med=2.77μs   max=186.58ms p(90)=9.39μs   p(95)=8.85ms
    http_req_connecting........: avg=5.44ms   min=0s       med=0s       max=115.8ms  p(90)=0s       p(95)=5.16ms
    http_req_duration..........: avg=109.39ms min=100.73ms med=108.59ms max=148.3ms  p(90)=114.59ms p(95)=119.62ms
    http_req_receiving.........: avg=55.89μs  min=16.15μs  med=37.92μs  max=9.67ms   p(90)=80.07μs  p(95)=100.34μs
    http_req_sending...........: avg=15.69μs  min=4.94μs   med=10.05μs  max=109.1μs  p(90)=30.32μs  p(95)=45.83μs
    http_req_tls_handshaking...: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s
    http_req_waiting...........: avg=109.31ms min=100.69ms med=108.49ms max=148.22ms p(90)=114.54ms p(95)=119.56ms
    http_reqs..................: 1000   15.987698/s
    iteration_duration.........: avg=3.11s    min=3.1s     med=3.1s     max=3.3s     p(90)=3.12s    p(95)=3.15s
    iterations.................: 1000   15.987698/s
    vus........................: 50     min=50 max=50
    vus_max....................: 50     min=50 max=50

HTTP-specific built-in metric:
Summary export: You can also make k6 output detailed statistics in a CSV format by using the –out/-o option for k6 run, like this:
$ k6 run --out csv=my_test_result.csv script.js

Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to apply style to parent if it has child with CSS?
Difference Between PUT and PATCH Request
How to execute PHP code using command line ?
REST API (Introduction)
How to redirect to another page in ReactJS ?