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