merge
This commit is contained in:
41
benchmarks/hub_timers.py
Normal file
41
benchmarks/hub_timers.py
Normal 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,)
|
@@ -1,4 +1,4 @@
|
|||||||
import bisect
|
import heapq
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
@@ -196,10 +196,10 @@ class BaseHub(object):
|
|||||||
self.timer_finished(timer)
|
self.timer_finished(timer)
|
||||||
|
|
||||||
def prepare_timers(self):
|
def prepare_timers(self):
|
||||||
ins = bisect.insort_right
|
heappush = heapq.heappush
|
||||||
t = self.timers
|
t = self.timers
|
||||||
for item in self.next_timers:
|
for item in self.next_timers:
|
||||||
ins(t, item)
|
heappush(t, item)
|
||||||
del self.next_timers[:]
|
del self.next_timers[:]
|
||||||
|
|
||||||
def schedule_call_local(self, seconds, cb, *args, **kw):
|
def schedule_call_local(self, seconds, cb, *args, **kw):
|
||||||
@@ -229,10 +229,21 @@ class BaseHub(object):
|
|||||||
|
|
||||||
def fire_timers(self, when):
|
def fire_timers(self, when):
|
||||||
t = self.timers
|
t = self.timers
|
||||||
last = bisect.bisect_right(t, (when, 1))
|
heappop = heapq.heappop
|
||||||
|
|
||||||
i = 0
|
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:
|
||||||
try:
|
try:
|
||||||
timer()
|
timer()
|
||||||
@@ -242,7 +253,6 @@ class BaseHub(object):
|
|||||||
self.squelch_timer_exception(timer, sys.exc_info())
|
self.squelch_timer_exception(timer, sys.exc_info())
|
||||||
finally:
|
finally:
|
||||||
self.timer_finished(timer)
|
self.timer_finished(timer)
|
||||||
del t[:last]
|
|
||||||
|
|
||||||
# for debugging:
|
# for debugging:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user