-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1977 from MoritzBrueckner/profiler
Implement optional profiling for export
- Loading branch information
Showing
3 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import cProfile | ||
import os | ||
import pstats | ||
|
||
import arm.log as log | ||
import arm.utils as utils | ||
|
||
|
||
class Profile: | ||
"""Context manager for profiling the enclosed code when the given condition is true. | ||
The output file is stored in the SDK directory and can be opened by tools such as SnakeViz. | ||
""" | ||
def __init__(self, filename_out: str, condition: bool): | ||
self.filename_out = filename_out | ||
self.condition = condition | ||
self.pr = cProfile.Profile() | ||
|
||
def __enter__(self): | ||
if self.condition: | ||
self.pr.enable() | ||
log.debug("Profiling started") | ||
|
||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
if self.condition: | ||
self.pr.disable() | ||
log.debug("Profiling finished") | ||
|
||
profile_path = os.path.join(utils.get_sdk_path(), self.filename_out) | ||
with open(profile_path, 'w') as profile_file: | ||
stats = pstats.Stats(self.pr, stream=profile_file) | ||
stats.dump_stats(profile_path) | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters