Skip to content

Instantly share code, notes, and snippets.

@radix
Created October 10, 2013 23:37
Show Gist options
  • Save radix/6927351 to your computer and use it in GitHub Desktop.
Save radix/6927351 to your computer and use it in GitHub Desktop.
benchmark tool for ersid using treq
from __future__ import print_function
import sys
import time
from twisted.internet.task import cooperate, react, Cooperator
from twisted.internet.defer import gatherResults
import treq
def parallel_map(function, inputs, parallelism, *args):
"""
Like L{map}, but parallel.
C{function} must return a Deferred. It will be called in an undefined
order with every element of C{inputs}. It will be called in parallel
up to the number in C{parallelism}.
"""
cooperator = Cooperator()
work = (function(elem, *args + (cooperator,)) for elem in inputs)
tasks = [cooperator.cooperate(work) for i in xrange(parallelism)]
return gatherResults([task.whenDone() for task in tasks])
def hitit(num, url, cooperator):
print("request number:", num, "current tasks:", len(cooperator._tasks))
start = time.time()
def responseReceived(response):
stop = time.time()
print("received a response for", num, stop - start, "current tasks:", len(cooperator._tasks))
return stop - start
# return treq.get(url).addCallback(treq.text_content).addCallback(responseReceived)
return treq.get(url).addCallback(responseReceived)
def main(reactor, host, port, key, parallelism, requests):
url = "http://%s:%s/%s" % (host, port, key)
result = parallel_map(hitit, range(int(requests)), int(parallelism), url)
def benchmarkDone(timings):
average_response_time = sum(timings) / len(timings)
print("Average response time!", average_response_time)
return result.addCallback(benchmarkDone).addErrback(print)
if __name__ == '__main__':
react(main, sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment