I have a small Python script which sends POST requests to a server and gets their response.
It iterates 10000 times, and I managed to print the current progress in command prompt using:
code=current_requestnumber
print('{0}/{1}'.format(str(code),"10000"),end="\r")
at the end of each loop.
Because this involves interaction with a webserver, I would like to show the current average speed next to this too (updated like every 2 seconds).
An example at the bottom of the command prompt would then be like this:
(1245/10000), 6.3 requests/second
How do I achieve this?
You can get a total average number of events per second like this:
Which in this example would print approximately 10. Please note that I used a
timestamp
method ofdatetime
which is only availble in Python 3.Now, if you would like to calculate the "current" number of events per second, say over the last 10 events, you can do it like this:
Here, I'm keeping a list of durations of last 10 iterations over which I can then use to get the average number of events per second.