Belle II Software development
PyProfilingModule Class Reference
Inheritance diagram for PyProfilingModule:
WrapperModule

Public Member Functions

def __init__ (self, module, output_file_name=None)
 
def initialize (self)
 
def event (self)
 
def terminate (self)
 

Public Attributes

 output_file_name
 The output file name the results will be written into.
 
 profiler
 The used profiler instance.
 

Static Public Attributes

str default_output_file_name = "profile.txt"
 The default name for output if none is given.
 

Detailed Description

Wrapper module that evaluates the computational performance of python modules.

Uses cProfile.

Attributes:
  module (basf2.Module): The wrapped module that should be profiled.
    Should be a module written in Python, since the profile interacts
    with the interpretor for the measurements, but cannot look into c++ implementations.

  output_file_name (str, optional): Path to the file where the profiling information
    shall be stored. Defaults to profile.txt.

  profiler (cProfile.Profile): Profiler instance to manage and extract the profiling statistics.

Definition at line 111 of file metamodules.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  module,
  output_file_name = None 
)
Create a new PyProfilingModule wrapped around the given module
   which outputs its results to the output_file_name of given (if not, to profile.txt).

Reimplemented from WrapperModule.

Definition at line 131 of file metamodules.py.

131 def __init__(self, module, output_file_name=None):
132 """Create a new PyProfilingModule wrapped around the given module
133 which outputs its results to the output_file_name of given (if not, to profile.txt)."""
134 super().__init__(module)
135
136
137 self.output_file_name = self.default_output_file_name
138
139 if output_file_name is not None:
140 self.output_file_name = output_file_name
141

Member Function Documentation

◆ event()

def event (   self)
Event method of the module

Reimplemented from WrapperModule.

Definition at line 148 of file metamodules.py.

148 def event(self):
149 """Event method of the module"""
150 profiler = self.profiler
151 profiler.enable()
152 super().event()
153 profiler.disable()
154

◆ initialize()

def initialize (   self)
Initialize method of the module

Reimplemented from WrapperModule.

Definition at line 142 of file metamodules.py.

142 def initialize(self):
143 """Initialize method of the module"""
144
145 self.profiler = cProfile.Profile()
146 super().initialize()
147

◆ terminate()

def terminate (   self)
Terminate method of the module

Reimplemented from WrapperModule.

Definition at line 155 of file metamodules.py.

155 def terminate(self):
156 """Terminate method of the module"""
157 super().terminate()
158 sortby = 'cumulative'
159 with open(self.output_file_name, 'w') as profile_output_file:
160 profile_stats = pstats.Stats(self.profiler, stream=profile_output_file)
161 profile_stats.sort_stats(sortby)
162 profile_stats.print_stats()
163
164

Member Data Documentation

◆ default_output_file_name

str default_output_file_name = "profile.txt"
static

The default name for output if none is given.

Definition at line 129 of file metamodules.py.

◆ output_file_name

output_file_name

The output file name the results will be written into.

Definition at line 137 of file metamodules.py.

◆ profiler

profiler

The used profiler instance.

Definition at line 145 of file metamodules.py.


The documentation for this class was generated from the following file: