netskope/measure.py

47 lines
1.3 KiB
Python
Raw Normal View History

2022-07-18 17:28:22 +02:00
#!/usr/bin/env python3
#
# Copyright 2019, Mischa Peters <mischa AT netskope DOT com>, Netskope.
# Version 1.0 - 20191028
#
# Measure timing for DNS lookup as well as HTTP page load
#
# Requires:
# - Python 3.x
#
import argparse
import socket
import time
import ssl
import urllib.request
from urllib.parse import urlparse
parser = argparse.ArgumentParser(description="Measure load times", epilog="2019 (c) Netskope")
parser.add_argument("url", type=str, help="url (eg. https://google.com)")
try:
args = parser.parse_args()
url = args.url
except argparse.ArgumentError as e:
print(str(e))
print (url, "timing:")
urlinfo = urlparse(url)
request_headers = {'Cache-Control': 'no-cache', 'User-Agent': 'Mozilla/5.0'}
no_cert_check = ssl.create_default_context()
no_cert_check.check_hostname=False
no_cert_check.verify_mode=ssl.CERT_NONE
start = time.time()
ip = socket.gethostbyname(urlinfo.netloc)
dns_time = time.time()-start
print ("DNS Lookup:\t{:.3f} seconds".format(dns_time))
start = time.time()
req = urllib.request.Request(url, headers=request_headers)
content = urllib.request.urlopen(req, context=no_cert_check).read()
load_time = time.time()-start
print ("Page Load:\t{:.3f} seconds".format(load_time))
print ("w/o DNS Lookup:\t{:.3f} seconds".format(load_time-dns_time))