이번에는 k6를 이용해서 부하테스트 진행하는 방법을 알아보겠습니다.
k6 설치
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
$ echo "deb 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
Smoke Test
테스트를 진행하기 위해서는 script가 필요하기 때문에, 다음과 같이 테스트 시나리오에 필요한 로직을 작성합니다.
# smoke.js
import http from 'k6/http';
import { check, group, sleep, fail } from 'k6';
export let options = {
vus: 1,
duration: '1m',
thresholds: {
http_req_duration: ['p(99)<100'], // 99% of requests must complete below 0.1s
},
};
const BASE_URL = 'https://gyeom-subway-admin.kro.kr/';
const USERNAME = 'koreatech93@naver.com';
const PASSWORD = 'tkatkatka05)%';
export default function () {
mainPage();
const authHeaders = login();
retrieveMember(authHeaders);
pathPage();
findPath(authHeaders);
sleep(1);
}
function mainPage() {
const response = http.get(BASE_URL);
check(response, {
'entered in main page successfully': (res) => res.status === 200
});
}
function login() {
const payload = JSON.stringify({
email: USERNAME,
password: PASSWORD,
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};
const response = http.post(`${BASE_URL}/login/token`, payload, params);
check(response, {
'logged in successfully': (res) => res.json('accessToken') !== '',
});
return {
headers: {
Authorization: `Bearer ${response.json('accessToken')}`,
},
};
}
function retrieveMember(authHeaders) {
let myObjects = http.get(`${BASE_URL}/members/me`, authHeaders).json();
check(myObjects, { 'retrieved member': (obj) => obj.id != 0 });
}
function pathPage() {
const response = http.get(`${BASE_URL}/path`);
check(response, {
'entered in path page successfully': (res) => res.status === 200
});
}
function findPath(authHeaders) {
const response = http.get(`${BASE_URL}/path?source=3&target=4`, authHeaders);
check(response, {
'get path info successfully': (res) => res.status === 200
});
}
스크립트가 작성되었으면 다음과 같이 k6 명령어를 실행합니다.
$ k6 run smoke.js
k6 테스트 결과
'DevOps. > Infra' 카테고리의 다른 글
Grafana k6 사용하여 부하테스트 하는 방법 3 (0) | 2022.12.07 |
---|---|
Grafana k6 사용하여 부하테스트 하는 방법 1 (0) | 2022.12.05 |
Nginx로 Reverse Proxy 서버 만들기 (0) | 2022.11.21 |