This commit is contained in:
Eugene Oden
2010-02-23 21:23:23 -05:00
2 changed files with 58 additions and 7 deletions

41
benchmarks/hub_timers.py Normal file
View File

@@ -0,0 +1,41 @@
#! /usr/bin/env python
# test timer adds & expires on hubs.hub.BaseHub
import sys
import eventlet
import random
import time
from eventlet.hubs import timer, get_hub
timer_count = 100000
if len(sys.argv) >= 2:
timer_count = int(sys.argv[1])
l = []
def work(n):
l.append(n)
timeouts = [random.uniform(0, 10) for x in xrange(timer_count)]
hub = get_hub()
start = time.time()
scheduled = []
for timeout in timeouts:
t = timer.Timer(timeout, work, timeout)
t.schedule()
scheduled.append(t)
hub.prepare_timers()
hub.fire_timers(time.time()+11)
hub.prepare_timers()
end = time.time()
print "Duration: %f" % (end-start,)

View File

@@ -1,4 +1,4 @@
import bisect
import heapq
import sys
import traceback
@@ -196,10 +196,10 @@ class BaseHub(object):
self.timer_finished(timer)
def prepare_timers(self):
ins = bisect.insort_right
heappush = heapq.heappush
t = self.timers
for item in self.next_timers:
ins(t, item)
heappush(t, item)
del self.next_timers[:]
def schedule_call_local(self, seconds, cb, *args, **kw):
@@ -229,10 +229,21 @@ class BaseHub(object):
def fire_timers(self, when):
t = self.timers
last = bisect.bisect_right(t, (when, 1))
heappop = heapq.heappop
i = 0
for i in xrange(last):
timer = t[i][2]
while t:
next = t[0]
exp = next[0]
timer = next[2]
if when < exp:
break
heappop(t)
try:
try:
timer()
@@ -242,7 +253,6 @@ class BaseHub(object):
self.squelch_timer_exception(timer, sys.exc_info())
finally:
self.timer_finished(timer)
del t[:last]
# for debugging: