Skip to content

Instantly share code, notes, and snippets.

@danillab
Created October 29, 2017 06:48
Show Gist options
  • Save danillab/cdf5f9f2e07f8192067d1c187622ee17 to your computer and use it in GitHub Desktop.
Save danillab/cdf5f9f2e07f8192067d1c187622ee17 to your computer and use it in GitHub Desktop.
Getting TTFB (time till first byte) for an HTTP Request
# https://stackoverflow.com/questions/744532/getting-ttfb-time-till-first-byte-for-an-http-request
import pycurl
import sys
import json
WEB_SITES = sys.argv[1]
def main():
c = pycurl.Curl()
c.setopt(pycurl.URL, WEB_SITES) #set url
c.setopt(pycurl.FOLLOWLOCATION, 1)
content = c.perform() #execute
dns_time = c.getinfo(pycurl.NAMELOOKUP_TIME) #DNS time
conn_time = c.getinfo(pycurl.CONNECT_TIME) #TCP/IP 3-way handshaking time
starttransfer_time = c.getinfo(pycurl.STARTTRANSFER_TIME) #time-to-first-byte time
total_time = c.getinfo(pycurl.TOTAL_TIME) #last requst time
c.close()
data = json.dumps({'dns_time':dns_time,
'conn_time':conn_time,
'starttransfer_time':starttransfer_time,
'total_time':total_time})
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment