diff --git a/AUTHORS b/AUTHORS index a89b2ef..4b105ff 100644 --- a/AUTHORS +++ b/AUTHORS @@ -142,3 +142,4 @@ Thanks To * Whitney Young * Matthew D. Pagel * Matt Yule-Bennett +* Artur Stawiarski diff --git a/eventlet/green/profile.py b/eventlet/green/profile.py index e323adc..21ffa51 100644 --- a/eventlet/green/profile.py +++ b/eventlet/green/profile.py @@ -42,7 +42,7 @@ from eventlet import greenthread from eventlet import patcher from eventlet.support import six -thread = patcher.original('thread') # non-monkeypatched module needed +thread = patcher.original(six.moves._thread.__name__) # non-monkeypatched module needed # This class provides the start() and stop() functions @@ -117,13 +117,6 @@ class Profile(profile_orig.Profile): self.trace_dispatch_c_call(frame, 0) return self.trace_dispatch_return(frame, t) - # Add "return safety" to the dispatchers - dispatch = dict(profile_orig.Profile.dispatch) - dispatch.update({ - "return": trace_dispatch_return_extend_back, - "c_return": trace_dispatch_c_return_extend_back, - }) - def SwitchTasklet(self, t0, t1, t): # tally the time spent in the old tasklet pt, it, et, fn, frame, rcur = self.cur @@ -141,19 +134,6 @@ class Profile(profile_orig.Profile): self.simulate_call("profiler") self.simulate_call("new_tasklet") - def ContextWrap(f): - @functools.wraps(f) - def ContextWrapper(self, arg, t): - current = greenthread.getcurrent() - if current != self.current_tasklet: - self.SwitchTasklet(self.current_tasklet, current, t) - t = 0.0 # the time was billed to the previous tasklet - return f(self, arg, t) - return ContextWrapper - - # Add automatic tasklet detection to the callbacks. - dispatch = dict([(key, ContextWrap(val)) for key, val in six.iteritems(dispatch)]) - def TallyTimings(self): oldtimings = self.sleeping self.sleeping = {} @@ -216,6 +196,26 @@ class Profile(profile_orig.Profile): return cur +def ContextWrap(f): + @functools.wraps(f) + def ContextWrapper(self, arg, t): + current = greenthread.getcurrent() + if current != self.current_tasklet: + self.SwitchTasklet(self.current_tasklet, current, t) + t = 0.0 # the time was billed to the previous tasklet + return f(self, arg, t) + return ContextWrapper + + +# Add "return safety" to the dispatchers +Profile.dispatch = dict(profile_orig.Profile.dispatch, **{ + 'return': Profile.trace_dispatch_return_extend_back, + 'c_return': Profile.trace_dispatch_c_return_extend_back, +}) +# Add automatic tasklet detection to the callbacks. +Profile.dispatch = dict((k, ContextWrap(v)) for k, v in six.viewitems(Profile.dispatch)) + + # run statements shamelessly stolen from profile.py def run(statement, filename=None, sort=-1): """Run statement under profiler optionally saving results in filename diff --git a/tests/green_profile_test.py b/tests/green_profile_test.py new file mode 100644 index 0000000..dc6ee5a --- /dev/null +++ b/tests/green_profile_test.py @@ -0,0 +1,9 @@ +import eventlet +from eventlet.green import profile +import tests + + +def test_green_profile_basic(): + statement = 'eventlet.sleep()' + result = profile.Profile().runctx(statement, {'eventlet': eventlet}, {}) + assert ('profile', 0, statement) in result.timings