Skip to content

Instantly share code, notes, and snippets.

View pfreixes's full-sized avatar

Pau Freixes pfreixes

  • Github
  • Barcelona
View GitHub Profile
@pfreixes
pfreixes / json_fallback.py
Created June 8, 2018 14:54
JSON fallback for protobuf is so slow!
>>> timeit.timeit("""json.dumps({"name": "foo", "id":1})""", setup="from __main__ import json", number=10000)
0.04011281501152553
>>> timeit.timeit("json_format.MessageToJson(person)", setup="from __main__ import json_format, person", number=10000)
0.2599130409944337
>>> person
name: "foo"
id: 1
@pfreixes
pfreixes / test_dns_tracing.py
Created June 1, 2018 07:21
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')
@pfreixes
pfreixes / test_task.py
Created June 1, 2018 04:37
Getting the figures of how much overhead implies having a Collector installed
import asyncio
import contextvars
import uvloop
from types import SimpleNamespace
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
N = 100000
@pfreixes
pfreixes / test_dns_uvloop_tracing.py
Created May 20, 2018 08:01
Snippet that shows how the POC for tracing the uvloop execution works
import asyncio
import contextvars
import uvloop
from types import SimpleNamespace
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
trace_context = contextvars.ContextVar('trace_context')
@pfreixes
pfreixes / test.py
Created March 25, 2018 09:17
Asyncio task hierarchy using an Aiohttp server plus an Aioredis pool.
import aioredis
import asyncio
import json
from aiohttp import web
tasks = {}
task_id = 0
@pfreixes
pfreixes / arena_id.py
Created March 25, 2018 08:54
The id of different Asyncio tasks are the same due to the CPython memory implementation for objects smaller than X bytes
import asyncio
import sys
async def foo():
pass
async def test():
t = asyncio.tasks.create_task(foo())
print(sys.getsizeof(t))
print(id(t))
Python 3.7.0b1 (v3.7.0b1:9561d7f501, Jan 30 2018, 19:10:11)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextvars
>>> foo = contextvars.ContextVar("foo")
>>> foo.set("a")
<Token var=<ContextVar name='foo' at 0x10ccbe2b0> at 0x10ccc36c0>
>>> bar = contextvars.ContextVar("bar", default="b")
>>> list(contextvars.copy_context().keys())
[<ContextVar name='foo' at 0x10ccbe2b0>]
@pfreixes
pfreixes / test_python.py
Created January 26, 2018 20:35
Testing how slow or fast is fetch attributes and call external functions
from timeit import timeit
class Object:
def __init__(self, x):
self.x = x
class Rule:
def __init__(self, valid_values, valid_values2):
self.valid_values = valid_values
self.valid_values2 = valid_values2
@pfreixes
pfreixes / request_tracing.py
Created October 19, 2017 10:59
Simple example of how to use the new request tracing
async def request_start(session, trace_context, method, host, port, headers):
trace_context.start = loop.time()
headers['X-Request-id'] = 'request_id'
async def request_stop(session, trace_context, response):
print("request type {}".format(trace_context.query))
print("request finsihed with {} status code".format(response.status_code))
print("request took {}".format(loop.time() - trace_context.start))
async def request_exception(session, trace_context, exc):
@pfreixes
pfreixes / metrics_client.py
Last active October 19, 2017 10:58
Example of how the new RequestTracing might be used to send the proper metrics at each request and interpose some header
class MetricsClient(ClientSession):
def __init__(self, metric_name, region, **kwargs):
self(MetricsClient, self).__init__(
self,
*args,
request_tracing=True,
request_tracing_ctx=dict
**kwargs)
self.metric_name = metric_name