本文整理汇总了Python中pyinstrument.Profiler方法的典型用法代码示例。如果您正苦于以下问题:Python pyinstrument.Profiler方法的具体用法?Python pyinstrument.Profiler怎么用?Python pyinstrument.Profiler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyinstrument
的用法示例。
在下文中一共展示了pyinstrument.Profiler方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def main():
p = pyinstrument.Profiler()
p.start()
t_lz4 = timeit.timeit(lambda: StoryData.decode(StoryData(
random_content(), version=StoryData.VERSION_LZ4).encode()), number=10000)
print(t_lz4)
p.stop()
html = p.output_html()
with open('benchmark_story_data_lz4.html', 'w') as f:
f.write(html)
p = pyinstrument.Profiler()
p.start()
t_gzip = timeit.timeit(lambda: StoryData.decode(StoryData(
random_content(), version=StoryData.VERSION_GZIP).encode()), number=10000)
print(t_gzip)
p.stop()
html = p.output_html()
with open('benchmark_story_data_gzip.html', 'w') as f:
f.write(html)
示例2: __call__
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def __call__(self, request: HttpRequest):
response = self._render_profiler_record(request)
if response:
return response
profiler = None
try:
profiler = Profiler()
profiler.start()
response = self.get_response(request)
finally:
if profiler is not None:
profiler.stop()
print(profiler.output_text(unicode=True, color=True))
link = self._output_html(request, profiler)
print(f'* Profiler HTML: {link}\n')
return response
示例3: execute_test_code
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def execute_test_code(xml_string, profiler):
"""
The test code to be executed. If a profiler is defined it is enabled
just before the test code is executed and disabled just after the
code is executed.
"""
if profiler:
if isinstance(profiler, cProfile.Profile):
profiler.enable()
elif isinstance(profiler, Profiler):
profiler.start()
# The code to be tested
tt = _tupletree.xml_to_tupletree_sax(xml_string, "TestData")
parse_cim(tt)
if profiler:
if isinstance(profiler, cProfile.Profile):
profiler.disable()
elif isinstance(profiler, Profiler):
profiler.stop()
示例4: execute_pyinstrument_tests
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def execute_pyinstrument_tests(self):
"""Execute the parse test for all of the input parameters in args.
Since this profiler has no enable or disable concept the profiler
must be enabled for the complete test and the results output
At the end of the test, the profiler results are printed
"""
table_rows = []
profiler = Profiler()
table_rows = self.execute_raw_tests(profiler)
_uprint(None, profiler.output_text(unicode=True, color=True))
if self.logfile:
_uprint(self.logfile, profiler.output_text(unicode=True,
color=True))
return table_rows
示例5: main
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def main(args):
"""
Run local training.
:param args: Dict[str, Any]
:return:
"""
args, log_id_dir, initial_step, logger = Init.main(MODE, args)
R.save_extern_classes(log_id_dir)
container = Local(args, logger, log_id_dir, initial_step)
if args.profile:
try:
from pyinstrument import Profiler
except:
raise ImportError("You must install pyinstrument to use profiling.")
container.nb_step = 10e3
profiler = Profiler()
profiler.start()
try:
container.run()
finally:
if args.profile:
profiler.stop()
print(profiler.output_text(unicode=True, color=True))
container.close()
if args.eval:
from adept.scripts.evaluate import main
eval_args = {
"log_id_dir": log_id_dir,
"gpu_id": 0,
"nb_episode": 30,
}
if args.custom_network:
eval_args["custom_network"] = args.custom_network
main(eval_args)
示例6: start_profiler
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def start_profiler():
import pyinstrument
profiler = pyinstrument.Profiler()
profiler.start()
return profiler
示例7: process_request
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def process_request(self, request):
profile_dir = getattr(settings, 'PYINSTRUMENT_PROFILE_DIR', None)
use_signal = getattr(settings, 'PYINSTRUMENT_USE_SIGNAL', True)
if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET or profile_dir:
profiler = Profiler(use_signal=use_signal)
try:
profiler.start()
request.profiler = profiler
except NotMainThreadError:
raise NotMainThreadError(not_main_thread_message)
示例8: profile_django_context
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def profile_django_context(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
profiler = None
if CONFIG.profiler_enable:
profiler = Profiler()
profiler.start()
try:
return f(*args, **kwargs)
finally:
if profiler is not None:
profiler.stop()
print(profiler.output_text(unicode=True, color=True))
return wrapper
示例9: _output_html
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def _output_html(self, request: HttpRequest, profiler: Profiler):
html = profiler.output_html()
t = int(time.time() * 1000)
key = '{}-{}-{}'.format(t, request.method, request.path)
_PROFILER_RECORDS[key] = html
while len(_PROFILER_RECORDS) > 20:
_PROFILER_RECORDS.popitem(False)
port = request.META['SERVER_PORT']
link = f'http://localhost:{port}/__profiler__/{key}'
return link
示例10: __enter__
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def __enter__(self):
if self.profile:
self.profiler = profiler = Profiler()
profiler.start()
示例11: profile_if_necessary
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def profile_if_necessary(profiler_name, file_path):
if profiler_name == 'pyinstrument':
profiler = pyinstrument.Profiler(use_signal=False)
profiler.start()
try:
yield
finally:
if profiler_name == 'pyinstrument':
profiler.stop()
profiler.save(filename=os.path.join(file_path, 'launch-task.trace'))
示例12: from_pyinstrument_trace
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def from_pyinstrument_trace(file_ref):
import pyinstrument
profiler = pyinstrument.Profiler()
profiler.add(file_ref)
root_frame = profiler.root_frame()
def visit_frame(frame):
code_pos = frame.code_position_short
if code_pos is None:
code_pos = ''
index = code_pos.find('site-packages')
if index > 0:
code_pos = code_pos[index + (len('site-packages') + 1):]
measurement = Measurement(
'{function} {code_pos}'.format(
function=frame.function or '',
code_pos=code_pos
),
self_time=datetime.timedelta(seconds=frame.self_time)
)
for child_frame in frame.children:
measurement.add_child(visit_frame(child_frame))
return measurement
return visit_frame(root_frame)
示例13: profile
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def profile(func):
@wraps(func)
def wrapper(*args, **kwargs):
profiler = Profiler()
profiler.start()
r = func(*args, **kwargs)
profiler.stop()
print profiler.output_text(color=True)
return r
return wrapper
示例14: attach_pyinstrument_profiler
# 需要导入模块: import pyinstrument [as 别名]
# 或者: from pyinstrument import Profiler [as 别名]
def attach_pyinstrument_profiler():
"""Run the pyinstrument profiler in the background and dump its output to
stdout when the process receives SIGTRAP. In general, you probably want to
use the facilities in inbox.util.profiling instead."""
profiler = Profiler()
profiler.start()
def handle_signal(signum, frame):
print profiler.output_text(color=True)
# Work around an arguable bug in pyinstrument in which output gets
# frozen after the first call to profiler.output_text()
delattr(profiler, '_root_frame')
signal.signal(signal.SIGTRAP, handle_signal)