Commit 72b28da7 authored by Denis Bilenko's avatar Denis Bilenko

better bench_sleep0.py

parent 65869852
"""Benchmarking sleep(0) performance."""
import sys
from time import time
from gevent import sleep
import os
N = 10
def noop(p):
pass
N = 100000
ARG = 0
while True:
def test(sleep, arg):
start = time()
user_time, system_time = os.times()[:2]
for _ in xrange(N):
sleep(0)
user_time_x, system_time_x = os.times()[:2]
delta = time() - start
if delta > 0.2:
break
N *= 10
user_time_x -= user_time
system_time_x -= system_time
ms = 1000000. / N
print 'N=%s delta=%s utime=%s stime=%s' % (N, delta, user_time_x, system_time_x)
print ('sleep(0): %.1f, utime: %.1f, stime: %.1f (microseconds)' % (delta * ms, user_time_x * ms, system_time_x * ms))
sleep(arg)
return time() - start
def bench_none():
test(noop)
def bench_gevent(arg=0):
import gevent
from gevent import sleep
delta = test(sleep, arg)
print ('gevent %s (%s): sleep(%r): %.1f microseconds' % (gevent.__version__, gevent.__file__, arg, delta * 1000000./N))
def bench_eventlet(arg):
try:
import eventlet
except ImportError, ex:
sys.stderr.write('Failed to import eventlet: %s\n' % ex)
return
from eventlet.api import sleep
delta = test(sleep, arg)
print ('eventlet %s (%s): sleep(%r): %.1f microseconds' % (eventlet.__version__, eventlet.__file__, arg, delta * 1000000./N))
def main():
global N
for arg in [0, -1, 0.00001]:
bench_gevent(arg)
bench_eventlet(arg)
N = 1000
bench_gevent(0.001)
bench_eventlet(0.001)
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment