ding.utils.profiler_helper¶
ding.utils.profiler_helper
¶
Profiler
¶
Overview
A class for profiling code execution. It can be used as a context manager or a decorator.
Interfaces
__init__, mkdir, write_profile, profile.
__init__()
¶
Overview
Initialize the Profiler object.
mkdir(directory)
¶
OverView
Create a directory if it doesn't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- directory (
|
obj: |
required |
write_profile(pr, folder_path)
¶
OverView
Write the profiling results to files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- pr (
|
obj: |
required | |
- folder_path (
|
obj: |
required |
profile(folder_path='./tmp')
¶
OverView
Enable profiling and save the results to files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- folder_path (
|
obj: |
required |
Full Source Code
../ding/utils/profiler_helper.py
1import atexit 2import pstats 3import io 4import cProfile 5import os 6 7 8def register_profiler(write_profile, pr, folder_path): 9 atexit.register(write_profile, pr, folder_path) 10 11 12class Profiler: 13 """ 14 Overview: 15 A class for profiling code execution. It can be used as a context manager or a decorator. 16 17 Interfaces: 18 ``__init__``, ``mkdir``, ``write_profile``, ``profile``. 19 """ 20 21 def __init__(self): 22 """ 23 Overview: 24 Initialize the Profiler object. 25 """ 26 27 self.pr = cProfile.Profile() 28 29 def mkdir(self, directory: str): 30 """ 31 OverView: 32 Create a directory if it doesn't exist. 33 34 Arguments: 35 - directory (:obj:`str`): The path of the directory to be created. 36 """ 37 if not os.path.exists(directory): 38 os.makedirs(directory) 39 40 def write_profile(self, pr: cProfile.Profile, folder_path: str): 41 """ 42 OverView: 43 Write the profiling results to files. 44 45 Arguments: 46 - pr (:obj:`cProfile.Profile`): The profiler object containing the profiling results. 47 - folder_path (:obj:`str`): The path of the folder where the profiling files will be saved. 48 """ 49 pr.disable() 50 s_tottime = io.StringIO() 51 s_cumtime = io.StringIO() 52 53 ps = pstats.Stats(pr, stream=s_tottime).sort_stats('tottime') 54 ps.print_stats() 55 with open(folder_path + "/profile_tottime.txt", 'w+') as f: 56 f.write(s_tottime.getvalue()) 57 58 ps = pstats.Stats(pr, stream=s_cumtime).sort_stats('cumtime') 59 ps.print_stats() 60 with open(folder_path + "/profile_cumtime.txt", 'w+') as f: 61 f.write(s_cumtime.getvalue()) 62 63 pr.dump_stats(folder_path + "/profile.prof") 64 65 def profile(self, folder_path="./tmp"): 66 """ 67 OverView: 68 Enable profiling and save the results to files. 69 70 Arguments: 71 - folder_path (:obj:`str`): The path of the folder where the profiling files will be saved. \ 72 Defaults to "./tmp". 73 """ 74 self.mkdir(folder_path) 75 self.pr.enable() 76 register_profiler(self.write_profile, self.pr, folder_path)