My task is to do periodic requests to server and write time of this request to database. I use Python 2.7.3, cURL and pycurl as wrapper for cURL. I do request like so:
import pycurl
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
c.perform()
But how I can determine the time of this request?
UPD1
Ok, I understand, that I should take 2 timestamps and the diff between them will be the duration of request. But I face some problem:
when I execute:
import pycurl, time
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
ts = time.clock()
c.perform()
print time.clock() - ts
I get 0.0 o (sometimes) 0.01. It's wrong diff, because of I execute this commands in python shell and some time is left after I do ts = time.clock()
and before I do print time.clock() - ts
, so the diff should be about 3 sec.
This output I get on my Ubuntu server 12.04 installed on Virtualbox. Virtualbox installed on Windows 7. When I try code below in windows 7 I get correct output.
Here is another question - maybe I should use time.time() instead of time.clock()?
Solution
Idea was taken from this post . Succinctly, you should use time.clock() on windows and time.time() on Linux or Unix (Ubuntu and others, FreeBSD and others, MacOS). And you can also use timeit.default_timer(). This function detect os type and choose time.time() or time.clock(). So the solution's code is:
import pycurl, timeit
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
ts = timeit.default_timer()
c.perform()
print timeit.default_timer() - ts
import pycurl, time
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
ts = time.clock()
c.perform()
print time.clock() - ts
don't use time.clock() for timing elapsed intervals. It's also now deprecated.
After you do curl.perform()
you can extract information from a curl handle:
m = {}
m['total-time'] = curl.getinfo(pycurl.TOTAL_TIME)
m['namelookup-time'] = curl.getinfo(pycurl.NAMELOOKUP_TIME)
m['connect-time'] = curl.getinfo(pycurl.CONNECT_TIME)
m['pretransfer-time'] = curl.getinfo(pycurl.PRETRANSFER_TIME)
m['redirect-time'] = curl.getinfo(pycurl.REDIRECT_TIME)
m['starttransfer-time'] = curl.getinfo(pycurl.STARTTRANSFER_TIME)
Docs for getinfo(): http://pycurl.io/docs/latest/curlobject.html#pycurl.Curl.getinfo
List of available information to get from request: https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.