def profile_session(bias=None):
# we are not running more than one greenlet in this test, so it's fine to
# use python's standard profiler
import cProfile
import profile
import pstats
if bias is None:
bias_profiler = profile.Profile()
runs = [bias_profiler.calibrate(100000) for _ in range(5)]
bias = min(runs)
# the bias should be consistent, otherwise we need to increase the number of iterations
msg = 'Bias calculations: {}, used bias: {}'.format(runs, bias)
print(msg)
profiler = cProfile.Profile()
profiler.bias = bias
profiler.enable()
yield
profiler.create_stats()
stats = pstats.Stats(profiler)
stats.strip_dirs().sort_stats('cumulative').print_stats(15)
stats.strip_dirs().sort_stats('time').print_stats(15)
评论列表
文章目录