k6는 Go로 작성된 성능 및 부하 테스트 도구로, 환경 설정이나 테스트 스크립트를 JavaScript 파일(.js) 형식으로 작성합니다. YAML이나 다른 형식이 아닌, JavaScript를 사용하는 것이 k6의 특징 중 하나입니다. 이를 통해 사용자는 테스트 시나리오를 프로그래밍 방식으로 세밀하게 제어할 수 있으며, 조건부 로직, 변수 사용, 외부 데이터 소스로부터 데이터 가져오기 등 복잡한 테스트 케이스를 구현할 수 있습니다.
Go는 구글에서 개발한 프로그래밍 언어로, 간결함, 효율성, 그리고 성능을 강조합니다. 공식적으로는 2009년에 출시되었습니다. Go는 정적 타입, 컴파일 언어이며, 가비지 컬렉션, 메모리 안전성, CSP(Communicating Sequential Processes) 스타일의 동시성 프로그래밍 기능을 제공합니다. 이러한 특징들로 인해 Go는 대규모 분산 시스템, 클라우드 서비스, 백엔드 애플리케이션 등의 개발에 널리 사용됩니다.
설치: https://k6.io/docs/get-started/installation/
brew install k6
k6 new
script.js 파일이 생성된다.
https://sjparkk-dev1og.tistory.com/221
https://grafana.com/docs/k6/latest/examples/get-started-with-k6/test-for-functional-behavior/
// script.js
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
// A number specifying the number of VUs to run concurrently.
vus: 10,
// A string specifying the total duration of the test run.
duration: '30s',
// The following section contains configuration options for execution of this
// test script in Grafana Cloud.
//
// See https://grafana.com/docs/grafana-cloud/k6/get-started/run-cloud-tests-from-the-cli/
// to learn about authoring and running k6 test scripts in Grafana k6 Cloud.
//
// cloud: {
// // The ID of the project to which the test is assigned in the k6 Cloud UI.
// // By default tests are executed in default project.
// projectID: "",
// // The name of the test in the k6 Cloud UI.
// // Test runs with the same name will be grouped.
// name: "script.js"
// },
// Uncomment this section to enable the use of Browser API in your tests.
//
// See https://grafana.com/docs/k6/latest/using-k6-browser/running-browser-tests/ to learn more
// about using Browser API in your test scripts.
//
// scenarios: {
// // The scenario name appears in the result summary, tags, and so on.
// // You can give the scenario any name, as long as each name in the script is unique.
// ui: {
// // Executor is a mandatory parameter for browser-based tests.
// // Shared iterations in this case tells k6 to reuse VUs to execute iterations.
// //
// // See https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/ for other executor types.
// executor: 'shared-iterations',
// options: {
// browser: {
// // This is a mandatory parameter that instructs k6 to launch and
// // connect to a chromium-based browser, and use it to run UI-based
// // tests.
// type: 'chromium',
// },
// },
// },
// }
};
// The function that defines VU logic.
//
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
// about authoring k6 scripts.
//
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
# 입력
k6 run script.js
# 출력
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
execution: local
script: script.js
output: -
scenarios: (100.00%) 1 scenario, 10 max VUs, 1m0s max duration (incl. graceful stop):
* default: 10 looping VUs for 30s (gracefulStop: 30s)
data_received..................: 2.8 MB 90 kB/s
data_sent......................: 27 kB 859 B/s
http_req_blocked...............: avg=27.33ms min=1µs med=7µs max=760.08ms p(90)=11µs p(95)=29.44µs
http_req_connecting............: avg=9.66ms min=0s med=0s max=376.99ms p(90)=0s p(95)=0s
http_req_duration..............: avg=250.36ms min=190.17ms med=226.73ms max=469.08ms p(90)=402.65ms p(95)=427.86ms
{ expected_response:true }...: avg=250.36ms min=190.17ms med=226.73ms max=469.08ms p(90)=402.65ms p(95)=427.86ms
http_req_failed................: 0.00% ✓ 0 ✗ 240
http_req_receiving.............: avg=22.56ms min=14µs med=103.5µs max=256.32ms p(90)=190.81ms p(95)=209ms
http_req_sending...............: avg=29.06µs min=5µs med=28µs max=143µs p(90)=43µs p(95)=47.04µs
http_req_tls_handshaking.......: avg=10.4ms min=0s med=0s max=282.81ms p(90)=0s p(95)=0s
http_req_waiting...............: avg=227.77ms min=190ms med=219.51ms max=385.59ms p(90)=244.65ms p(95)=280.53ms
http_reqs......................: 240 7.648501/s
iteration_duration.............: avg=1.27s min=1.19s med=1.22s max=1.96s p(90)=1.42s p(95)=1.43s
iterations.....................: 240 7.648501/s
vus............................: 1 min=1 max=10
vus_max........................: 10 min=10 max=10
running (0m31.4s), 00/10 VUs, 240 complete and 0 interrupted iterations
default ✓ [======================================] 10 VUs 30s
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
scenarios: {
example_scenario: {
// name of the executor to use
executor: 'shared-iterations',
// common scenario configuration
startTime: '10s',
gracefulStop: '5s',
env: { EXAMPLEVAR: 'testing' },
tags: { example_tag: 'testing' },
// executor-specific configuration
vus: 10,
iterations: 200,
maxDuration: '10s',
},
},
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
k6와 Grafana의 연동은 부하 테스트 결과를 시각화하고 모니터링하는 데 아주 효과적인 방법입니다. k6로부터 생성된 데이터를 Grafana 대시보드에서 시각적으로 분석할 수 있도록 하는 과정은 다음과 같은 단계를 포함합니다:
InfluxDB 설정: InfluxDB는 시계열 데이터를 저장하기 위한 데이터베이스로, k6의 부하 테스트 결과를 저장하는 데 사용됩니다. k6는 --out
옵션을 통해 InfluxDB로 직접 데이터를 전송할 수 있습니다.
k6 실행: 부하 테스트를 실행할 때, k6에게 InfluxDB로 결과 데이터를 전송하도록 설정합니다. 이는 k6 실행 명령어에 --out influxdb=http://localhost:8086/myk6db
(여기서 http://localhost:8086/myk6db
는 InfluxDB의 주소와 데이터베이스 이름을 나타냅니다)와 같은 형태로 추가하여 수행할 수 있습니다.
Grafana 설치 및 설정: Grafana를 설치하고, 설정 페이지에서 InfluxDB를 데이터 소스로 추가합니다. InfluxDB 데이터 소스를 추가할 때, InfluxDB의 URL과 데이터베이스 이름, 사용자 이름, 비밀번호 등의 정보를 제공해야 합니다.
Grafana 대시보드 구성: Grafana에서 새로운 대시보드를 만들고, InfluxDB에서 k6의 데이터를 쿼리하여 시각화할 패널을 추가합니다. Grafana는 다양한 시각화 옵션(그래프, 게이지, 히트맵 등)을 제공하므로, 이를 활용하여 부하 테스트 결과를 다양한 시각적 형태로 표현할 수 있습니다.
이 과정을 통해, 사용자는 k6로부터의 부하 테스트 결과를 실시간으로 모니터링하고, 성능 지표를 시각적으로 분석할 수 있게 됩니다. Grafana 대시보드를 통해 응답 시간, 요청 처리량, 에러 비율 등의 중요한 성능 지표를 한눈에 볼 수 있으며, 이를 통해 시스템의 성능 병목 지점을 식별하고 개선 방안을 모색할 수 있습니다.