0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

curlコマンドのレスポンスタイムを手軽に計測する方法3選

Posted at

はじめに

curlコマンドのレスポンスタイムを計測する方法を備忘録としてまとめます。

curlコマンドのレスポンスタイムを手軽に計測する方法3選

この記事ではhttpbinへのリクエストを例にします。

httpbinは下記のようなレスポンスを返してくれるサービスです。

% curl https://httpbin.org/get
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/8.7.1", 
    "X-Amzn-Trace-Id": "Root=1-68232dac-309e4610759c1c0914dc0226"
  }, 
  "origin": "54.92.97.215", 
  "url": "https://httpbin.org/get"
}

originにはアクセス元のIPアドレスが返されるため、マスクしています。

-w(--write-out)オプション

次のように%{time_total}を渡して使います。

% curl https://httpbin.org/get -w "%{time_total}秒\n"  
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/8.7.1", 
    "X-Amzn-Trace-Id": "Root=1-68232e03-0dc9b4544b4fde143c620665"
  }, 
  "origin": "52.193.217.102", 
  "url": "https://httpbin.org/get"
}
0.699421秒

レスポンスタイムに関わる変数はtime_total以外にもいくつか用意されています。

以下は一例です。

  • time_connect: TCPコネクションの確立にかかった時間
  • time_namelookup: DNSの名前解決の完了にかかった時間

他にもたくさんあるため、気になる方は公式ドキュメントをご確認ください。

timeコマンド

timeコマンドは単純に先頭に記述するだけでOKです。

% time curl https://httpbin.org/get  
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/8.7.1", 
    "X-Amzn-Trace-Id": "Root=1-68232e1d-204d8fd0628c515b5719548c"
  }, 
  "origin": "52.193.217.102", 
  "url": "https://httpbin.org/get"
}
curl https://httpbin.org/get  0.02s user 0.01s system 4% cpu 0.774 total

timeはOSに標準搭載されているコマンドなので、curl以外にも使えます。

CPUやシステムリソースの使用状況も確認可能なのが特徴です。

httpstatコマンド

curlの機能を拡張したツールとして、httpstatを使う方法もあります。

ただしbrew install httpstatでインストールが必要です。

% httpstat https://httpbin.org/get 
Connected to 54.236.92.255:443 from 172.16.0.2:52164

HTTP/2 200 
date: Tue, 13 May 2025 11:34:32 GMT
content-type: application/json
content-length: 255
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true

Body stored in: /var/folders/qf/klw8c2194mz14ybjr3n7q6680000gn/T/tmpka77l8fi

  DNS Lookup   TCP Connection   TLS Handshake   Server Processing   Content Transfer
[     1ms    |      179ms     |     331ms     |       245ms       |        1ms       ]
             |                |               |                   |                  |
    namelookup:1ms            |               |                   |                  |
                        connect:180ms         |                   |                  |
                                    pretransfer:511ms             |                  |
                                                      starttransfer:756ms            |
                                                                                 total:757ms   

HTTPリクエストの各ステップを視覚的に表示してくれるため見やすいのが特徴です。

おわりに

  • 雑に計測したい時: timeコマンド
  • スクリプト内で使ったり文字列として整形して出力したい時: -wオプション
  • ボトルネックを視覚的に可視化したい時: httpstat

こんな感じの使い分けになるのかなと思いました。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?