API load test with K6 (JavaScript + Go)

import config from '../common/config.js' // I use config.js to export API endpoint variablesimport http from 'k6/http'import { check, sleep } from 'k6'import { Trend } from 'k6/metrics'import faker from 'cdnjs.com/libraries/Faker' // this library is great to generate mock dataexport let SignUpDuration = new Trend('Sign up Duration')
let duration = 1000
export let options = {vus: 10,duration: '30s',thresholds: {'Sign up Duration': [`p(95)<${duration}`]}}export default function () {let res = http.post(__ENV.HOST + config.api.signUp, {"email": 'QA_TECH_' + faker.internet.email(),"password": faker.internet.password(),"language": "vn", "gender": "M"})check(res, {'status is OK': res => res.status == 200,'transaction time is less than threshold': res => res.timings.duration < duration})SignUpDuration.add(res.timings.duration)sleep(1)}
k6 run -e HOST=https://www.example.com --vus 5 --duration 10s tests/signUp.js
/\      |‾‾|  /‾‾/  /‾/   
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
output: -
script: tests/signUp.js
duration: 10s, iterations: -
vus: 1, max: 1
done [==========================================================] 10s / 10s✗ transaction time is less than threshold
↳ 0% — ✓ 0 / ✗ 2
✓ status is OK
✗ Sign up Duration...........: avg=2816.323819 min=1926.378042 med=2816.323819 max=3706.269597 p(90)=3528.280442 p(95)=3617.275019
checks.....................: 50.00% ✓ 2 ✗ 2
data_received..............: 5.0 kB 504 B/s
data_sent..................: 921 B 92 B/s
http_req_blocked...........: avg=101.78ms min=1.87µs med=101.78ms max=203.57ms p(90)=183.21ms p(95)=193.39ms
http_req_connecting........: avg=34.57ms min=0s med=34.57ms max=69.14ms p(90)=62.22ms p(95)=65.68ms
http_req_duration..........: avg=2.81s min=1.92s med=2.81s max=3.7s p(90)=3.52s p(95)=3.61s
http_req_receiving.........: avg=218.81µs min=195.49µs med=218.81µs max=242.14µs p(90)=237.47µs p(95)=239.8µs
http_req_sending...........: avg=1.64ms min=354.72µs med=1.64ms max=2.93ms p(90)=2.67ms p(95)=2.8ms
http_req_tls_handshaking...: avg=66.86ms min=0s med=66.86ms max=133.73ms p(90)=120.36ms p(95)=127.04ms
http_req_waiting...........: avg=2.81s min=1.92s med=2.81s max=3.7s p(90)=3.52s p(95)=3.61s
http_reqs..................: 2 0.199997/s
iteration_duration.........: avg=3.92s min=3.13s med=3.92s max=4.7s p(90)=4.55s p(95)=4.63s
iterations.................: 2 0.199997/s
vus........................: 1 min=1 max=1
vus_max....................: 1 min=1 max=1
ERRO[0012] some thresholds have failed

--

--

--

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hung Tran Nguyen

Hung Tran Nguyen

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

More from Medium

File upload security in web applications

Connecting to Aws RDS database with IAM authentication in NHibernate with C# .net

Secure API with Oauth 2.0 Kong Plugin

Solve SSL issue when host Identity Server 4 behind nginx