devops

Grafana k6 설치 및 사용하는 방법 본문

DevOps/Opensource

Grafana k6 설치 및 사용하는 방법

vata500 2022. 7. 9. 19:44
반응형

K6

Grafana k6는 성능 테스트를 쉽게 할 수 있는 오픈소스 부하 테스트 도구다. 무료로 사용할 수 있고, 자유자재로 확장가능하다.

k6 홈페이지

Grafana Labs와 커뮤니티가 개발 했으며, 시스템의 안정성을 테스트하는 데 굉장히 효과적이다. 아래와 같은 특징이 있다.

  • 개발자 친화적인 API
  • Javascript ES2015/ES6의 Script(로컬과 연결 모듈 지원)
  • 목표 지향적이고 자동화 부하 테스트 가능

K6 설치 방법(Ubuntu/Debian)

$ sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
$ echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] 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

 

혹시 Window에서 WSL를 사용한다면, k6의 패키지 서명 키를 가져오는 명령이 다음과 같은 에러가 뜰 수 있다.

gpg: keybox '/usr/share/keyrings/k6-archive-keyring.gpg' created
gpg: failed to create temporary file '/root/.gnupg/.#lk0x000055db689f2310.a86c4b090dc7.7': No such file or directory
gpg: connecting dirmngr at '/root/.gnupg/S.dirmngr' failed: No such file or directory
gpg: keyserver receive failed: No dirmngr

이럴 경우, sudo gpg -k 를 입력해서 /root/ .gnupg/ 디렉토리를 생성하면 된다. 외에도 혹시 실행에 어려움을 겪는다면 아래 Troubleshouting 페이지 참고.

https://k6.io/docs/getting-started/installation/troubleshooting/

 

Troubleshooting

Instructions to fix the most common installation issues.

k6.io

HTTP Requests

HTTP 요청을 통해서 서비스의 성능을 테스트하려고 한다. 기본적인 GET과 POST의 example

GET

import http from 'k6/http';

export default function () {
  http.get('http://test.k6.io'); // http의 get 요청을 'http://test.k6.io' URL로 보낸다. 
}

POST

import http from 'k6/http';

export default function () {
  const url = 'http://test.k6.io/login'; // 요청 보내는 URL
  const payload = JSON.stringify({ // POST 요청시 전달되는 Body
    email: 'aaa',
    password: 'bbb',
  });

  const params = { // header 설정(요청 형식 json)
    headers: {
      'Content-Type': 'application/json',
    },
  };

  http.post(url, payload, params); // post 요청
}

기타 메소드 및 옵션은 다음 공식 문서에서 확인

https://k6.io/docs/using-k6/http-requests/

 

HTTP Requests

Define the HTTP requests and methods you want to use. k6 adds tags to the requests, making it easier to filter results. You can customize tags as you wish.

k6.io

기본 실행 명령어

k6 run -u 1 -i 100 ./single-request.k6.js

-i , --iteractions : 반복요청하는 횟수, 최대 지속시간은 10분이다.
-u , --vus : 동시에 실행하는 vu 수(병렬) 

https://k6.io/docs/using-k6/k6-options/reference/#console-output

 

Options reference

A complete list of all k6 options, with descriptions, defaults, and examples of how to set the option in your script, config files, environment variables, or CLI.

k6.io

 

반응형
Comments