Skip to content

Instantly share code, notes, and snippets.

@jqlblue
Last active December 23, 2015 17:09
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 jqlblue/6667098 to your computer and use it in GitHub Desktop.
Save jqlblue/6667098 to your computer and use it in GitHub Desktop.
trace url
#!/bin/bash
#
# curl wrapper returning timing information.
# curl format adapted from
# http://digdeeply.org/archives/05102012.html
# Example usage:
# $ trace-url http://www.apple.com
# $ time trace-url http://www.apple.com -v
set -e
curl_format='{
"content_type": %{content_type},
"filename_effective": %{filename_effective},
"http_code": %{http_code},
"http_connect": %{http_connect},
"local_ip": %{local_ip},
"local_port": %{local_port},
"num_connects": %{num_connects},
"num_redirects": %{num_redirects},
"redirect_url": %{redirect_url},
"url_effective": %{url_effective},
"remote_ip": %{remote_ip},
"remote_port": %{remote_port},
"size_download": %{size_download},
"size_header": %{size_header},
"size_request": %{size_request},
"size_upload": %{size_upload},
"speed_download": %{speed_download},
"speed_upload": %{speed_upload},
"ssl_verify_result": %{ssl_verify_result},
"time_namelookup": %{time_namelookup},
"time_connect": %{time_connect},
"time_appconnect": %{time_appconnect},
"time_pretransfer": %{time_pretransfer},
"time_redirect": %{time_redirect},
"time_starttransfer": %{time_starttransfer},
"time_total": %{time_total}
}'
#exec curl --no-keepalive --no-sessionid --noproxy -s -o /dev/null -w "$curl_format\n" "$@" -v -L --trace-time | awk '{
exec curl --no-keepalive --no-sessionid --noproxy -s -o /dev/null -w "$curl_format\n" "$@" -v -L 2>&1 | awk '
function format_row(row)
{
split(row, row_info, ": ")
info = row_info[2]
return substr(info, 1, length(info) - 1)
}
function format_size(row)
{
return row / 1024" kb"
}
function format_speed(row)
{
return row / 1024" kb/s"
}
BEGIN{
cache_hit = "";
}
{
if (match($0,/< X-Cache/)) {
if (match(tolower($0),/hit from/)) {
getline
split($0, cache_info, "<")
cache_hit = cache_info[2]
}
}
if (match($0,/local_ip/)) {
print "local_ip: "format_row($0)
}
if (match($0,/remote_ip/)) {
print "remote_ip: "format_row($0)
}
if (match($0,/size_download/)) {
print "download_size: "format_size(format_row($0))
}
if (match($0,/speed_download/)) {
print "download_speed: "format_speed(format_row($0))
}
if (match($0,/url_effective/)) {
print "request_url: "format_row($0)
}
if (match($0,/time_total/)) {
print "total_time: "format_row($0)"s"
}
} END {
if (cache_hit) {
print "cache_hit: "cache_hit
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment