Skip to content

Instantly share code, notes, and snippets.

@t2psyto
Last active October 10, 2023 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t2psyto/1c6ba7378596591a57e204983259ee09 to your computer and use it in GitHub Desktop.
Save t2psyto/1c6ba7378596591a57e204983259ee09 to your computer and use it in GitHub Desktop.
curl でwebの応答時間をログ出力する for windows環境
@echo off
setlocal enabledelayedexpansion
echo ■■ パタン1: インターネット(yahoo)
set cwd1=%~dp0
set cwd=%cwd1:~0,-1%
rem タイムスタンプ YYYYMMDD_hhmmss を取得
set time2=%time: =0%
set time3=%time2::=%
set timestamp=%date:/=%_%time3:~0,6%
rem echo timestamp: %timestamp%
rem 接続先ログファイルのパス
set LOGDIR=%cwd%\web応答計測
rem set LOGFILE=%cwd%\%timestamp%_%COMPUTERNAME%_webresponse.log
mkdir %LOGDIR% > nul 2>&1
set LOGFILE=%LOGDIR%\%timestamp%_%COMPUTERNAME%_web応答テスト_Yahoo.log
rem 接続先URL
set URL=http://yahoo.co.jp
rem PROXY
set HTTP_PROXY=
set HTTPS_PROXY=
rem 試行回数
set CYCLE=3
echo ■ webサーバの応答速度を計測します。
echo timestamp: %timestamp%
echo URL: %URL%
echo HTTP_PROXY: %HTTP_PROXY%
echo LOGFILE: %LOGFILE%
echo.
set CURL_RESULT_HEADER=timestamp, content_type, filename_effective, ftp_entry_path, http_code, http_connect, local_ip, local_port, num_connects, num_redirects, redirect_url, remote_ip, remote_port, size_download, size_header, size_request, size_upload, speed_download, speed_upload, ssl_verify_result, time_appconnect, time_connect, time_namelookup, time_pretransfer, time_redirect, time_starttransfer, time_total, url_effective
echo %CURL_RESULT_HEADER% >> %LOGFILE%
for /l %%i in (1, 1, %CYCLE%) do (
echo □ 回数: %%i / %CYCLE%
rem @echo|set /p=%timestamp% >> %LOGFILE%
del /S /Q %TEMP%\tmpclip*.txt > nul 2>&1
curl -m 3 %URL% -o NUL -SsfL -w "%%{content_type}, %%{filename_effective}, %%{ftp_entry_path}, %%{http_code}, %%{http_connect}, %%{local_ip}, %%{local_port}, %%{num_connects}, %%{num_redirects}, %%{redirect_url}, %%{remote_ip}, %%{remote_port}, %%{size_download}, %%{size_header}, %%{size_request}, %%{size_upload}, %%{speed_download}, %%{speed_upload}, %%{ssl_verify_result}, %%{time_appconnect}, %%{time_connect}, %%{time_namelookup}, %%{time_pretransfer}, %%{time_redirect}, %%{time_starttransfer}, %%{time_total}, %%{url_effective}" 2> %TEMP%\tmpcliperr.txt 1> %TEMP%\tmpclip.txt
rem curl のエラーメッセージを計測結果に結合
if exist %TEMP%\tmpcliperr.txt (
echo|set /p=, >> %TEMP%\tmpclip.txt
type %TEMP%\tmpcliperr.txt >> %TEMP%\tmpclip.txt
)
rem curl 計測結果を環境変数に格納
for /f "usebackq delims=\\" %%i in (`type %TEMP%\tmpclip.txt`) do set CURL_RESULT=%timestamp%, %%i
del /S /Q %TEMP%\tmpclip*.txt > nul 2>&1
echo %CURL_RESULT_HEADER%
echo !CURL_RESULT!
echo !CURL_RESULT! >> %LOGFILE%
echo.
timeout /T 5 > nul
)
echo.
echo.
echo ■ 計測が完了しました。
echo.
echo.
echo.
echo.
rem pause
echo ■ 計測がすべて完了しました。終了します。
timeout /T 5 /NOBREAK
@t2psyto
Copy link
Author

t2psyto commented Oct 10, 2023

こんな感じのログがとれる。

timestamp, content_type, filename_effective, ftp_entry_path, http_code, http_connect, local_ip, local_port, num_connects, num_redirects, redirect_url, remote_ip, remote_port, size_download, size_header, size_request, size_upload, speed_download, speed_upload, ssl_verify_result, time_appconnect, time_connect, time_namelookup, time_pretransfer, time_redirect, time_starttransfer, time_total, url_effective
20231010_113012, text/html; charset=UTF-8, NUL, , 200, 000, 192.168.1.126, 58716, 2, 1, , 183.79.219.252, 443, 33887, 1326, 152, 0, 53051, 0, 0, 0.515274, 0.094089, 0.074380, 0.515501, 0.272529, 0.604225, 0.638759, https://www.yahoo.co.jp:443/,
20231010_113012, text/html; charset=UTF-8, NUL, , 200, 000, 192.168.1.126, 58727, 2, 1, , 183.79.219.252, 443, 33887, 1326, 152, 0, 73097, 0, 0, 0.325958, 0.100470, 0.074726, 0.326110, 0.188319, 0.428783, 0.463584, https://www.yahoo.co.jp:443/,
20231010_113012, text/html; charset=UTF-8, NUL, , 200, 000, 192.168.1.126, 58735, 2, 1, , 183.79.219.252, 443, 33887, 1326, 152, 0, 72788, 0, 0, 0.330058, 0.087712, 0.073436, 0.330241, 0.191445, 0.429355, 0.465554, https://www.yahoo.co.jp:443/,

@t2psyto
Copy link
Author

t2psyto commented Oct 10, 2023

接続エラーの時はログ末尾にcurlのエラーメッセージがつく。
下記はプロキシに接続できなかった場合のログ

timestamp, content_type, filename_effective, ftp_entry_path, http_code, http_connect, local_ip, local_port, num_connects, num_redirects, redirect_url, remote_ip, remote_port, size_download, size_header, size_request, size_upload, speed_download, speed_upload, ssl_verify_result, time_appconnect, time_connect, time_namelookup, time_pretransfer, time_redirect, time_starttransfer, time_total, url_effective
20231010_113607, , NUL, , 000, 000, , 0, 0, 0, , , 0, 0, 0, 0, 0, 0, 0, 0, 0.000000, 0.000000, 0.000037, 0.000000, 0.000000, 0.000000, 2.038002, https://yahoo.co.jp/, curl: (7) Failed to connect to 192.168.1.2 port 8080 after 2037 ms: Couldn't connect to server
20231010_113607, , NUL, , 000, 000, , 0, 0, 0, , , 0, 0, 0, 0, 0, 0, 0, 0, 0.000000, 0.000000, 0.000039, 0.000000, 0.000000, 0.000000, 2.037337, https://yahoo.co.jp/, curl: (7) Failed to connect to 192.168.1.2 port 8080 after 2037 ms: Couldn't connect to server
20231010_113607, , NUL, , 000, 000, , 0, 0, 0, , , 0, 0, 0, 0, 0, 0, 0, 0, 0.000000, 0.000000, 0.000039, 0.000000, 0.000000, 0.000000, 2.020430, https://yahoo.co.jp/, curl: (7) Failed to connect to 192.168.1.2 port 8080 after 2020 ms: Couldn't connect to server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment