I am interested in measuring the time elapsed during a (synchronous) HTTP request and/or a (synchronous) request to a database on a remote server. After reading this page, my understanding is that time.clock() is an accurate measure of the processor time. But I don't know if "processor time" is relevant in my case, since the CPU would be idling while waiting for the response. In other words:
s0 = time.time()
# send a HTTP request
s1 = time.time()
t0 = time.clock()
# send a HTTP request
t1 = time.clock()
Which one actually measures what I want?
For measuring HTTP response time, I think time.time() is enough.
As others suggested, use timeit if you want to do benchmarking.
I personally haven't used time.clock() before, but after reading the example :
#!/usr/bin/python
import time
def procedure():
time.sleep(2.5)
# measure process time
t0 = time.clock()
procedure()
print time.clock() - t0, "seconds process time"
# measure wall time
t0 = time.time()
procedure()
print time.time() - t0, "seconds wall time"
I don't think time.clock() is appropriate measuring HTTP response time.
One approach is to use New Relic for python. You just install it and enable in application. After that, you will be able to see such charts in your New Relic account. It has a free plan.
Related
How can I measure the full time grpc-python takes to handle a request?
So far the best I can do is:
def Run(self, request, context):
start = time.time()
# service code...
end = time.time()
return myservice_stub.Response()
But this doesn't measure how much time grpc takes to serialize the request, response, to transfer it over the network.. and so on. I'm looking for a way to "hook" into these steps.
You can measure on the client side:
start = time.time()
response = stub.Run(request)
total_end_to_end = time.time() - start
Then you can get the total overhead (serialization, transfer) by reducing the computation of the Run method.
To automate the process, you can add (at least for the sake of the test) the computation time as a field to the myservice_stub.Response.
I am trying to test how long a http request processing code block takes inside my flask controller, here is the sample code I used:
cancelled = []
t0 = time.time()
t1 = time.clock()
users = requests.get('https://www.example.com/users/')
for i in users.json():
user = requests.get('https://www.example.com/user/%s' % i['id]').json()
if user['status'] == 'Cancelled':
cancelled.append(user)
t2 = time.clock()
t3 = time.time()
print t2 - t1
print t3 - t0
Here are the outputs:
2.712326
76.424875021
The second output from the time.time() function matches the actual seconds it took to display the results, so I am not sure why the value from time.clock() is so small?
Edit: My system is OSX and python 2.7, and my question is that why is time.clock() generally considered "better" if time.time() reflects the actual time a user experiences/waits?
Note that as of Python 3.3 time.clock is now deprecated as the behavior is platform dependent. The documentation recommends using time.process_time or time.perf_counter for measuring performance.
Otherwise I would recommend using the timeit module (especially since this allows you much more control over the testing environment)
I need to compare a few CDN services, so I write a short python script to repeatedly send get requests to resources deployed on these CDN, and record the round-trip time. I run the scripts on several PCs in different cities.
This is how I did it:
t0 = time.clock()
r = requests.get(test_cdn_url)
t1 = time.clock()
roundtrip = t1-t0 # in seconds
For most requests, the roundtrip time is within 1 second:200-500ms, but occasionally, it reports a request that finishes in several seconds: 3-5 seconds, once 9 seconds.
Is this just the way it is, or am I using the wrong tool to measure? In other words, does requests lib do something (caching or some heavy-weight operations) that makes the metric totally wrong?
The Response object provides an elapsed attribute:
The amount of time elapsed between sending the request and the arrival
of the response (as a timedelta)
Your code would then look like:
r = requests.get(test_cdn_url)
roundtrip = r.elapsed.total_seconds()
If you're worried that requests is doing anything heavy-weight (or caching), you could always use urllib:
nf = urllib.urlopen(url)
t0 = time.time()
page = nf.read()
t1 = time.time()
nf.close()
roundtrip = t1 - t0
Alternatively, if you include a Cache-Control: no-cache header along with your request, then that should ensure that no caching happens along the way - and your original code should time the request effectively.
I am trying to make and ftp client to upload and download a file in python but I need to log the time each operation takes. Hope can I get help in this site. I saw some code here but it shows just how to upload/download but how can I know the time each operation takes?
You can measure the elapsed time by recording the current time before the up- or download, then calculating the number of seconds since the previous time-stamp after the operation:
import time
start = time.time() # seconds since the UNIX epoch
# upload your file
elapsed = time.time() - start
where elapsed will then be a floating point value measuring seconds since the first time.time() call.
Demo:
>>> import time, random
>>> start = time.time()
>>> time.sleep(random.randrange(20)) # random amount of time to wait
>>> print time.time() - start
20.8919820786
Through a python program, sending a command to specific device and that device is responding on the behalf of the command. Now I have to calculate timing between send and receive (means how much time taking to response of the command ).
Ex.
device ip - 10.0.0.10
transmitting 'L004' command through our local system to 10.0.10.
Receving 'L' response from 10.0.0.10.
So now I have to calculate time difference between start time and end time.
Please provide an API through that I can calculate.
import time
t1 = time.time()
# some time-demanding operations
t2 = time.time()
print "operation took around {0} seconds to complete".format(t2 - t1)
time.time() returns the current unix timestamp as a float number. Store this number at given points of your code and calculate the difference. You will get the time difference in seconds (and fractions).
The timeit standard module makes it easy to do this kind of task.
Just Use "timeit" module. It works with both Python 2 And Python 3
import timeit
start = timeit.default_timer()
#ALL THE PROGRAM STATEMETNS
stop = timeit.default_timer()
execution_time = stop - start
print("Program Executed in "+execution_time) #It returns time in sec
It returns in Seconds and you can have your Execution Time. Simple but you should write these in Main Function which starts program execution. If you want to get the Execution time even when you get error then take your parameter "Start" to it and calculate there like
`def sample_function(start,**kwargs):
try:
#your statements
Except:
#Except Statements
stop = timeit.default_timer()
execution_time = stop - start