Skip to content

Commit

Permalink
Add small utility to profile any function
Browse files Browse the repository at this point in the history
  • Loading branch information
Okroshiashvili committed Mar 1, 2023
1 parent 71efa79 commit 8faeb84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions profiling/profiling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import functools

from pyinstrument.profiler import Profiler


def profile_function(output_file="profile.html"):
"""
Profiles a function execution time.
Parameters
----------
output_file: file to write profile output. Defaults to "profile.html".
"""

def decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
profiler = Profiler()
profiler.start()
result = function(*args, **kwargs)
profiler.stop()
output = profiler.output_html()
with open(output_file, "w") as f:
f.write(output)
return result

return wrapper

return decorator
1 change: 1 addition & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage>=6.4.4
flake8>=3.9.2
isort>=5.8.0
mypy>=0.740
pyinstrument>=4.4.0

0 comments on commit 8faeb84

Please sign in to comment.