Skip to content

Instantly share code, notes, and snippets.

@bra-fsn
Created June 3, 2016 10:53
Show Gist options
  • Save bra-fsn/08734197601e5a63d6a2b56d7b048119 to your computer and use it in GitHub Desktop.
Save bra-fsn/08734197601e5a63d6a2b56d7b048119 to your computer and use it in GitHub Desktop.
from twisted.internet import reactor
from twisted.internet.threads import deferToThread
from twisted.internet.defer import inlineCallbacks
from time import time
from elasticsearch import Elasticsearch
import sys
es=Elasticsearch(['http://localhost:9201'],sniff_on_start=False,
sniff_on_connection_fail=False)
@inlineCallbacks
def dtt():
t1=0
t2=0
for i in xrange(10000):
st=time()
res = yield deferToThread(es.get,index='idx', doc_type='object',id='id')
t1+=time()-st
st=time()
res = es.get(index='idx', doc_type='object',id='id')
t2+=time()-st
print ("deferToThread: avg %.2f us, sync: avg %.2f us, %.2fx increase" % (t1/i*1000**2, t2/i*1000**2, (t1/i)/(t2/i)))
reactor.callWhenRunning(dtt)
reactor.suggestThreadPoolSize(64)
for i in range(int(sys.argv[1])):
reactor.callInThread(dtt)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment