Python pyinstrument.Profiler() Examples

The following are 14 code examples of pyinstrument.Profiler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyinstrument , or try the search function .
Example #1
Source File: benchmark_story_data.py    From rssant with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #2
Source File: profiler.py    From rssant with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #3
Source File: run_response_performance.py    From pywbem with GNU Lesser General Public License v2.1 6 votes vote down vote up
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() 
Example #4
Source File: run_response_performance.py    From pywbem with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 
Example #5
Source File: local.py    From adeptRL with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #6
Source File: __init__.py    From veros with MIT License 5 votes vote down vote up
def start_profiler():
    import pyinstrument
    profiler = pyinstrument.Profiler()
    profiler.start()
    return profiler 
Example #7
Source File: middleware.py    From pyFileFixity with MIT License 5 votes vote down vote up
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) 
Example #8
Source File: actor_helper.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #9
Source File: profiler.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #10
Source File: cli.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __enter__(self):
        if self.profile:
            self.profiler = profiler = Profiler()
            profiler.start() 
Example #11
Source File: local.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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')) 
Example #12
Source File: measure.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example #13
Source File: debug.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
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 
Example #14
Source File: debug.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
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)