개발일기
성능 테스트 k6+ Grafana +influxDB 본문
테스트코드에 관심이 많다보니, 자연스럽게 성능테스트에도 눈길이 갔습니다. 그래서 지금까지 성능 테스트를 Jmeter 사용하여 로컬에서 진행했습니다. 그러다가 문득 든 의문은 로컬에 있는 스펙과 서버에있는 스펙이 다른데 서버에서 성능 테스트를 더 좋다는 결론이 나왔습니다.
기존처럼 Jmeter로 진행할까도 고민했지만, k6 javascript 사용하여 손쉽게 성능 테스트를 할 수 있다는 장점이 있어 빠르게 습득할 수 있겠다는 생각이 들어 k6로 진행하겠습니다.
services:
influxdb:
image: influxdb:1.8
ports:
- "8086:8086"
environment:
- INFLUXDB_DB=k6
networks:
- monitor
prometheus:
image: prom/prometheus:latest
container_name: prometheus
user: "65534:65534"
volumes:
- ./prometheus/config:/etc/prometheus
- ./prometheus/data:/prometheus/data
ports:
- "9090:9090"
command:
- '--web.enable-lifecycle'
- '--config.file=/etc/prometheus/prometheus.yml'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
networks:
- monitor
grafana:
image: grafana/grafana:latest
container_name: grafana
user: "472:472"
ports:
- "3000:3000"
volumes:
- ./grafana/data:/var/lib/grafana
networks:
- monitor
loki:
image: grafana/loki:2.7.3
container_name: loki
volumes:
- ./loki:/loki
- ./loki/local-config.yaml:/etc/loki/loki.yaml
ports:
- "3100:3100"
command: -config.file=/etc/loki/loki.yaml
environment:
- TZ=Asia/Seoul
networks:
- monitor
k6:
image: grafana/k6:latest
ports:
- "6565:6565"
environment:
- K6_OUT=influxdb=http://influxdb:8086/k6
volumes:
- ./examples:/scripts
networks:
- monitor
networks:
monitor:
name: monitor
driver: bridge
Influxdb
influxdb:
image: influxdb:1.8
ports:
- "8086:8086"
environment:
- INFLUXDB_DB=k6
networks:
- monitor
influxDB
- 이미지: influxdb:1.8 버전 사용
포트 매핑
- 8086:8086 InfluxDB HTTP API 포트
환경변수
- INFLUXDB_DB=k6 기본적으로 k6라는 데이터베이스를 생성
사용목적:
- k6에서 성능 테스트 데이터를 InfluxDB에 저장
- 이후 Grafana 데이터를 시각화에 사용
Prometheus
prometheus:
image: prom/prometheus:latest
container_name: prometheus
user: "65534:65534"
volumes:
- ./prometheus/config:/etc/prometheus
- ./prometheus/data:/prometheus/data
ports:
- "9090:9090"
command:
- '--web.enable-lifecycle'
- '--config.file=/etc/prometheus/prometheus.yml'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
networks:
- monitor
prom/promethues:latest 최신 프로메테우스 버전 사용
컨테이너 이름: prometheus
포트 매핑:
- 9090:9090: Prometheus 웹 UI 및 API 포트
사용자 권한:
- user: "65534:65534" 컨테이너 내 파일 시스템 접근을 제한
불륨:
- ./prometheus/config:/etc/prometheus: 프로메테우스 설정 파일 (prometheus.yml) 매핑
- ./prometheus/data:/prometheus/data: 프로메테우스 데이터 저장소 매핑
명령어:
--web.enable-lifecycle: Prometheus 웹 UI에서 종료 관리 가능
--config.file=/etc/prometheus/prometheus.yml : 프로메테우스 설정 파일 경로 지정
Grafana
grafana:
image: grafana/grafana:latest
container_name: grafana
user: "472:472"
ports:
- "3000:3000"
volumes:
- ./grafana/data:/var/lib/grafana
networks:
- monitor
이미지: grafana/grafana:latest 최신 Grafana 버전 사용
컨테이너 이름: grafana
포트 매핑:
- 3000:3000 Grafana 웹 UI 포트
볼륨:
- ./grafana/data:/var/lib/grafana Grafana 설정 및 데이터 저장소.
사용자 권한:
- user: "472:472" 컨테이너 내부 권한 제한
사용 목적:
- Prometheus, Loki, InfluxDB 등에서 데이터 가져와 대시보드로 시각화
- 다양한 데이터 소스 연결 및 알람 설정 가능
k6
k6:
image: grafana/k6:latest
ports:
- "6565:6565"
environment:
- K6_OUT=influxdb=http://influxdb:8086/k6
volumes:
- ./examples:/scripts
networks:
- monitor
- 이미지: grafana/k6:latest (k6 퍼포먼스 테스트 도구)
- 포트 매핑: 6565:6565 K6 HTTP API 포트
- 환경 변수: K6_OUT=influxdb=http://influxdb:8086/k6 k6 테스트 결과물 InfluxDB 저장
- 볼륨: ./examples:/scaripts: 테스트 스크립트 파일 경로
- 사용목적: API 및 시스템 부하 테스트 수행 , 성능 결과를 InfluxDB에 기록하여 Grafana 시각화
docker-compose up -d 로 실행
이제 Grafana 넘어가서
https://grafana.com/grafana/dashboards/2587-k6-load-testing-results/
k6 Load Testing Results | Grafana Labs
About A nice-lookin’ dashboard to visualize load testing results from the k6 load testing tool. Currently requires InfluxDB. See the k6 docs to set this up. Version History Version 1 - 06 July 2017 Version 2 - 18 July 2017 [Feature] Added drop-down for s
grafana.com
InfluxDB
DashBoard
해당 명령어를 입력한다.
k6 run --out influxdb=localhost:8086 capo-backend.js
import http from "k6/http";
import { sleep } from "k6";
import { check } from "k6";
export const options = {
vus: 1,
duration : "10s",
};
export default function () {
const url = "https://photocard.site/v2/auth/email"
const payload = JSON.stringify({email : "hanseu9839@gmail.com"});
const params = {
headers: { "Content-Type" : "application/json" },
};
const res = http.post(url, payload, params);
check(res, {
"is status 201": (r) => r.status === 201,
"response contains success message": (r) => r.body.includes("메일 전송 성공"),
});
sleep(1);
}
vus 1기준으로 해주었습니다. k6 용어정리는 추후 포스팅 할 예정입니다.
Grafana를 들어가주면, 성능 테스트한 내역을 확인할 수 있습니다.
'photocard backend server 개발일기' 카테고리의 다른 글
SonarQube , JaCoCo 테스트 커버리지 측정하기 (1) | 2025.02.06 |
---|---|
[리팩토링] 1부 Spring Security 리팩토링 / 회원 로그인, 로그아웃 (0) | 2025.01.21 |
[JPA] QueryDSL No property 'xxxxx' found for type 'xxxxx' (0) | 2025.01.14 |
[DDD 모델링] 포토카드 도메인 주도 개발을 위한 회원 , 프로필 모델링 (2) | 2025.01.13 |
[Swagger] 스웨거 커스텀하여 @ApiResponses 커스텀 어노테이션으로 대체하기 (1) | 2025.01.10 |