Skip to content

Instantly share code, notes, and snippets.

@RodrigoCMoraes
Last active March 25, 2021 18:02
Show Gist options
  • Save RodrigoCMoraes/e26950d644883d1f051f8b8c2997efd9 to your computer and use it in GitHub Desktop.
Save RodrigoCMoraes/e26950d644883d1f051f8b8c2997efd9 to your computer and use it in GitHub Desktop.
compute load time stats for web page
import os
import sys
import json
import pprint
import argparse
import subprocess
"""
Reference: https://blog.cloudflare.com/a-question-of-timing/
Usage:
single url:
$> python3 ttfb.py -u www.zasag.mn
$> {'proctime_server': 0.266721,
'tcp_handshake': 0.421435,
'time_appconnect': 1.302903,
'time_connect': 0.423711,
'time_namelookup': 0.002276,
'time_starttransfer': 1.991059,
'ttfb': 0.688156}
single or multiple urls:
$> python3 ttfb.py -U www.zasag.mn
$> {'proctime_server': 0.266721,
'tcp_handshake': 0.421435,
'time_appconnect': 1.302903,
'time_connect': 0.423711,
'time_namelookup': 0.002276,
'time_starttransfer': 1.991059,
'ttfb': 0.688156}
$> python3 ttfb.py -U www.zasag.mn www.zasag.mn
{'proctime_server': 0.3054720000000001,
'tcp_handshake': 0.41525,
'time_appconnect': 1.270451,
'time_connect': 0.417553,
'time_namelookup': 0.002303,
'time_starttransfer': 1.991173,
'ttfb': 0.7207220000000001,
'url': 'www.zasag.mn'}
{'proctime_server': 0.30917799999999995,
'tcp_handshake': 0.400841,
'time_appconnect': 1.295195,
'time_connect': 0.403332,
'time_namelookup': 0.002491,
'time_starttransfer': 2.005214,
'ttfb': 0.710019,
'url': 'www.zasag.mn'}
multiple urls from file:
$> cat urls.txt | xargs python3 ttfb.py -U
{'proctime_server': 0.3054720000000001,
'tcp_handshake': 0.41525,
'time_appconnect': 1.270451,
'time_connect': 0.417553,
'time_namelookup': 0.002303,
'time_starttransfer': 1.991173,
'ttfb': 0.7207220000000001,
'url': 'www.zasag.mn'}
{'proctime_server': 0.30917799999999995,
'tcp_handshake': 0.400841,
'time_appconnect': 1.295195,
'time_connect': 0.403332,
'time_namelookup': 0.002491,
'time_starttransfer': 2.005214,
'ttfb': 0.710019,
'url': 'www.zasag.mn'}
"""
parser = argparse.ArgumentParser(
description="compute Time To First Byte (TTFB) for a given page"
)
parser.add_argument("-u", "--url", type=str, help="url to compute TTFB")
parser.add_argument("-U", "--urls", nargs="+", type=str, help="urls to compute TTFB")
args = parser.parse_args()
if args.url:
urls = [args.url]
elif args.urls:
urls = args.urls
else:
os.system("python3 ttfb.py -h")
sys.exit(1)
for url in urls:
result_raw = subprocess.run(
[
"curl",
"--no-sessionid",
"-w",
'{"time_appconnect": %{time_appconnect}, '
'"time_starttransfer": %{time_starttransfer}, '
'"time_namelookup": %{time_namelookup}, '
'"time_connect": %{time_connect}}',
"-H 'Cache-Control: no-cache'",
"-so",
"/dev/null",
url,
],
capture_output=True,
text=True,
)
result_dict = json.loads(result_raw.stdout)
result_dict["ttfb"] = (
result_dict["time_starttransfer"] - result_dict["time_appconnect"]
)
result_dict["tcp_handshake"] = (
result_dict["time_connect"] - result_dict["time_namelookup"]
)
result_dict["proctime_server"] = result_dict["ttfb"] - result_dict["tcp_handshake"]
result_dict["url"] = url
pprint.pprint(result_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment