Pioneer_11
Pioneer_11

Reputation: 1280

Measuring server query time in python

I've been looking at measuring the time taken for a responce to a server query. I know I can use time.time() but this is a pretty terrible measure due to it's low tick rate, adjustability and it being non-monotonic. However, as (unless I'm mistaken) time.process_time() measures only CPU time and not any time spent at sleep I suspect that also wouldn't work for measuring IO time.

Can anyone suggest a good alternative.

Thanks in advance

Upvotes: 0

Views: 95

Answers (2)

El.
El.

Reputation: 1245

I suggest perf_couner from time.

You can utilize it like below:

from time import perf_counter

start = perf_counter()
# do some IO
end = perf_counter()
# time spent
print(end - start)

Also, you can try Cprofile which gives you more detailed stats.

import cProfile
import pstats
import io
from pstats import SortKey

pr = cProfile.Profile()
pr.enable()

# do some IO

pr.disable()
s = io.StringIO()
sortBy = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortBy)
ps.print_stats()
print(s.getvalue())

Upvotes: 0

Pieter Geelen
Pieter Geelen

Reputation: 548

What library are you using to do requests? If you are doing a API call with requests, the object should provide you with this information.

import requests
resp = requests.get('https://api.github.com/events')
print(resp.elapsed)

Please check out the API docs https://requests.readthedocs.io/en/latest/api/#requests.Response

Upvotes: 1

Related Questions