Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Created June 1, 2018 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfreixes/5e9cccc4f5881de003243879edd5c470 to your computer and use it in GitHub Desktop.
Save pfreixes/5e9cccc4f5881de003243879edd5c470 to your computer and use it in GitHub Desktop.
UVloop tracing test dns tracing
import asyncio
import contextvars
import uvloop
from types import SimpleNamespace
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
trace_context = contextvars.ContextVar('trace_context')
class MyCollector(uvloop.TracingCollector):
def __init__(self, loop=None):
self._loop = loop if loop else asyncio.get_event_loop()
uvloop.TracingCollector.__init__(self)
@property
def loop(self):
return self._loop
## Collector methods
def dns_request_begin(self, host, port, family, type, proto, flags):
ctx = SimpleNamespace(dns_request_begin = self.loop.time())
trace_context.set(ctx)
def dns_request_end(self, data):
delta = loop.time() - trace_context.get().dns_request_begin
print(f"DNS request took {delta}")
async def main(loop):
with uvloop.tracing(MyCollector(loop)):
for i in range(100):
host = await loop.getaddrinfo('google.com', 80)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment