🎯
ローカルPCから手軽に並列リクエストする【Apache Benchmark】
やりたいこと
- APIサーバに数十オーダーの軽めの負荷をかけ、レスポンスタイムを確認したい
- 厳密さは求めない
- 無料でやりたい
結論: Apache Benchmarkを使う
# GETリクエスト
ab -n 9 -c 3 https://httpbin.org/get
# POSTリクエスト
echo '{"message":"Hello"}' | ab -n 9 -c 3 -p /dev/stdin -T 'application/json' https://httpbin.org/post
-
-n 9: 全9回リクエスト -
-c 3: 3並列リクエスト -
-p /dev/stdin: POSTのボディを/dev/stdinから読み込む -
-T 'application/json': Content-Type: application/jsonを指定
環境を汚したくない人向け (ephemeralなコンテナから実行)
docker run --rm -it httpd:alpine /bin/sh
ab -n 9 -c 3 https://httpbin.org/get
結果例
/usr/local/apache2 # ab -n 9 -c 3 https://httpbin.org/get
This is ApacheBench, Version 2.3 <$Revision: 1923142 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking httpbin.org (be patient).....done
Server Software: gunicorn/19.9.0
Server Hostname: httpbin.org
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Server Temp Key: ECDH prime256v1 256 bits
TLS Server Name: httpbin.org
Document Path: /get
Document Length: 259 bytes
Concurrency Level: 3
Time taken for tests: 5.733 seconds
Complete requests: 9
Failed requests: 0
Total transferred: 4356 bytes
HTML transferred: 2331 bytes
Requests per second: 1.57 [#/sec] (mean)
Time per request: 1911.036 [ms] (mean)
Time per request: 637.012 [ms] (mean, across all concurrent requests)
Transfer rate: 0.74 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 600 686 77.6 725 818
Processing: 206 497 310.8 452 1025
Waiting: 206 496 310.7 451 1024
Total: 806 1183 351.3 1126 1843
Percentage of the requests served within a certain time (ms)
50% 1025
66% 1227
75% 1227
80% 1639
90% 1843
95% 1843
98% 1843
99% 1843
100% 1843 (longest request)
Discussion