Last active
March 25, 2021 18:02
-
-
Save RodrigoCMoraes/e26950d644883d1f051f8b8c2997efd9 to your computer and use it in GitHub Desktop.
compute load time stats for web page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import sys | |
| import json | |
| import pprint | |
| import argparse | |
| import subprocess | |
| """ | |
| Reference: https://blog.cloudflare.com/a-question-of-timing/ | |
| Usage: python3 ttfb.py -u https://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} | |
| """ | |
| 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") | |
| args = parser.parse_args() | |
| if not args.url: | |
| os.system("python3 ttfb.py -h") | |
| sys.exit(1) | |
| 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", | |
| args.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"] | |
| pprint.pprint(result_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment