
cURL 응답 시간 curl을 사용하여 Web API의 응답 시간을 측정하는 방법
2022-10-13 last update
5 minutes reading curl왜 컬?
HTTP 요청을 벤치마킹하기 위한 많은 특정 도구가 있습니다.
ab
, JMeter
, wrk
...그렇다면 왜 여전히 목적으로
curl
를 사용합니까?curl
가 널리 사용되고 있고 웹 개발자를 위한 일종의 공용어이기 때문입니다.또한 일부 도구에는 HTTP 요청을
curl
명령으로 검색하는 기능이 있습니다.
URL과 매개변수뿐만 아니라
Authorization
또는 Cookie
를 포함한 요청 헤더도 복사하기 때문에 매우 유용합니다.도구
이 기사에서는 다음 도구를 사용합니다.
curl을 사용하여 응답 시간 측정
먼저
curl
명령어를 준비해보자. 이때 구글 크롬을 이용해서 제 개인 블로그에 요청하는 명령을 받았습니다. (Cookie
제거됨)$ curl 'https://blog.yuyat.jp/' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,ja;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36' -H 'Connection: keep-alive' --compressed
서버에서 응답 본문을 출력합니다.
이 옵션을 추가해 보겠습니다.
-s -o /dev/null -w "%{time_starttransfer}\n"
-s
는 진행을 멈추게 하고, -o
는 /dev/null
에 응답 본문을 처분합니다.그리고 중요한 것은
-w
입니다.다양한 형식을 지정할 수 있으며 이번에는
time_starttransfer
를 사용하여 응답 시간(첫 번째 바이트까지의 시간)을 검색했습니다.아래와 같이 표시됩니다.
$ curl 'https://blog.yuyat.jp/' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,ja;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36' -H 'Connection: keep-alive' --compressed -s -o /dev/null -w "%{time_starttransfer}\n"
0.188947
응답 시간은 0.188947초(188msec)입니다.
단순화하기 위해 래퍼 명령
curlb
도 만들었습니다.#!/bin/sh
curl -s -o /dev/null -w '%{time_starttransfer}\n' "[email protected]"
응답 시간의 백분위수 측정
단일 요청으로 벤치마킹하는 것은 적절하지 않습니다.
그런 다음 100개 요청의 백분위수를 측정해 보겠습니다.
ntimes
는 이러한 목적에 유용합니다.go get github.com/yuya-takeyama/ntimes
로 설치하거나 저장소에 미리 빌드된 바이너리가 있습니다.ntimes 100 --
명령의 시작 부분에 curl
를 추가합시다.$ ntimes 100 -- curlb 'https://blog.yuyat.jp/' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,ja;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36' -H 'Connection: keep-alive' --compressed
0.331915
0.064085
0.059883
0.074047
0.059774
...
그리고 숫자의 백분위수를 측정하려면
percentile
라는 명령이 가장 쉬운 옵션일 수 있습니다.go get github.com/yuya-takeyama/percentile
로 설치하거나 리포지토리에서 미리 빌드된 바이너리를 다운로드합니다.그리고 명령 끝에
| percentile
를 추가합니다.$ ntimes 100 -- curlb 'https://blog.yuyat.jp/' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,ja;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36' -H 'Connection: keep-alive' --compressed | percentile
50%: 0.061777
66%: 0.06412
75%: 0.06872300000000001
80%: 0.07029000000000001
90%: 0.07496700000000001
95%: 0.076153
98%: 0.077226
99%: 0.07957
100%: 0.109931
그게 다야!