Created
September 19, 2018 15:13
-
-
Save wyllie/af981101f87173186172bef73d7a200f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 is an interesting way to profile code | |
# Probably would not be too hard to add some foo activate/deactivate it | |
# and then maybe use it in integration test code. The decorator will | |
# produce a file called <function_being_profiled>.profile which can | |
# be processed with 'snakeviz' (pip install snakeviz) | |
import cProfile | |
import functools | |
def profile_this(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
datafn = func.__name__ + ".profile" # Name the data file sensibly | |
prof = cProfile.Profile() | |
retval = prof.runcall(func, *args, **kwargs) | |
prof.dump_stats(datafn) | |
return retval | |
return wrapper | |
# Usage | |
@profile_this | |
def test_the_thing_that_goes_too_slow(): | |
# calls into actual code go here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment