LoginSignup
1
0

More than 3 years have passed since last update.

curlでレスポンスにかかる時間を計測するスクリプト

Last updated at Posted at 2020-06-12

curlですぐにapiの時間を計測!

shellで複数回apiを叩いて平均的なレスポンスの時間を取得したいという場合に簡単に使えるスクリプトを書きました
ENDPOINTに計測したいapiのurlを書いて、実行するだけです。
実行回数とAPI method, data も変数内に記入することができるようにしています


$ vim response.sh # お好きなエディタで

...下記のコードを保存...

$ chmod 700 response.sh

$ ./response.sh
#!/bin/bash

# 集計用
TOTAL=0

# custom variables
## curl設定
ENDPOINT="https://xxxxxx/yyyyy"
METHOD="POST"
DATA='{"hoge": "fuga"}'

## 回数
COUNT=10

## 平均の計算で小数点以下何桁まで表示するか
SCALE=10


for ((c=1; c<=$COUNT; c++))
do
  # curlでレスポンスにかかった時間を取得
  TIME=$(curl $ENDPOINT -X $METHOD -d $DATA -s -o /dev/null -w "%{time_starttransfer}\n")
  # 時間を標準出力に表示
  echo "time: $TIME [ms]"

  # 加算する
  TOTAL=`echo "$TOTAL + $TIME" | bc`
done

# 平均を計算
AVG=`echo "scale=$SCALE; $TOTAL / $COUNT" | bc`

# 結果出力
echo "total: $TOTAL [ms]"
echo "avarage: $AVG [ms]"
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0