Skip to content

Instantly share code, notes, and snippets.

@RodrigoCMoraes
Last active March 25, 2021 18:02
Show Gist options
  • Select an option

  • Save RodrigoCMoraes/e26950d644883d1f051f8b8c2997efd9 to your computer and use it in GitHub Desktop.

Select an option

Save RodrigoCMoraes/e26950d644883d1f051f8b8c2997efd9 to your computer and use it in GitHub Desktop.

Revisions

  1. RodrigoCMoraes revised this gist Mar 25, 2021. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions loadtime_webpage_stats.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    Usage:
    single url:
    $> python3 ttfb.py -u https://www.zasag.mn
    $> python3 ttfb.py -u www.zasag.mn
    $> {'proctime_server': 0.266721,
    'tcp_handshake': 0.421435,
    'time_appconnect': 1.302903,
    @@ -19,31 +19,31 @@
    'time_starttransfer': 1.991059,
    'ttfb': 0.688156}
    single or multiple urls:
    $> python3 ttfb.py -U https://www.zasag.mn
    $> 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 https://www.zasag.mn https://www.zasag.mn
    $> 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': 'https://www.zasag.mn'}
    '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': 'https://www.zasag.mn'}
    'url': 'www.zasag.mn'}
    multiple urls from file:
    $> cat urls.txt | xargs python3 ttfb.py -U
    {'proctime_server': 0.3054720000000001,
    @@ -53,15 +53,15 @@
    'time_namelookup': 0.002303,
    'time_starttransfer': 1.991173,
    'ttfb': 0.7207220000000001,
    'url': 'https://www.zasag.mn'}
    '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': 'https://www.zasag.mn'}
    'url': 'www.zasag.mn'}
    """

    parser = argparse.ArgumentParser(
  2. RodrigoCMoraes revised this gist Mar 24, 2021. 1 changed file with 88 additions and 34 deletions.
    122 changes: 88 additions & 34 deletions loadtime_webpage_stats.py
    Original file line number Diff line number Diff line change
    @@ -8,49 +8,103 @@
    """
    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}
    Usage:
    single url:
    $> 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}
    single or multiple urls:
    $> 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}
    $> python3 ttfb.py -U https://www.zasag.mn https://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': 'https://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': 'https://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': 'https://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': 'https://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 not args.url:
    if args.url:
    urls = [args.url]
    elif args.urls:
    urls = args.urls
    else:
    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"]
    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)
    pprint.pprint(result_dict)
  3. RodrigoCMoraes revised this gist Mar 24, 2021. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions loadtime_webpage_stats.py
    Original file line number Diff line number Diff line change
    @@ -20,9 +20,9 @@
    """

    parser = argparse.ArgumentParser(
    description="compute load time stats for web page"
    description="compute Time To First Byte (TTFB) for a given page"
    )
    parser.add_argument("-u", "--url", type=str, help="url to compute load time stats")
    parser.add_argument("-u", "--url", type=str, help="url to compute TTFB")
    args = parser.parse_args()

    if not args.url:
    @@ -38,6 +38,7 @@
    '"time_starttransfer": %{time_starttransfer}, '
    '"time_namelookup": %{time_namelookup}, '
    '"time_connect": %{time_connect}}',
    "-H 'Cache-Control: no-cache'",
    "-so",
    "/dev/null",
    args.url,
    @@ -52,4 +53,4 @@
    )
    result_dict["proctime_server"] = result_dict["ttfb"] - result_dict["tcp_handshake"]

    pprint.pprint(result_dict)
    pprint.pprint(result_dict)
  4. RodrigoCMoraes revised this gist Mar 24, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions loadtime_webpage_stats.py
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@
    import subprocess

    """
    Reference: https://blog.cloudflare.com/a-question-of-timing/
    Usage: python3 ttfb.py -u https://www.zasag.mn
    {'proctime_server': 0.266721,
  5. RodrigoCMoraes created this gist Mar 24, 2021.
    53 changes: 53 additions & 0 deletions loadtime_webpage_stats.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    import os
    import sys
    import json
    import pprint
    import argparse
    import subprocess

    """
    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 load time stats for web page"
    )
    parser.add_argument("-u", "--url", type=str, help="url to compute load time stats")
    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}}',
    "-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)