Profiling

Profiler implementations

The default implementations of profilers are listed here, these are the classes that should be used by users.

Note

See Profiler for more info on how to use its implementations.

class TracingProfiler(fname)[source]

A profiler that outputs a JSON file that can be read by Chrome Tracing

The results will be stored in a JSON file. The contents of this file, i.e. the profiling results, can be visualized using Chrome Tracing. To open Chrome Tracing, open a window in Google Chrome and type chrome://tracing in the address bar.

Parameters

fname (str) – The name of the file in which the results will be written

Profiler Interface

These are the classes that should be used when creating a custom profiler.

class Profiler[source]

Base class for profilers

To profile a program, create an instance p of one of the Profiler implementations and apply the @p.profile decorator to all functions that should be investigated.

Example:

import plogpro
p = plogpro.TracingProfiler("results.json")

@p.profile
def func()
    # do something ...
    pass

func()

To create a custom profiler, create a subclass of Profiler and implement the method write(self, name, start_time, end_time) that accepts 3 arguments:

  • name: the name of the decorated function

  • start_time: the start time in seconds since Epoch

  • end_time: the end time in seconds since Epoch

If you need to do anything once in the beginning or the and, you can override the setup() and/or teardown() methods respectively.

Warning

The profile() method should not be overwritten.

profile(func)[source]

Decorator to use for profiling a function