Skip to content

jittagornp/k6-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

k6 example

ตัวอย่างการเขียน/ใช้งาน k6

Table of Contens

Google Slide

k6 Introduction

k6 คืออะไร ?

  • k6 เป็น Load test tool ตัวนึงที่คนนิยมใช้งานกันอยู่ในปัจจุบัน
  • ถูกเขียนขึ้นด้วยภาษา Go
  • กิน Resource น้อย
  • ทำให้สามารถ จำลองปริมาณ Load/Virtual users ได้ในปริมาณมาก
  • การกำหนด Scenario ของการ Test เขียนผ่าน Code Javascript
  • ไม่มี GUI สำหรับ Build Script (ยกเว้นแบบ On Cloud)
  • มี Extensions ต่าง ๆ ให้สามารถ Install เพิ่มเติมได้
  • พัฒนาโดยทีม Grafana

Official Document

https://k6.io/docs/

วิธีการใช้งาน k6

ในตัวอย่างนี้จะใช้งานบนเครื่อง Mac ถ้าอยากใช้งานกับ OS อื่น ๆ ให้อ่านที่เอกสาร Installation ของ k6

  1. การติดตั้ง/Installation
$ brew install go
$ brew install k6
  1. Check version
$ k6 --version
# k6 v0.48.0 (go1.21.6, darwin/amd64)
  1. Create sample script
# Command format
# k6 new <script-name.js>

$ k6 new hello-k6.js
  1. Run
$ k6 run hello-k6.js

  1. การ Run ผ่าน Docker
$ docker run -v $(pwd):/script --rm -i grafana/k6 run /script/hello-k6.js 

Dashboard

  1. Install xk6

https://github.com/grafana/xk6/

This command line tool and associated Go package makes it easy to make custom builds of k6. -- Document

$ go install go.k6.io/xk6/cmd/xk6@latest
  1. Intall k6 extension

https://github.com/grafana/xk6-dashboard

$ xk6 build --with github.com/grafana/xk6-dashboard@latest

ถ้า run แล้วได้ error command not found: xk6 ให้ลอง run command นี้ดู แล้วค่อย run คำสั่งด้านบนอีกที

export PATH=$(go env GOPATH)/bin:$PATH

เมื่อ run เสร็จ จะได้ไฟล์ k6 มาแบบนี้

ให้ใช้ k6 นี้ run dashboard แทนตัว global command

การ ดู Dashboard แบบ realtime

  1. Run

ใช้ ./k6 แทน k6

$ ./k6 run --out web-dashboard hello-k6.js

  1. เปิดดู Dashboard

ต้องเปิดในขณะที่ Command ยัง run ไม่เสร็จ ถึงจะเปิดได้ เพราะมันต้องดูแบบ realtime เท่านั้น

http://127.0.0.1:5665/ui/?endpoint=/

การ ดู Dashboard แบบไม่ realtime

จะต้อง run k6 แล้ว save result เป็น .json ไว้ แล้วค่อยเอามาเปิดผ่าน dashboard อีกที

  1. Run (Save output เป็น .json)
$ ./k6 run --out json=output.json hello-k6.js
  1. Install k6-web-dashboard

https://github.com/grafana/xk6-dashboard/tree/master/cmd/k6-web-dashboard

The k6-web-dashboard is a command-line tool that enables the dashboard event file (saved during the previous k6 run) to be played back (and displayed in a browser). In addition to playback, it also offers the possibility to create a single file HTML report from the event file.

$ go install github.com/grafana/xk6-dashboard/cmd/k6-web-dashboard@latest
  1. แปลง .json ไปเป็น .ndjson (Web Dashboard support .ndjson)
$ k6-web-dashboard aggregate output.json output.ndjson
  1. Run/Replay dashboard ผ่าน .ndjson
$ k6-web-dashboard replay output.ndjson

Run dashboard ผ่าน Docker

$ docker run --rm -i -p 5665:5665 -v $(pwd):/script ghcr.io/grafana/xk6-dashboard:0.7.2 run --out web-dashboard /script/hello-k6.js

Export to HTML

$ docker run --rm -i -p 5665:5665 -v $(pwd):/script ghcr.io/grafana/xk6-dashboard:0.7.2 run --out web-dashboard=export=/script/output.html /script/hello-k6.js

จะได้ output เป็นไฟล์ html แบบนี้

เมื่อเราเปิดไฟล์ดู จะได้ result หน้าตาแบบนี้

Extensions

ดูทั้งหมดได้จาก

https://k6.io/docs/extensions/

Custom k6

ตัวอย่างการ Custom k6 โดยเพิ่ม dashboard และ mongodb extension เข้าไป

Dockerfile

FROM golang:latest as builder

RUN go install go.k6.io/xk6/cmd/xk6@latest

RUN xk6 build \
    --with github.com/grafana/xk6-dashboard@latest \
    --with github.com/GhMartingit/xk6-mongo@latest \
    --output /k6

FROM grafana/k6:latest
COPY --from=builder /k6 /usr/bin/k6

Build

$ docker build -t custom-k6 .

ตัวอย่างการ Run

$ docker run --rm -i -p 5665:5665 -v $(pwd):/script custom-k6 run --out web-dashboard=export=/script/output.html /script/hello-k6.js 

Connect Mongodb

$ docker run --rm -i -p 5665:5665 --add-host host.docker.internal:host-gateway -v $(pwd)/examples:/script custom-k6 run --out web-dashboard=export=/script/output.html /script/connect-mongodb.js

About

k6s load testing example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published