Skip to content

Instantly share code, notes, and snippets.

View queertypes's full-sized avatar

Allele Dev queertypes

  • Georgia, USA
View GitHub Profile
@queertypes
queertypes / microbenchmark-in.py
Created May 2, 2013 16:54
A toy microbenchmark of the performance of 'in' in Python on various data structures.
from timeit import timeit as t
# 1 << 24 = 16 million elements
# Python 2.7
>>> t('needle in x', 'x, needle = range(1<<24), 1<<10')
13.510631084442139
>>> t('x.index(needle)', 'x, needle = range(1<<24), 1<<10')
14.749315023422241
>>> t('needle in x', 'x, needle = set(range(1<<24)), 1<<10')
@queertypes
queertypes / jsonschema-benchmarks.py
Created October 1, 2013 16:39
A series of microbenchmarks for jsonschema in Pythons (2.7.5, 3.3.2, pypy-2.1).
# Microbenchmarks using jsonschema in Python (2.7.5, 3.3.2) and pypy 2.1
## Common code
import jsonschema
schema = {
'type': 'object',
'properties': {
'weight': {'type': 'integer', 'minimum': 1},
'hosts': {'type': 'array', 'minItems': 1, 'items':
@queertypes
queertypes / str-concat.py
Created October 3, 2013 15:42
Microbenchmark of small string concatenation methods in Python 2.7.5, Python 3.3.2, and pypy-2.1.
# Microbenchmark of small string concatenation methods in Python 2.7.5, Python 3.3.2, and pypy-2.1
## Python 2.7.5
In [1]: %timeit '{0}/{1}'.format('taco', 'bell')
1000000 loops, best of 3: 226 ns per loop
In [2]: %timeit '{}/{}'.format('taco', 'bell')
1000000 loops, best of 3: 366 ns per loop
In [3]: %timeit '%s/%s' % ('taco', 'bell')
@queertypes
queertypes / urlparse.py
Last active December 25, 2015 01:19
A benchmark comparing different ways of extracting a scheme from a URI in Python
# A benchmark comparing different ways of extracting a scheme from a URI in Python
# common code
# import statements
uri = 'redis://localhost:27017'
# pypy-2.1: after warm up
In [12]: %timeit urlparse.urlparse(uri).scheme
1000000 loops, best of 3: 298 ns per loop
@queertypes
queertypes / gist:7322535
Created November 5, 2013 17:14
Shard story.
alejandro@rainbow-generator:~:$ http get localhost:8000/v1/shards?detailed=TrueHTTP/1.1 200 OK
Connection: close
Content-Length: 347
Content-Location: /v1/shards?detailed=True
Content-Type: application/json; charset=utf-8
Date: Tue, 05 Nov 2013 17:11:36 GMT
Server: gunicorn/18.0
{
"shards": [
#!/usr/bin/env python
"""summarize: Given Tsung's HTTP Return Code table saved in a file, outputs:
===================
Summary: % of Total
===================
{200: 0.03223020521177586,
201: 0.6133764139909427,
204: 0.3496694087548634,
#!/usr/bin/env python
try:
import pymongo
except ImportError:
print('error: missing `pymongo`: `pip install pymongo`')
quit()
import sys
remove_set = ['messages', 'queues', 'catalogue']
@queertypes
queertypes / udp-statsd-capture.py
Created December 12, 2013 20:21
Simple UDP server to capture statsd repeater-counter events.
import re
import socket
# Handles only counter events
stats_capture = re.compile(r'^(.*):(\d+)\|c$')
s = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
s.bind(('127.0.0.1', 8001))
def wrap(app):
def middleware(env, start_response):
path = env['PATH_INFO']
is_forwarded = env['HTTP_X_FORWARDED_FOR']:
if is_forwarded and path == '/v1/health':
return start_response('200 OK', [])
return app(env, start_response)
return middleware
import random
import time
import uuid
import pyrax
pyrax.set_credential_file('/whatever')
pyrax.queues.client_id = str(uuid.uuid4())