2

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
Dmitry Belaventsev
  • 6,347
  • 12
  • 52
  • 75
  • 1
    possible duplicate of [Measuring elapsed time in python](http://stackoverflow.com/questions/7421641/measuring-elapsed-time-in-python) –  Aug 12 '12 at 17:04
  • Are you asking for the _duration_ of the request, the local time the request began, the time when the remote server received it, something else...? – Adrian Petrescu Aug 12 '12 at 17:05
  • @Adrian Petrescu the time between the moment when request sent from local machine and the moment when response reach local machine – Dmitry Belaventsev Aug 12 '12 at 17:08
  • 2
    get hold of the timestamp before and after and take the difference. where is the problem? –  Aug 12 '12 at 17:09
  • @Ashwini Chaudhary Ping determine how fast server make the response by ICMP protocol. Usin curl I measure the time by TCP protocol - how fast web server make the response. – Dmitry Belaventsev Aug 12 '12 at 17:10

3 Answers3

5

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

kwackbars
  • 51
  • 1
  • 3
2
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
  • thx! but here is some problem. I have primary OS win7 and python 2.7.3 installed. I also have virtualbox with ubuntu server 12.04 installed with python 2.7.3. when I execute time.clock() on win7 I get good values like 14.2348885920, but when I execute time.clock() on virtual machine I get values like 0.2, 0.4 and so on (all < 1). – Dmitry Belaventsev Aug 12 '12 at 17:29
  • If I use time.time() it's ok. – Dmitry Belaventsev Aug 12 '12 at 17:29
  • Should we know what you have tried so far? If you have a more specific problem then state this is in your question instead of letting us guess which is clearly a chance for getting downvoted. –  Aug 12 '12 at 17:31
  • I added description of problem with your code to question. So the question is - why I have problems with time.clock() on linux and don't have them on windows? Maybe I should simply use time.time()? – Dmitry Belaventsev Aug 12 '12 at 17:46
0

don't use time.clock() for timing elapsed intervals. It's also now deprecated.

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143