Commit 3d48b813 authored by Denis Bilenko's avatar Denis Bilenko

examples/bench_spawn.py: add geventraw and geventpool options

parent a92e7730
...@@ -7,9 +7,10 @@ N = 20000 ...@@ -7,9 +7,10 @@ N = 20000
counter = 0 counter = 0
def incr(**kwargs): def incr(sleep, **kwargs):
global counter global counter
counter += 1 counter += 1
sleep(0)
if not sys.argv[1:]: if not sys.argv[1:]:
sys.exit('USAGE: python bench_spawn.py none|gevent|eventlet [eventlet_hub]') sys.exit('USAGE: python bench_spawn.py none|gevent|eventlet [eventlet_hub]')
...@@ -21,10 +22,26 @@ if sys.argv[-1]=='kwargs': ...@@ -21,10 +22,26 @@ if sys.argv[-1]=='kwargs':
kwargs = {'foo': 1, 'bar': 'hello'} kwargs = {'foo': 1, 'bar': 'hello'}
del sys.argv[-1] del sys.argv[-1]
def noop(p):
pass
def test(spawn, sleep):
start = time()
for _ in xrange(N):
spawn(incr, sleep, **kwargs)
delta = time() - start
print 'spawning: %.1f microseconds per greenlet' % (delta*1000000.0/N)
assert counter == 0, counter
start = time()
sleep(0)
delta = time() - start
assert counter == N, (counter, N)
print 'sleep(0): %.1f microseconds per greenlet' % (delta*1000000.0/N)
if sys.argv[1]=='none': if sys.argv[1]=='none':
start = time() start = time()
for _ in xrange(N): for _ in xrange(N):
incr(**kwargs) incr(noop, **kwargs)
delta = time() - start delta = time() - start
assert counter == N, (counter, N) assert counter == N, (counter, N)
print '%.2f microseconds' % (delta*1000000.0/N) print '%.2f microseconds' % (delta*1000000.0/N)
...@@ -32,49 +49,34 @@ elif sys.argv[1]=='gevent': ...@@ -32,49 +49,34 @@ elif sys.argv[1]=='gevent':
import gevent import gevent
print gevent.__file__ print gevent.__file__
from gevent import spawn, sleep from gevent import spawn, sleep
test(spawn, sleep)
elif sys.argv[1]=='geventraw':
import gevent
print gevent.__file__
from gevent import sleep
from gevent.rawgreenlet import spawn
test(spawn, sleep)
elif sys.argv[1]=='geventpool':
import gevent
print gevent.__file__
from gevent import sleep
from gevent.pool import Pool
p = Pool()
test(p.spawn, sleep)
start = time() start = time()
for _ in xrange(N): p.join()
spawn(incr, **kwargs)
delta = time() - start
print 'spawning: %d microseconds' % (delta*1000000.0/N)
assert counter == 0, counter
start = time()
sleep(0)
delta = time() - start delta = time() - start
assert counter == N, (counter, N) print 'joining: %.1f microseconds per greenlet' % (delta*1000000.0/N)
print 'switching: %d microseconds' % (delta*1000000.0/N)
elif sys.argv[1]=='eventlet': elif sys.argv[1]=='eventlet':
import eventlet import eventlet
print eventlet.__file__ print eventlet.__file__
from eventlet.api import spawn, sleep, use_hub from eventlet.api import spawn, sleep, use_hub
if sys.argv[2:]: if sys.argv[2:]:
use_hub(sys.argv[2]) use_hub(sys.argv[2])
start = time() test(spawn, sleep)
for _ in xrange(N): elif sys.argv[1]=='eventlet1':
spawn(incr, **kwargs)
delta = time() - start
print 'spawning: %d microseconds' % (delta*1000000.0/N)
assert counter == 0, counter
start = time()
sleep(0)
delta = time() - start
assert counter == N, (counter, N)
print 'switching: %d microseconds' % (delta*1000000.0/N)
elif sys.argv[1]=='eventletproc':
from eventlet.proc import spawn_greenlet as spawn from eventlet.proc import spawn_greenlet as spawn
from eventlet.api import sleep, use_hub from eventlet.api import sleep, use_hub
if sys.argv[2:]: if sys.argv[2:]:
use_hub(sys.argv[2]) use_hub(sys.argv[2])
start = time() test(spawn, sleep, sleep)
for _ in xrange(N):
spawn(incr, **kwargs)
delta = time() - start
print 'spawning: %d microseconds' % (delta*1000000.0/N)
assert counter == 0, counter
start = time()
sleep(0)
delta = time() - start
assert counter == N, (counter, N)
print 'switching: %d microseconds' % (delta*1000000.0/N)
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