Getting TTFB (time till first byte) for an HTTP Request

2024/10/2 1:37:06

Here is a python script that loads a url and captures response time:

import urllib2
import timeopener = urllib2.build_opener()
request = urllib2.Request('http://example.com')start = time.time()
resp = opener.open(request)
resp.read()
ttlb = time.time() - start

Since my timer is wrapped around the whole request/response (including read()), this will give me the TTLB (time to last byte).

I would also like to get the TTFB (time to first byte), but am not sure where to start/stop my timing. Is urllib2 granular enough for me to add TTFB timers? If so, where would they go?

Answer

you should use pycurl, not urllib2

  1. install pyCurl:
    you can use pip / easy_install, or install it from source.

    easy_install pyCurl

    maybe you should be a superuser.

  2. usage:

    import pycurl
    import sys 
    import jsonWEB_SITES = sys.argv[1]def main():c = pycurl.Curl()c.setopt(pycurl.URL, WEB_SITES)              #set urlc.setopt(pycurl.FOLLOWLOCATION, 1)  content = c.perform()                        #execute dns_time = c.getinfo(pycurl.NAMELOOKUP_TIME) #DNS timeconn_time = c.getinfo(pycurl.CONNECT_TIME)   #TCP/IP 3-way handshaking timestarttransfer_time = c.getinfo(pycurl.STARTTRANSFER_TIME)  #time-to-first-byte timetotal_time = c.getinfo(pycurl.TOTAL_TIME)  #last requst timec.close()data = json.dumps({'dns_time':dns_time,         'conn_time':conn_time,        'starttransfer_time':starttransfer_time,    'total_time':total_time})return dataif __name__ == "__main__":    print main()
    
https://en.xdnf.cn/q/70903.html

Related Q&A

accessing kubernetes python api through a pod

so I need to connect to the python kubernetes client through a pod. Ive been trying to use config.load_incluster_config(), basically following the example from here. However its throwing these errors. …

Understanding DictVectorizer in scikit-learn?

Im exploring the different feature extraction classes that scikit-learn provides. Reading the documentation I did not understand very well what DictVectorizer can be used for? Other questions come to …

Parsing RSS with Elementtree in Python

How do you search for namespace-specific tags in XML using Elementtree in Python?I have an XML/RSS document like:<?xml version="1.0" encoding="UTF-8"?> <rss version=&quo…

String module object has no attribute join

So, I want to create a user text input box in Pygame, and I was told to look at a class module called inputbox. So I downloaded inputbox.py and imported into my main game file. I then ran a function in…

TypeError: the JSON object must be str, not Response with Python 3.4

Im getting this error and I cant figure out what the problem is:Traceback (most recent call last):File "C:/Python34/Scripts/ddg.py", line 8, in <module>data = json.loads(r)File "C:…

Redirect while passing message in django

Im trying to run a redirect after I check to see if the user_settings exist for a user (if they dont exist - the user is taken to the form to input and save them).I want to redirect the user to the app…

Django sorting by date(day)

I want to sort models by day first and then by score, meaning Id like to see the the highest scoring Articles in each day. class Article(models.Model):date_modified = models.DateTimeField(blank=True, n…

ImportError: No module named pynotify. While the module is installed

So this error keeps coming back.Everytime I try to tun the script it returns saying:Traceback (most recent call last):File "cli.py", line 11, in <module>import pynotify ImportError: No …

business logic in Django

Id like to know where to put code that doesnt belong to a view, I mean, the logic.Ive been reading a few similar posts, but couldnt arrive to a conclusion. What I could understand is:A View is like a …

Faster alternatives to Pandas pivot_table

Im using Pandas pivot_table function on a large dataset (10 million rows, 6 columns). As execution time is paramount, I try to speed up the process. Currently it takes around 8 secs to process the whol…