Skip to content

Instantly share code, notes, and snippets.

@greg-barnett
Last active August 8, 2017 11:43
Show Gist options
  • Save greg-barnett/00b67d3b098bc782db642b068ad4b0d3 to your computer and use it in GitHub Desktop.
Save greg-barnett/00b67d3b098bc782db642b068ad4b0d3 to your computer and use it in GitHub Desktop.
aiohttp TimeService compare
import asyncio
import aiohttp
from time import time, gmtime
from aiohttp.helpers import TimeService
def _format_date_time(timestamp):
# Weekday and month names for HTTP date/time formatting;
# always English!
# Tuples are contants stored in codeobject!
_weekdayname = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
_monthname = (None, # Dummy so we can use 1-based month numbers
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
year, month, day, hh, mm, ss, wd, y, z = gmtime(timestamp)
return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
_weekdayname[wd], day, _monthname[month], year, hh, mm, ss
)
async def compare_times(iterations=12, sleep=6):
loop = asyncio.get_event_loop()
time_service = TimeService(loop)
for i in range(iterations):
_ts_time = time_service.time()
_actual_time = time()
_ts_loop = time_service.loop_time
_actual_loop = loop.time()
_ts_strtime = time_service.strtime()
_actual_strtime = _format_date_time(_actual_time)
print("about {} seconds elapsed".format(i * sleep))
print("TimeService timestamp {}".format(_ts_time))
print("actual timestamp {}".format(_actual_time))
print("timestamp diff {:.03f}".format(_ts_time - _actual_time))
print("TimeService loop time {}".format(_ts_loop))
print("actual loop time {}".format(_actual_loop))
print("loop time diff {:.03f}".format(_ts_loop - _actual_loop))
print("TimeService strtime {}".format(_ts_strtime))
print("actual strtime {}".format(_actual_strtime))
if i + 1 < iterations:
print('-------------------------------------')
await asyncio.sleep(sleep)
else:
print('done')
loop = asyncio.get_event_loop()
loop.run_until_complete(compare_times())
loop.close()
about 0 seconds elapsed
TimeService timestamp 1502191089.1840677
actual timestamp 1502191089.1841753
timestamp diff -0.000
TimeService loop time 804053.694846589
actual loop time 804053.694955929
loop time diff -0.000
TimeService strtime Tue, 08 Aug 2017 11:18:09 GMT
actual strtime Tue, 08 Aug 2017 11:18:09 GMT
-------------------------------------
about 60 seconds elapsed
TimeService timestamp 1502191119.1840677
actual timestamp 1502191149.1868014
timestamp diff -30.003
TimeService loop time 804113
actual loop time 804113.697583692
loop time diff -0.698
TimeService strtime Tue, 08 Aug 2017 11:18:39 GMT
actual strtime Tue, 08 Aug 2017 11:19:09 GMT
-------------------------------------
about 120 seconds elapsed
TimeService timestamp 1502191149.1840677
actual timestamp 1502191209.1893532
timestamp diff -60.005
TimeService loop time 804173
actual loop time 804173.700133041
loop time diff -0.700
TimeService strtime Tue, 08 Aug 2017 11:19:09 GMT
actual strtime Tue, 08 Aug 2017 11:20:09 GMT
-------------------------------------
about 180 seconds elapsed
TimeService timestamp 1502191179.1840677
actual timestamp 1502191269.1924062
timestamp diff -90.008
TimeService loop time 804233
actual loop time 804233.703188841
loop time diff -0.703
TimeService strtime Tue, 08 Aug 2017 11:19:39 GMT
actual strtime Tue, 08 Aug 2017 11:21:09 GMT
-------------------------------------
about 240 seconds elapsed
TimeService timestamp 1502191209.1840677
actual timestamp 1502191329.1938696
timestamp diff -120.010
TimeService loop time 804293
actual loop time 804293.704648746
loop time diff -0.705
TimeService strtime Tue, 08 Aug 2017 11:20:09 GMT
actual strtime Tue, 08 Aug 2017 11:22:09 GMT
-------------------------------------
about 300 seconds elapsed
TimeService timestamp 1502191239.1840677
actual timestamp 1502191389.1968107
timestamp diff -150.013
TimeService loop time 804353
actual loop time 804353.707592894
loop time diff -0.708
TimeService strtime Tue, 08 Aug 2017 11:20:39 GMT
actual strtime Tue, 08 Aug 2017 11:23:09 GMT
-------------------------------------
about 360 seconds elapsed
TimeService timestamp 1502191269.1840677
actual timestamp 1502191449.1976101
timestamp diff -180.014
TimeService loop time 804413
actual loop time 804413.708389821
loop time diff -0.708
TimeService strtime Tue, 08 Aug 2017 11:21:09 GMT
actual strtime Tue, 08 Aug 2017 11:24:09 GMT
-------------------------------------
about 420 seconds elapsed
TimeService timestamp 1502191299.1840677
actual timestamp 1502191509.1999466
timestamp diff -210.016
TimeService loop time 804473
actual loop time 804473.710727635
loop time diff -0.711
TimeService strtime Tue, 08 Aug 2017 11:21:39 GMT
actual strtime Tue, 08 Aug 2017 11:25:09 GMT
-------------------------------------
about 480 seconds elapsed
TimeService timestamp 1502191329.1840677
actual timestamp 1502191569.2020059
timestamp diff -240.018
TimeService loop time 804533
actual loop time 804533.712784893
loop time diff -0.713
TimeService strtime Tue, 08 Aug 2017 11:22:09 GMT
actual strtime Tue, 08 Aug 2017 11:26:09 GMT
-------------------------------------
about 540 seconds elapsed
TimeService timestamp 1502191359.1840677
actual timestamp 1502191629.2042341
timestamp diff -270.020
TimeService loop time 804593
actual loop time 804593.715015658
loop time diff -0.715
TimeService strtime Tue, 08 Aug 2017 11:22:39 GMT
actual strtime Tue, 08 Aug 2017 11:27:09 GMT
-------------------------------------
about 600 seconds elapsed
TimeService timestamp 1502191389.1840677
actual timestamp 1502191689.20462
timestamp diff -300.021
TimeService loop time 804653
actual loop time 804653.71539904
loop time diff -0.715
TimeService strtime Tue, 08 Aug 2017 11:23:09 GMT
actual strtime Tue, 08 Aug 2017 11:28:09 GMT
-------------------------------------
about 660 seconds elapsed
TimeService timestamp 1502191419.1840677
actual timestamp 1502191749.207018
timestamp diff -330.023
TimeService loop time 804713
actual loop time 804713.717803234
loop time diff -0.718
TimeService strtime Tue, 08 Aug 2017 11:23:39 GMT
actual strtime Tue, 08 Aug 2017 11:29:09 GMT
done
about 0 seconds elapsed
TimeService timestamp 1502191114.8137217
actual timestamp 1502191114.8137324
timestamp diff -0.000
TimeService loop time 804080
actual loop time 804079.324511472
loop time diff 0.675
TimeService strtime Tue, 08 Aug 2017 11:18:34 GMT
actual strtime Tue, 08 Aug 2017 11:18:34 GMT
-------------------------------------
about 60 seconds elapsed
TimeService timestamp 1502191173.8840427
actual timestamp 1502191174.815397
timestamp diff -0.931
TimeService loop time 804139
actual loop time 804139.326179239
loop time diff -0.326
TimeService strtime Tue, 08 Aug 2017 11:19:33 GMT
actual strtime Tue, 08 Aug 2017 11:19:34 GMT
-------------------------------------
about 120 seconds elapsed
TimeService timestamp 1502191233.9445593
actual timestamp 1502191234.8165975
timestamp diff -0.872
TimeService loop time 804199
actual loop time 804199.327376907
loop time diff -0.327
TimeService strtime Tue, 08 Aug 2017 11:20:33 GMT
actual strtime Tue, 08 Aug 2017 11:20:34 GMT
-------------------------------------
about 180 seconds elapsed
TimeService timestamp 1502191294.0142174
actual timestamp 1502191294.81765
timestamp diff -0.803
TimeService loop time 804259
actual loop time 804259.328431797
loop time diff -0.328
TimeService strtime Tue, 08 Aug 2017 11:21:34 GMT
actual strtime Tue, 08 Aug 2017 11:21:34 GMT
-------------------------------------
about 240 seconds elapsed
TimeService timestamp 1502191354.0745513
actual timestamp 1502191354.8194542
timestamp diff -0.745
TimeService loop time 804319
actual loop time 804319.330234033
loop time diff -0.330
TimeService strtime Tue, 08 Aug 2017 11:22:34 GMT
actual strtime Tue, 08 Aug 2017 11:22:34 GMT
-------------------------------------
about 300 seconds elapsed
TimeService timestamp 1502191414.1438243
actual timestamp 1502191414.8207614
timestamp diff -0.677
TimeService loop time 804379
actual loop time 804379.331541235
loop time diff -0.332
TimeService strtime Tue, 08 Aug 2017 11:23:34 GMT
actual strtime Tue, 08 Aug 2017 11:23:34 GMT
-------------------------------------
about 360 seconds elapsed
TimeService timestamp 1502191474.198687
actual timestamp 1502191474.8224432
timestamp diff -0.624
TimeService loop time 804439
actual loop time 804439.333222388
loop time diff -0.333
TimeService strtime Tue, 08 Aug 2017 11:24:34 GMT
actual strtime Tue, 08 Aug 2017 11:24:34 GMT
-------------------------------------
about 420 seconds elapsed
TimeService timestamp 1502191534.263021
actual timestamp 1502191534.823847
timestamp diff -0.561
TimeService loop time 804499
actual loop time 804499.334627335
loop time diff -0.335
TimeService strtime Tue, 08 Aug 2017 11:25:34 GMT
actual strtime Tue, 08 Aug 2017 11:25:34 GMT
-------------------------------------
about 480 seconds elapsed
TimeService timestamp 1502191594.3077097
actual timestamp 1502191594.8253467
timestamp diff -0.518
TimeService loop time 804559
actual loop time 804559.336126453
loop time diff -0.336
TimeService strtime Tue, 08 Aug 2017 11:26:34 GMT
actual strtime Tue, 08 Aug 2017 11:26:34 GMT
-------------------------------------
about 540 seconds elapsed
TimeService timestamp 1502191654.371176
actual timestamp 1502191654.8270307
timestamp diff -0.456
TimeService loop time 804619
actual loop time 804619.337812587
loop time diff -0.338
TimeService strtime Tue, 08 Aug 2017 11:27:34 GMT
actual strtime Tue, 08 Aug 2017 11:27:34 GMT
-------------------------------------
about 600 seconds elapsed
TimeService timestamp 1502191714.4287581
actual timestamp 1502191714.8282955
timestamp diff -0.400
TimeService loop time 804679
actual loop time 804679.339074536
loop time diff -0.339
TimeService strtime Tue, 08 Aug 2017 11:28:34 GMT
actual strtime Tue, 08 Aug 2017 11:28:34 GMT
-------------------------------------
about 660 seconds elapsed
TimeService timestamp 1502191774.4983428
actual timestamp 1502191774.8290296
timestamp diff -0.331
TimeService loop time 804740
actual loop time 804739.339811069
loop time diff 0.660
TimeService strtime Tue, 08 Aug 2017 11:29:34 GMT
actual strtime Tue, 08 Aug 2017 11:29:34 GMT
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment