Most chapters of this book deal with functional issues – that is, issues related to the functionality (or its absence) of the code in question. However, debugging can also involve nonfunctional issues, however – performance, usability, reliability, and more. In this chapter, we give a short introduction on how to debug such nonfunctional issues, notably performance issues.
from bookutils import YouTubeVideo
YouTubeVideo("0tMeB9G0uUI")
Prerequisites
import bookutils.setup
import StatisticalDebugger
import DeltaDebugger
To use the code provided in this chapter, write
>>> from debuggingbook.PerformanceDebugger import <identifier>
and then make use of the following features.
This chapter provides a class PerformanceDebugger
that allows measuring and visualizing the time taken per line in a function.
>>> with PerformanceDebugger(TimeCollector) as debugger:
>>> for i in range(100):
>>> s = remove_html_markup('<b>foo</b>')
The distribution of executed time within each function can be obtained by printing out the debugger:
>>> print(debugger)
238 2% def remove_html_markup(s): # type: ignore
239 2% tag = False
240 1% quote = False
241 1% out = ""
242 0%
243 17% for c in s:
244 15% assert tag or not quote
245 0%
246 14% if c == '<' and not quote:
247 2% tag = True
248 11% elif c == '>' and not quote:
249 3% tag = False
250 8% elif (c == '"' or c == "'") and tag:
251 0% quote = not quote
252 9% elif not tag:
253 5% out = out + c
254 0%
255 2% return out
The sum of all percentages in a function should always be 100%.
These percentages can also be visualized, where darker shades represent higher percentage values:
>>> debugger
238 def remove_html_markup(s): # type: ignore
239 tag = False
240 quote = False
241 out = ""
242
243 for c in s:
244 assert tag or not quote
245
246 if c == '<' and not quote:
247 tag = True
248 elif c == '>' and not quote:
249 tag = False
250 elif (c == '"' or c == "'") and tag:
251 quote = not quote
252 elif not tag:
253 out = out + c
254
255 return out
The abstract MetricCollector
class allows subclassing to build more collectors, such as HitCollector
.
The solution to debugging performance issues fits in two simple rules:
The first part, actually measuring performance, is key here. Developers often take elaborated guesses on which aspects of their code impact performance, and think about all possible ways to optimize their code – and at the same time, making it harder to understand, harder to evolve, and harder to maintain. In most cases, such guesses are wrong. Instead, measure performance of your program, identify the very few parts that may need to get improved, and again measure the impact of your changes.
Almost all programming languages offer a way to measure performance and breaking it down to individual parts of the code – a means also known as profiling. Profiling works by measuring the execution time for each function (or even more fine-grained location) in your program. This can be achieved by
Instrumenting or tracing code such that the current time at entry and exit of each function (or line), thus determining the time spent. In Python, this is achieved by profilers like profile or cProfile
Sampling the current function call stack at regular intervals, and thus assessing which functions are most active (= take the most time) during execution. For Python, the scalene profiler works this way.
Pretty much all programming languages support profiling, either through measuring, sampling, or both. As a rule of thumb, interpreted languages more frequently support measuring (as it is easy to implement in an interpreter), while compiled languages more frequently support sampling (because instrumentation requires recompilation). Python is lucky to support both methods.
Let us illustrate profiling in a simple example. The ChangeCounter
class (which we will encounter in the chapter on mining version histories) reads in a version history from a git repository. Yet, it takes more than a minute to read in the debugging book change history:
from ChangeCounter import ChangeCounter, debuggingbook_change_counter # minor dependency
import Timer
with Timer.Timer() as t:
change_counter = debuggingbook_change_counter(ChangeCounter)
t.elapsed_time()
132.9539235000002
The Python profile
and cProfile
modules offer a simple way to identify the most time-consuming functions. They are invoked using the run()
function, whose argument is the command to be profiled. The output reports, for each function encountered:
ncalls
column)tottime
column)tottime
/ ncalls
(first percall
column)cumtime
column)cumtime
/ percall
(second percall
column)Let us have a look at the profile we obtain:
import cProfile
cProfile.run('debuggingbook_change_counter(ChangeCounter)', sort='cumulative')
20229584 function calls (20086427 primitive calls) in 182.081 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 182.081 182.081 {built-in method builtins.exec} 1 0.000 0.000 182.081 182.081 <string>:1(<module>) 1 0.000 0.000 182.081 182.081 ChangeCounter.ipynb:168(debuggingbook_change_counter) 1 0.000 0.000 182.081 182.081 ChangeCounter.ipynb:51(__init__) 1 1.061 1.061 182.081 182.081 ChangeCounter.ipynb:88(mine) 1172 0.025 0.000 180.790 0.154 ChangeCounter.ipynb:102(mine_commit) 1172 0.037 0.000 180.592 0.154 commit.py:680(modified_files) 1172 0.021 0.000 180.553 0.154 commit.py:696(_get_modified_files) 1122 0.208 0.000 154.573 0.138 diff.py:95(diff) 1165 0.084 0.000 82.444 0.071 cmd.py:1114(_call_process) 1165 0.101 0.000 82.342 0.071 cmd.py:726(execute) 1165 0.183 0.000 81.912 0.070 subprocess.py:753(__init__) 1165 0.229 0.000 81.688 0.070 subprocess.py:1682(_execute_child) 1165 79.249 0.068 79.249 0.068 {built-in method _posixsubprocess.fork_exec} 1123 0.058 0.000 79.224 0.071 cmd.py:638(<lambda>) 1122 14.849 0.013 75.150 0.067 diff.py:445(_index_from_patch_format) 1122 0.065 0.000 47.601 0.042 cmd.py:71(handle_process_output) 11320 47.058 0.004 47.058 0.004 {method 'acquire' of '_thread.lock' objects} 2246 0.014 0.000 46.996 0.021 threading.py:1057(join) 2246 0.019 0.000 46.980 0.021 threading.py:1095(_wait_for_tstate_lock) 1172 0.052 0.000 25.921 0.022 commit.py:730(_parse_diff) 23898 0.027 0.000 24.907 0.001 commit.py:759(_get_undecoded_content) 79263 0.087 0.000 20.358 0.000 cmd.py:528(read) 66944 0.022 0.000 20.301 0.000 base.py:137(read) 158504 20.264 0.000 20.264 0.000 {method 'read' of '_io.BufferedReader' objects} 11949 0.095 0.000 11.865 0.001 diff.py:290(__init__) 11147 0.013 0.000 11.561 0.001 base.py:363(submodules) 11147 0.047 0.000 11.548 0.001 util.py:1092(list_items) 23621/23618 0.030 0.000 11.486 0.000 {method 'extend' of 'list' objects} 33441 0.135 0.000 11.456 0.000 base.py:1228(iter_items) 79263 0.119 0.000 7.002 0.000 db.py:46(stream) 79263 0.188 0.000 6.803 0.000 cmd.py:1263(stream_object_data) 123851 0.388 0.000 5.095 0.000 cmd.py:1235(__get_object_header) 22356 0.019 0.000 4.700 0.000 base.py:129(data_stream) 58029 0.057 0.000 3.970 0.000 util.py:248(__getattr__) 204423 3.916 0.000 3.917 0.000 {method 'readline' of '_io.BufferedReader' objects} 33441 0.027 0.000 3.725 0.000 base.py:527(commit) 123851 0.029 0.000 3.320 0.000 cmd.py:1221(_get_persistent_cmd) 33441 0.018 0.000 3.289 0.000 symbolic.py:212(_get_commit) 33441 0.035 0.000 3.272 0.000 symbolic.py:203(_get_object) 78029/22294 0.046 0.000 3.050 0.000 tree.py:347(__getitem__) 78029/22294 0.159 0.000 3.025 0.000 tree.py:245(join) 44588 0.049 0.000 2.616 0.000 tree.py:224(_set_cache_) 44588 0.103 0.000 2.385 0.000 base.py:75(new_from_sha) 11147 0.049 0.000 2.202 0.000 base.py:196(_config_parser) 44588 0.034 0.000 2.167 0.000 symbolic.py:143(dereference_recursive) 89177 0.066 0.000 2.133 0.000 symbolic.py:196(_get_ref_info) 89177 0.233 0.000 2.067 0.000 symbolic.py:156(_get_ref_info_helper) 44588 0.074 0.000 2.023 0.000 db.py:42(info) 44588 0.036 0.000 1.901 0.000 cmd.py:1243(get_object_header) 1165 1.735 0.001 1.735 0.001 {built-in method posix.read} 11147 0.057 0.000 1.434 0.000 fun.py:191(rev_parse) 11147 0.038 0.000 1.369 0.000 fun.py:121(name_to_object) 13441 0.032 0.000 1.287 0.000 commit.py:196(_set_cache_) 103022 1.170 0.000 1.222 0.000 {built-in method io.open} 11147 0.022 0.000 1.048 0.000 util.py:72(__init__) 89426/78112 0.070 0.000 1.040 0.000 config.py:104(assure_data_present) 11229 0.047 0.000 1.034 0.000 config.py:281(__init__) 11229 0.057 0.000 0.982 0.000 configparser.py:601(__init__) 44588 0.689 0.000 0.903 0.000 fun.py:59(tree_entries_from_data) 1006233 0.774 0.000 0.774 0.000 {method 'decode' of 'bytes' objects} 11229 0.186 0.000 0.771 0.000 configparser.py:1321(__init__) 89426/78197 0.066 0.000 0.763 0.000 config.py:543(read) 1122 0.759 0.001 0.759 0.001 {method 'join' of 'bytes' objects} 11949 0.004 0.000 0.679 0.000 commit.py:749(_get_decoded_str) 11229 0.227 0.000 0.497 0.000 config.py:391(_read) 11229 0.406 0.000 0.406 0.000 {built-in method builtins.dir} 12319 0.096 0.000 0.392 0.000 commit.py:525(_deserialize) 89337 0.267 0.000 0.332 0.000 {method 'read' of '_io.TextIOWrapper' objects} 123851 0.280 0.000 0.281 0.000 {method 'flush' of '_io.BufferedWriter' objects} 1539795 0.259 0.000 0.259 0.000 {method 'match' of 're.Pattern' objects} 13121 0.006 0.000 0.242 0.000 commit.py:587(committer_date) 13121 0.007 0.000 0.236 0.000 commit.py:209(committed_datetime) 11949 0.038 0.000 0.236 0.000 commit.py:155(__init__) 1173 0.002 0.000 0.229 0.000 repository.py:207(traverse_commits) 131755 0.147 0.000 0.227 0.000 posixpath.py:71(join) 1165 0.077 0.000 0.226 0.000 subprocess.py:1222(_close_pipe_fds) 2246 0.013 0.000 0.225 0.000 threading.py:909(start) 2246 0.083 0.000 0.220 0.000 threading.py:820(__init__) 1165 0.039 0.000 0.214 0.000 os.py:711(copy) 123851 0.150 0.000 0.213 0.000 cmd.py:1207(_prepare_ref) 148539 0.158 0.000 0.200 0.000 base.py:50(__init__) 22610 0.018 0.000 0.198 0.000 pathlib.py:955(__new__) 44669 0.051 0.000 0.187 0.000 configparser.py:766(get) 390145 0.111 0.000 0.187 0.000 compat.py:49(safe_decode) 777783 0.126 0.000 0.183 0.000 cmd.py:466(__getattr__) 123851 0.134 0.000 0.181 0.000 cmd.py:1185(_parse_object_header) 22612 0.017 0.000 0.179 0.000 pathlib.py:587(_from_parts) 1176 0.022 0.000 0.178 0.000 repository.py:246(_iter_commits) 90410 0.056 0.000 0.177 0.000 base.py:153(__init__) 22612 0.043 0.000 0.160 0.000 pathlib.py:567(_parse_args) 45760 0.036 0.000 0.154 0.000 tree.py:214(__init__) 24638 0.035 0.000 0.147 0.000 util.py:268(parse_actor_and_date) 100569 0.147 0.000 0.147 0.000 {method '__exit__' of '_io._IOBase' objects} 44588 0.079 0.000 0.144 0.000 util.py:85(get_object_type_by_name) 11229 0.071 0.000 0.141 0.000 configparser.py:1244(__init__) 2247 0.012 0.000 0.139 0.000 threading.py:582(wait) 1016334/1016333 0.137 0.000 0.137 0.000 {built-in method builtins.getattr} 22608 0.092 0.000 0.131 0.000 util.py:69(mode_str_to_int) 58129 0.045 0.000 0.125 0.000 commit.py:84(__init__) 2248 0.010 0.000 0.121 0.000 threading.py:288(wait) 116581 0.050 0.000 0.108 0.000 os.py:674(__getitem__) 22612 0.059 0.000 0.108 0.000 pathlib.py:56(parse_parts) 1760567 0.102 0.000 0.106 0.000 {built-in method builtins.isinstance} 269115 0.065 0.000 0.106 0.000 os.py:804(fsencode) 13121 0.018 0.000 0.096 0.000 util.py:167(from_timestamp) 22294 0.014 0.000 0.086 0.000 base.py:335(index) 2372 0.051 0.000 0.085 0.000 contextlib.py:496(callback) 1165 0.014 0.000 0.081 0.000 util.py:412(remove_password_if_present) 78109 0.061 0.000 0.081 0.000 util.py:163(join_path) 44669 0.039 0.000 0.076 0.000 configparser.py:1143(_unify_values) 45566 0.075 0.000 0.076 0.000 config.py:183(add) 22294 0.016 0.000 0.072 0.000 base.py:117(__init__) 2246 0.072 0.000 0.072 0.000 {built-in method _thread.start_new_thread} 11232 0.013 0.000 0.067 0.000 config.py:489(_has_includes) 71961 0.067 0.000 0.067 0.000 {method 'search' of 're.Pattern' objects} 1123 0.010 0.000 0.066 0.000 util.py:383(finalize_process) 89337 0.049 0.000 0.065 0.000 codecs.py:319(decode) 2247 0.010 0.000 0.065 0.000 threading.py:538(__init__) 117665 0.013 0.000 0.063 0.000 _collections_abc.py:877(__iter__) 31455 0.013 0.000 0.061 0.000 subprocess.py:1770(<genexpr>) 2245 0.039 0.000 0.061 0.000 cmd.py:470(wait) 1165 0.019 0.000 0.060 0.000 os.py:619(get_exec_path) 11352 0.033 0.000 0.056 0.000 parse.py:437(urlsplit) 22294 0.014 0.000 0.056 0.000 base.py:157(_index_path) 24638 0.021 0.000 0.054 0.000 util.py:665(_from_string) 11229 0.016 0.000 0.054 0.000 config.py:492(_included_paths) 22294 0.017 0.000 0.053 0.000 base.py:114(__init__) 44669 0.033 0.000 0.053 0.000 __init__.py:976(__getitem__) 1205 0.009 0.000 0.052 0.000 cmd.py:463(__del__) 89337 0.035 0.000 0.052 0.000 codecs.py:309(__init__) 89177 0.036 0.000 0.052 0.000 symbolic.py:43(_git_dir) 117665 0.027 0.000 0.050 0.000 os.py:697(__iter__) 23898 0.009 0.000 0.050 0.000 diff.py:432(_pick_best_path) 706030 0.050 0.000 0.050 0.000 {built-in method builtins.len} 509679 0.049 0.000 0.049 0.000 {method 'encode' of 'str' objects} 111678 0.040 0.000 0.049 0.000 config.py:192(__getitem__) 204764 0.047 0.000 0.047 0.000 {method 'split' of 'str' objects} 2252 0.047 0.000 0.047 0.000 threading.py:236(__init__) 233000 0.027 0.000 0.046 0.000 os.py:758(decode) 33442 0.015 0.000 0.045 0.000 base.py:342(head) 11949 0.004 0.000 0.045 0.000 ChangeCounter.ipynb:112(include) 13121 0.040 0.000 0.042 0.000 {built-in method fromtimestamp} 22374 0.012 0.000 0.042 0.000 util.py:200(join_path_native) 11949 0.009 0.000 0.041 0.000 ChangeCounter.ipynb:176(filter) 79042 0.041 0.000 0.041 0.000 config.py:180(__setitem__) 2286 0.005 0.000 0.040 0.000 subprocess.py:1199(wait) 23898 0.025 0.000 0.040 0.000 diff.py:54(decode_path) 80 0.000 0.000 0.039 0.000 util.py:98(wrapper) 80 0.000 0.000 0.039 0.000 base.py:1103(module) 81 0.002 0.000 0.039 0.000 base.py:108(__init__) 281131 0.039 0.000 0.039 0.000 {method 'endswith' of 'str' objects} 79263 0.025 0.000 0.038 0.000 base.py:128(__new__) 134652 0.026 0.000 0.038 0.000 posixpath.py:41(_get_sep) 694235 0.037 0.000 0.037 0.000 {method 'append' of 'list' objects} 1205 0.010 0.000 0.037 0.000 cmd.py:421(_terminate) 229688 0.036 0.000 0.036 0.000 {method 'startswith' of 'str' objects} 116581 0.023 0.000 0.036 0.000 os.py:754(encode) 11949 0.019 0.000 0.036 0.000 commit.py:923(_from_change_to_modification_type) 1173 0.000 0.000 0.035 0.000 git.py:110(get_list_commits) 2286 0.004 0.000 0.035 0.000 subprocess.py:1906(_wait) 1165 0.026 0.000 0.034 0.000 contextlib.py:533(__exit__) 22294 0.027 0.000 0.033 0.000 symbolic.py:439(to_full_path) 44589 0.025 0.000 0.033 0.000 <frozen importlib._bootstrap>:1053(_handle_fromlist) 80423 0.033 0.000 0.033 0.000 typing.py:1408(_no_init_or_replace_init) 2246 0.033 0.000 0.033 0.000 threading.py:1294(_make_invoke_excepthook) 135648 0.032 0.000 0.032 0.000 typing.py:305(inner) 44589 0.024 0.000 0.032 0.000 <frozen importlib._bootstrap>:404(parent) 148539 0.031 0.000 0.031 0.000 {method 'split' of 'bytes' objects} 29340 0.009 0.000 0.031 0.000 commit.py:243(new_path) 33443 0.022 0.000 0.030 0.000 head.py:38(__init__) 86018 0.029 0.000 0.029 0.000 {built-in method sys.intern} 1164 0.004 0.000 0.029 0.000 subprocess.py:1893(_try_wait) 216655 0.029 0.000 0.029 0.000 {built-in method binascii.a2b_hex} 22294 0.019 0.000 0.028 0.000 configparser.py:878(has_option) 159625 0.028 0.000 0.028 0.000 {built-in method __new__ of type object at 0x100e370a0} 123983 0.026 0.000 0.026 0.000 {method 'write' of '_io.BufferedWriter' objects} 1246 0.026 0.000 0.026 0.000 {built-in method posix.waitpid} 44588 0.016 0.000 0.025 0.000 base.py:35(__new__) 40 0.000 0.000 0.025 0.001 base.py:1121(module_exists) 1165 0.025 0.000 0.025 0.000 cmd.py:416(__init__) 81 0.000 0.000 0.025 0.000 cmd.py:1273(clear_cache) 79263 0.024 0.000 0.024 0.000 cmd.py:517(__init__) 124315 0.024 0.000 0.024 0.000 {method 'group' of 're.Match' objects} 2244 0.013 0.000 0.023 0.000 threading.py:1191(daemon) 13121 0.019 0.000 0.022 0.000 {method 'astimezone' of 'datetime.datetime' objects} 28859 0.014 0.000 0.022 0.000 pathlib.py:619(__str__) 1165 0.021 0.000 0.022 0.000 warnings.py:458(__enter__) 427923 0.021 0.000 0.021 0.000 {built-in method posix.fspath} 128589 0.020 0.000 0.020 0.000 {built-in method binascii.b2a_hex} 1 0.000 0.000 0.020 0.020 base.py:560(iter_commits) 1 0.000 0.000 0.019 0.019 commit.py:246(iter_items) 2372 0.012 0.000 0.019 0.000 contextlib.py:514(_push_exit_callback) 24638 0.019 0.000 0.019 0.000 util.py:110(utctz_to_altz) 1165 0.007 0.000 0.018 0.000 subprocess.py:1583(_get_handles) 238753 0.018 0.000 0.018 0.000 {method 'strip' of 'str' objects} 32698 0.014 0.000 0.018 0.000 base.py:105(__ne__) 2246 0.013 0.000 0.018 0.000 _weakrefset.py:86(add) 123944 0.018 0.000 0.018 0.000 {method 'startswith' of 'bytes' objects} 89337 0.018 0.000 0.018 0.000 codecs.py:260(__init__) 79263 0.017 0.000 0.017 0.000 base.py:132(__init__) 1165 0.017 0.000 0.017 0.000 contextlib.py:452(__init__) 89337 0.017 0.000 0.017 0.000 {built-in method _codecs.utf_8_decode} 79 0.000 0.000 0.017 0.000 base.py:240(__del__) 113145 0.013 0.000 0.016 0.000 {built-in method builtins.hasattr} 79 0.001 0.000 0.016 0.000 base.py:246(close) 44669 0.016 0.000 0.016 0.000 base.py:296(common_dir) 22376 0.014 0.000 0.016 0.000 configparser.py:644(sections) 135063 0.015 0.000 0.015 0.000 {method 'rstrip' of 'str' objects} 1173 0.001 0.000 0.015 0.000 commit.py:318(_iter_from_process_or_stream) 79263 0.015 0.000 0.015 0.000 cmd.py:604(__del__) 61225 0.015 0.000 0.015 0.000 {method 'groups' of 're.Match' objects} 3537 0.014 0.000 0.014 0.000 {built-in method posix.pipe} 11949 0.010 0.000 0.014 0.000 commit.py:549(committer) 44669 0.014 0.000 0.014 0.000 __init__.py:966(__init__) 2372 0.013 0.000 0.013 0.000 contextlib.py:446(_create_cb_wrapper) 56187 0.013 0.000 0.013 0.000 {built-in method builtins.setattr} 13121 0.013 0.000 0.013 0.000 util.py:147(__init__) 2 0.000 0.000 0.012 0.006 {built-in method builtins.next} 2 0.000 0.000 0.012 0.006 repository.py:173(_prep_repo) 22294 0.010 0.000 0.012 0.000 util.py:34(sm_name) 4703 0.012 0.000 0.012 0.000 {built-in method posix.close} 22926 0.009 0.000 0.011 0.000 diff.py:403(a_path) 10144/1165 0.007 0.000 0.011 0.000 cmd.py:1069(__unpack_args) 745 0.002 0.000 0.011 0.000 ChangeCounter.ipynb:121(update_stats) 44588 0.011 0.000 0.011 0.000 base.py:38(__init__) 6736 0.011 0.000 0.011 0.000 threading.py:546(is_set) 1165 0.006 0.000 0.011 0.000 warnings.py:165(simplefilter) 322/162 0.001 0.000 0.011 0.000 fun.py:85(find_submodule_git_dir) 11352 0.004 0.000 0.010 0.000 parse.py:155(password) 10425 0.008 0.000 0.010 0.000 diff.py:426(renamed_file) 19583 0.010 0.000 0.010 0.000 {method 'get' of 'dict' objects} 172670 0.010 0.000 0.010 0.000 typing.py:1715(cast) 5663 0.010 0.000 0.010 0.000 {built-in method _thread.allocate_lock} 11147 0.008 0.000 0.010 0.000 util.py:970(__new__) 1222 0.009 0.000 0.010 0.000 commit.py:623(parents) 1 0.000 0.000 0.009 0.009 contextlib.py:139(__exit__) 2 0.000 0.000 0.009 0.005 git.py:77(clear) 4492 0.009 0.000 0.009 0.000 threading.py:1176(daemon) 111810 0.009 0.000 0.009 0.000 {function _OMD.__getitem__ at 0x12b4d5900} 55941 0.009 0.000 0.009 0.000 {method 'rpartition' of 'str' objects} 112530 0.009 0.000 0.009 0.000 config.py:387(optionxform) 23328 0.006 0.000 0.008 0.000 diff.py:407(b_path) 33443 0.008 0.000 0.008 0.000 symbolic.py:65(__init__) 4620 0.008 0.000 0.008 0.000 {method 'append' of 'collections.deque' objects} 11465 0.006 0.000 0.008 0.000 pathlib.py:606(_format_parsed_parts) 1122 0.008 0.000 0.008 0.000 {method 'finditer' of 're.Pattern' objects} 81 0.000 0.000 0.008 0.000 base.py:488(config_reader) 1165 0.007 0.000 0.007 0.000 {built-in method posix.access} 4492 0.007 0.000 0.007 0.000 threading.py:1423(current_thread) 22608 0.007 0.000 0.007 0.000 {method 'sub' of 're.Pattern' objects} 11039 0.002 0.000 0.007 0.000 config.py:352(__del__) 24638 0.007 0.000 0.007 0.000 util.py:646(__init__) 2244 0.006 0.000 0.006 0.000 threading.py:775(_newname) 62282 0.006 0.000 0.006 0.000 {method 'readline' of '_io.BytesIO' objects} 11352 0.005 0.000 0.006 0.000 parse.py:189(_userinfo) 89176 0.006 0.000 0.006 0.000 {built-in method builtins.ord} 44588 0.006 0.000 0.006 0.000 base.py:52(type) 1164 0.006 0.000 0.006 0.000 subprocess.py:1060(__del__) 11147 0.006 0.000 0.006 0.000 util.py:973(__init__) 22454 0.006 0.000 0.006 0.000 base.py:290(working_tree_dir) 68332 0.006 0.000 0.006 0.000 {method 'lower' of 'str' objects} 2490 0.003 0.000 0.005 0.000 posixpath.py:150(dirname) 11229 0.004 0.000 0.005 0.000 configparser.py:1363(__iter__) 11147 0.004 0.000 0.005 0.000 base.py:99(__eq__) 44588 0.005 0.000 0.005 0.000 base.py:42(binsha) 2246 0.004 0.000 0.005 0.000 threading.py:1021(_stop) 22612 0.005 0.000 0.005 0.000 pathlib.py:239(splitroot) 1172 0.002 0.000 0.005 0.000 conf.py:257(is_commit_filtered) 403 0.001 0.000 0.005 0.000 fun.py:44(is_git_dir) 1165 0.005 0.000 0.005 0.000 warnings.py:437(__init__) 11352 0.004 0.000 0.005 0.000 parse.py:114(_coerce_args) 4738 0.004 0.000 0.005 0.000 base.py:123(hexsha) 1172 0.002 0.000 0.005 0.000 commit.py:529(hash) 11949 0.004 0.000 0.005 0.000 commit.py:614(msg) 11040 0.003 0.000 0.005 0.000 config.py:364(release) 745 0.005 0.000 0.005 0.000 ChangeCounter.ipynb:137(update_size) 2246 0.004 0.000 0.005 0.000 _weakrefset.py:39(_remove) 2260 0.004 0.000 0.005 0.000 threading.py:264(__enter__) 69408 0.004 0.000 0.004 0.000 {method 'replace' of 'str' objects} 2328 0.003 0.000 0.004 0.000 subprocess.py:1173(poll) 1165 0.002 0.000 0.004 0.000 warnings.py:181(_add_filter) 2244 0.004 0.000 0.004 0.000 threading.py:1162(is_alive) 44588 0.004 0.000 0.004 0.000 base.py:60(size) 11229 0.004 0.000 0.004 0.000 config.py:333(_acquire_lock) 13121 0.004 0.000 0.004 0.000 developer.py:27(__init__) 7066 0.003 0.000 0.004 0.000 conf.py:45(get) 1165 0.004 0.000 0.004 0.000 _collections_abc.py:828(keys) 11147 0.004 0.000 0.004 0.000 fun.py:180(to_commit) 3413 0.004 0.000 0.004 0.000 {method 'add' of 'set' objects} 1246 0.001 0.000 0.004 0.000 abc.py:117(__instancecheck__) 44669 0.004 0.000 0.004 0.000 configparser.py:363(before_get) 745 0.003 0.000 0.004 0.000 ChangeCounter.ipynb:145(update_changes) 1165 0.004 0.000 0.004 0.000 {built-in method sys.exc_info} 22374 0.003 0.000 0.003 0.000 util.py:194(to_native_path_linux) 2248 0.002 0.000 0.003 0.000 threading.py:279(_is_owned) 1246 0.003 0.000 0.003 0.000 {built-in method _abc._abc_instancecheck} 56145 0.003 0.000 0.003 0.000 {built-in method builtins.callable} 243 0.001 0.000 0.003 0.000 util.py:400(expand_path) 811 0.002 0.000 0.003 0.000 posixpath.py:337(normpath) 30290 0.003 0.000 0.003 0.000 {method 'endswith' of 'bytes' objects} 1 0.000 0.000 0.003 0.003 contextlib.py:130(__enter__) 2372 0.001 0.000 0.003 0.000 contextlib.py:448(_exit_wrapper) 52484 0.003 0.000 0.003 0.000 util.py:160(dst) 1136 0.003 0.000 0.003 0.000 {built-in method posix.stat} 2248 0.002 0.000 0.003 0.000 threading.py:273(_release_save) 40535 0.003 0.000 0.003 0.000 util.py:154(utcoffset) 1 0.000 0.000 0.003 0.003 git.py:39(__init__) 1 0.000 0.000 0.002 0.002 git.py:86(_open_repository) 808 0.001 0.000 0.002 0.000 genericpath.py:39(isdir) 12277 0.002 0.000 0.002 0.000 {method 'join' of 'str' objects} 101 0.000 0.000 0.002 0.000 parse.py:88(clear_cache) 22376 0.002 0.000 0.002 0.000 {method 'keys' of 'collections.OrderedDict' objects} 202 0.002 0.000 0.002 0.000 {method 'clear' of 'dict' objects} 2244 0.001 0.000 0.002 0.000 base.py:115(__str__) 1166 0.002 0.000 0.002 0.000 {built-in method builtins.sorted} 1169 0.002 0.000 0.002 0.000 {method 'remove' of 'list' objects} 11229 0.002 0.000 0.002 0.000 configparser.py:1191(converters) 11043 0.002 0.000 0.002 0.000 config.py:707(read_only) 2245 0.001 0.000 0.002 0.000 encoding.py:1(force_bytes) 12319 0.002 0.000 0.002 0.000 {method 'read' of '_io.BytesIO' objects} 11227 0.002 0.000 0.002 0.000 base.py:309(bare) 10425 0.001 0.000 0.001 0.000 diff.py:411(rename_from) 81 0.000 0.000 0.001 0.000 configparser.py:827(getboolean) 3492 0.001 0.000 0.001 0.000 subprocess.py:1858(_internal_poll) 1165 0.001 0.000 0.001 0.000 cmd.py:1058(transform_kwargs) 1173 0.001 0.000 0.001 0.000 __init__.py:1467(info) 13548 0.001 0.000 0.001 0.000 {method 'strip' of 'bytes' objects} 2248 0.001 0.000 0.001 0.000 threading.py:276(_acquire_restore) 245 0.000 0.000 0.001 0.000 posixpath.py:376(abspath) 11949 0.001 0.000 0.001 0.000 {method 'end' of 're.Match' objects} 22613 0.001 0.000 0.001 0.000 {method 'reverse' of 'list' objects} 1164 0.001 0.000 0.001 0.000 subprocess.py:1846(_handle_exitstatus) 81 0.000 0.000 0.001 0.000 configparser.py:806(_get_conv) 1165 0.001 0.000 0.001 0.000 cmd.py:1148(<dictcomp>) 2260 0.001 0.000 0.001 0.000 threading.py:267(__exit__) 81 0.000 0.000 0.001 0.000 configparser.py:803(_get) 4661 0.001 0.000 0.001 0.000 {method 'items' of 'dict' objects} 1165 0.001 0.000 0.001 0.000 warnings.py:477(__exit__) 2015 0.001 0.000 0.001 0.000 <string>:1(<lambda>) 1165 0.001 0.000 0.001 0.000 __init__.py:1455(debug) 1209 0.000 0.000 0.001 0.000 cmd.py:180(dashify) 11633 0.001 0.000 0.001 0.000 {method 'pop' of 'list' objects} 81 0.000 0.000 0.001 0.000 cmd.py:612(__init__) 10425 0.001 0.000 0.001 0.000 diff.py:415(rename_to) 1122 0.001 0.000 0.001 0.000 base.py:90(_set_cache_) 2338 0.001 0.000 0.001 0.000 __init__.py:1724(isEnabledFor) 1172 0.001 0.000 0.001 0.000 commit.py:538(author) 2245 0.001 0.000 0.001 0.000 cmd.py:632(__getattr__) 2328 0.001 0.000 0.001 0.000 {method 'close' of '_io.BufferedReader' objects} 11229 0.001 0.000 0.001 0.000 {built-in method builtins.iter} 10827 0.001 0.000 0.001 0.000 {method 'start' of 're.Match' objects} 2372 0.001 0.000 0.001 0.000 {method 'pop' of 'collections.deque' objects} 3 0.000 0.000 0.001 0.000 config.py:659(write) 11352 0.001 0.000 0.001 0.000 parse.py:103(_noop) 81 0.000 0.000 0.001 0.000 db.py:37(__init__) 1325 0.001 0.000 0.001 0.000 {method 'rfind' of 'str' objects} 1165 0.001 0.000 0.001 0.000 subprocess.py:246(_cleanup) 2/1 0.000 0.000 0.001 0.001 config.py:117(flush_changes) 1165 0.001 0.000 0.001 0.000 {method 'rfind' of 'bytes' objects} 1165 0.001 0.000 0.001 0.000 cmd.py:1156(<listcomp>) 1165 0.001 0.000 0.001 0.000 contextlib.py:530(__enter__) 4492 0.001 0.000 0.001 0.000 {built-in method _thread.get_ident} 41 0.000 0.000 0.001 0.000 subprocess.py:2093(terminate) 2015 0.001 0.000 0.001 0.000 {method 'find' of 'str' objects} 245 0.000 0.000 0.001 0.000 genericpath.py:27(isfile) 1 0.000 0.000 0.001 0.001 base.py:511(config_writer) 81 0.000 0.000 0.001 0.000 genericpath.py:16(exists) 81 0.000 0.000 0.001 0.000 loose.py:77(__init__) 4576 0.001 0.000 0.001 0.000 {method 'release' of '_thread.lock' objects} 1 0.000 0.000 0.001 0.001 config.py:791(set_value) 4 0.000 0.000 0.001 0.000 util.py:878(_obtain_lock) 4 0.000 0.000 0.001 0.000 util.py:856(_obtain_lock_or_raise) 41 0.001 0.000 0.001 0.000 {method 'close' of '_io.BufferedWriter' objects} 1 0.001 0.001 0.001 0.001 {built-in method posix.open} 1165 0.000 0.000 0.000 0.000 cmd.py:1149(<dictcomp>) 1172 0.000 0.000 0.000 0.000 git.py:134(get_commit_from_gitpython) 41 0.000 0.000 0.000 0.000 subprocess.py:2061(send_signal) 81 0.000 0.000 0.000 0.000 re.py:197(search) 2252 0.000 0.000 0.000 0.000 {method '__enter__' of '_thread.lock' objects} 4531 0.000 0.000 0.000 0.000 {method 'insert' of 'list' objects} 82 0.000 0.000 0.000 0.000 base.py:464(_get_config_path) 1165 0.000 0.000 0.000 0.000 _collections_abc.py:854(__init__) 3428 0.000 0.000 0.000 0.000 {method '__exit__' of '_thread.lock' objects} 2 0.000 0.000 0.000 0.000 pathlib.py:1062(resolve) 1 0.000 0.000 0.000 0.000 repository.py:240(<dictcomp>) 4 0.000 0.000 0.000 0.000 thread.py:161(submit) 2246 0.000 0.000 0.000 0.000 {method 'discard' of 'set' objects} 2254 0.000 0.000 0.000 0.000 {method '__exit__' of '_thread.RLock' objects} 2 0.000 0.000 0.000 0.000 posixpath.py:391(realpath) 2 0.000 0.000 0.000 0.000 posixpath.py:400(_joinrealpath) 81 0.000 0.000 0.000 0.000 re.py:288(_compile) 4 0.000 0.000 0.000 0.000 thread.py:180(_adjust_thread_count) 81 0.000 0.000 0.000 0.000 _collections_abc.py:820(__contains__) 407 0.000 0.000 0.000 0.000 posixpath.py:60(isabs) 41 0.000 0.000 0.000 0.000 {built-in method posix.kill} 8 0.000 0.000 0.000 0.000 {built-in method posix.lstat} 3495 0.000 0.000 0.000 0.000 {built-in method _warnings._filters_mutated} 1 0.000 0.000 0.000 0.000 repository.py:256(_split_in_chunks) 81 0.000 0.000 0.000 0.000 base.py:113(__init__) 1 0.000 0.000 0.000 0.000 {built-in method math.ceil} 3 0.000 0.000 0.000 0.000 config.py:615(_write) 2015 0.000 0.000 0.000 0.000 parse.py:419(_checknetloc) 45 0.000 0.000 0.000 0.000 config.py:618(write_section) 80 0.000 0.000 0.000 0.000 base.py:197(abspath) 79 0.000 0.000 0.000 0.000 mman.py:408(collect) 1172 0.000 0.000 0.000 0.000 commit.py:503(__init__) 243 0.000 0.000 0.000 0.000 posixpath.py:228(expanduser) 1207 0.000 0.000 0.000 0.000 {method 'update' of 'dict' objects} 1164 0.000 0.000 0.000 0.000 {built-in method posix.WIFSTOPPED} 1165 0.000 0.000 0.000 0.000 {built-in method sys.audit} 46 0.000 0.000 0.000 0.000 cmd.py:1042(transform_kwarg) 1 0.000 0.000 0.000 0.000 _base.py:636(__exit__) 1164 0.000 0.000 0.000 0.000 {built-in method posix.waitstatus_to_exitcode} 1122 0.000 0.000 0.000 0.000 util.py:257(_set_cache_) 79 0.000 0.000 0.000 0.000 mman.py:303(_collect_lru_region) 1 0.000 0.000 0.000 0.000 thread.py:216(shutdown) 241 0.000 0.000 0.000 0.000 cmd.py:368(is_cygwin) 1122 0.000 0.000 0.000 0.000 diff.py:86(_process_diff_args) 243 0.000 0.000 0.000 0.000 posixpath.py:284(expandvars) 5 0.000 0.000 0.000 0.000 _base.py:201(as_completed) 2251 0.000 0.000 0.000 0.000 {method 'locked' of '_thread.lock' objects} 1165 0.000 0.000 0.000 0.000 {method 'pop' of 'dict' objects} 1 0.000 0.000 0.000 0.000 git.py:92(_discover_main_branch) 81 0.000 0.000 0.000 0.000 base.py:70(__init__) 1 0.000 0.000 0.000 0.000 base.py:792(active_branch) 81 0.000 0.000 0.000 0.000 configparser.py:1163(_convert_to_boolean) 1 0.000 0.000 0.000 0.000 symbolic.py:288(_get_reference) 745 0.000 0.000 0.000 0.000 ChangeCounter.ipynb:155(update_elems) 80 0.000 0.000 0.000 0.000 base.py:266(__ne__) 45 0.000 0.000 0.000 0.000 config.py:216(items_all) 1 0.000 0.000 0.000 0.000 thread.py:123(__init__) 45 0.000 0.000 0.000 0.000 config.py:218(<listcomp>) 727 0.000 0.000 0.000 0.000 {built-in method _stat.S_ISDIR} 2 0.000 0.000 0.000 0.000 weakref.py:370(remove) 88 0.000 0.000 0.000 0.000 config.py:786(_value_to_string) 2 0.000 0.000 0.000 0.000 util.py:883(_release_lock) 1 0.000 0.000 0.000 0.000 util.py:139(rmfile) 80 0.000 0.000 0.000 0.000 base.py:261(__eq__) 4 0.000 0.000 0.000 0.000 threading.py:421(acquire) 241 0.000 0.000 0.000 0.000 util.py:347(is_cygwin_git) 1 0.000 0.000 0.000 0.000 {built-in method posix.remove} 132 0.000 0.000 0.000 0.000 config.py:209(getall) 6 0.000 0.000 0.000 0.000 _base.py:179(_yield_finished_futures) 1 0.000 0.000 0.000 0.000 repository.py:44(__init__) 1 0.000 0.000 0.000 0.000 symbolic.py:685(from_path) 1 0.000 0.000 0.000 0.000 conf.py:77(sanity_check_filters) 3 0.000 0.000 0.000 0.000 config.py:212(items) 3 0.000 0.000 0.000 0.000 config.py:214(<listcomp>) 163 0.000 0.000 0.000 0.000 {built-in method _stat.S_ISREG} 1 0.000 0.000 0.000 0.000 threading.py:415(__init__) 2 0.000 0.000 0.000 0.000 threading.py:793(_maintain_shutdown_locks) 4 0.000 0.000 0.000 0.000 thread.py:47(__init__) 2 0.000 0.000 0.000 0.000 pathlib.py:1090(stat) 4 0.000 0.000 0.000 0.000 _base.py:418(result) 4 0.000 0.000 0.000 0.000 pathlib.py:629(__fspath__) 88 0.000 0.000 0.000 0.000 encoding.py:11(force_text) 4 0.000 0.000 0.000 0.000 _base.py:318(__init__) 1 0.000 0.000 0.000 0.000 _base.py:157(_create_and_install_waiters) 57 0.000 0.000 0.000 0.000 {method 'find' of 'bytes' objects} 1 0.000 0.000 0.000 0.000 conf.py:191(build_args) 57 0.000 0.000 0.000 0.000 {method 'rstrip' of 'bytes' objects} 1 0.000 0.000 0.000 0.000 _base.py:146(__init__) 1 0.000 0.000 0.000 0.000 _base.py:79(__init__) 1 0.000 0.000 0.000 0.000 conf.py:287(_check_timezones) 79 0.000 0.000 0.000 0.000 {method 'values' of 'dict' objects} 1 0.000 0.000 0.000 0.000 conf.py:24(__init__) 1 0.000 0.000 0.000 0.000 conf.py:293(_replace_timezone) 5 0.000 0.000 0.000 0.000 {method 'put' of '_queue.SimpleQueue' objects} 81 0.000 0.000 0.000 0.000 {built-in method builtins.issubclass} 2 0.000 0.000 0.000 0.000 repository.py:148(_is_remote) 1 0.000 0.000 0.000 0.000 _base.py:63(__init__) 2 0.000 0.000 0.000 0.000 weakref.py:428(__setitem__) 7 0.000 0.000 0.000 0.000 conf.py:36(set_value) 1 0.000 0.000 0.000 0.000 {method 'replace' of 'datetime.datetime' objects} 1 0.000 0.000 0.000 0.000 reference.py:46(__init__) 1 0.000 0.000 0.000 0.000 conf.py:65(_check_only_one_from_commit) 1 0.000 0.000 0.000 0.000 configparser.py:892(set) 1 0.000 0.000 0.000 0.000 _base.py:153(__exit__) 8 0.000 0.000 0.000 0.000 util.py:851(_has_lock) 1 0.000 0.000 0.000 0.000 git.py:322(__del__) 1 0.000 0.000 0.000 0.000 contextlib.py:279(helper) 1 0.000 0.000 0.000 0.000 _base.py:149(__enter__) 4 0.000 0.000 0.000 0.000 threading.py:90(RLock) 8 0.000 0.000 0.000 0.000 {method 'partition' of 'str' objects} 1 0.000 0.000 0.000 0.000 util.py:844(__del__) 8 0.000 0.000 0.000 0.000 {method '__enter__' of '_thread.RLock' objects} 2 0.000 0.000 0.000 0.000 threading.py:803(<listcomp>) 2 0.000 0.000 0.000 0.000 conf.py:181(only_one_filter) 1 0.000 0.000 0.000 0.000 threading.py:572(clear) 1 0.000 0.000 0.000 0.000 contextlib.py:102(__init__) 1 0.000 0.000 0.000 0.000 conf.py:71(_check_only_one_to_commit) 4 0.000 0.000 0.000 0.000 _base.py:388(__get_result) 3 0.000 0.000 0.000 0.000 config.py:699(_assure_writable) 1 0.000 0.000 0.000 0.000 reference.py:100(name) 1 0.000 0.000 0.000 0.000 conf.py:54(_sanity_check_repos) 4 0.000 0.000 0.000 0.000 _base.py:225(<genexpr>) 2 0.000 0.000 0.000 0.000 {method 'difference_update' of 'set' objects} 5 0.000 0.000 0.000 0.000 {method 'remove' of 'set' objects} 2 0.000 0.000 0.000 0.000 util.py:847(_lock_file_path) 3 0.000 0.000 0.000 0.000 git.py:63(repo) 4 0.000 0.000 0.000 0.000 {built-in method time.monotonic} 1 0.000 0.000 0.000 0.000 pathlib.py:708(name) 8 0.000 0.000 0.000 0.000 {built-in method _stat.S_ISLNK} 4 0.000 0.000 0.000 0.000 {method 'lstrip' of 'str' objects} 1 0.000 0.000 0.000 0.000 conf.py:114(_check_correct_filters_order) 2 0.000 0.000 0.000 0.000 pathlib.py:1430(expanduser) 1 0.000 0.000 0.000 0.000 conf.py:142(get_starting_commit) 1 0.000 0.000 0.000 0.000 conf.py:165(get_ending_commit) 2 0.000 0.000 0.000 0.000 conf.py:189(<listcomp>) 4 0.000 0.000 0.000 0.000 {method 'acquire' of '_thread.RLock' objects} 1 0.000 0.000 0.000 0.000 util.py:840(__init__) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 2 0.000 0.000 0.000 0.000 {method 'remove' of 'collections.deque' objects} 1 0.000 0.000 0.000 0.000 configparser.py:663(has_section) 4 0.000 0.000 0.000 0.000 {method 'release' of '_thread.RLock' objects} 1 0.000 0.000 0.000 0.000 __init__.py:230(utcoffset) 1 0.000 0.000 0.000 0.000 _base.py:633(__enter__) 1 0.000 0.000 0.000 0.000 configparser.py:366(before_set)
Yes, that's an awful lot of functions, but we can quickly narrow things down. The cumtime
column is sorted by largest values first. We see that the debuggingbook_change_counter()
method at the top takes up all the time – but this is not surprising, since it is the method we called in the first place. This calls a method mine()
in the ChangeCounter
class, which does all the work.
The next places are more interesting: almost all time is spent in a single method, named modifications()
. This method determines the difference between two versions, which is an expensive operation; this is also supported by the observation that half of the time is spent in a diff()
method.
This profile thus already gets us a hint on how to improve performance: Rather than computing the diff between versions for every version, we could do so on demand (and possibly cache results so we don't have to compute them twice). Alas, this (slow) functionality is part of the
underlying PyDriller Python package, so we cannot fix this within the ChangeCounter
class. But we could file a bug with the developers, suggesting a patch to improve performance.
Instrumenting code is precise, but it is also slow. An alternate way to measure performance is to sample in regular intervals which functions are currently active – for instance, by examining the current function call stack. The more frequently a function is sampled as active, the more time is spent in that function.
One profiler for Python that implements such sampling is Scalene – a high-performance, high-precision CPU, GPU, and memory profiler for Python. We can invoke it on our example as follows:
$ scalene --html test.py > scalene-out.html
where test.py
is a script that again invokes
debuggingbook_change_counter(ChangeCounter)
The output of scalene
is sent to a HTML file (here, scalene-out.html
) which is organized by lines – that is, for each line, we see how much it contributed to overall execution time. Opening the output scalene-out.html
in a HTML browser, we see these lines:
As with cProfile
, above, we identify the mine()
method in the ChangeCounter
class as the main performance hog – and in the mine()
method, it is the iteration over all modifications that takes all the time. Adding the option --profile-all
to scalene
would extend the profile to all executed code, including the pydriller
third-party library.
Besides relying on sampling rather that tracing (which is more efficient) and breaking down execution time by line, scalene
also provides additional information on memory usage and more. If cProfile
is not sufficient, then scalene
will bring profiling to the next level.
Identifying a culprit is not always that easy. Notably, when the first set of obvious performance hogs is fixed, it becomes more and more difficult to squeeze out additional performance – and, as stated above, such optimization may be in conflict with readability and maintainability of your code. Here are some simple ways to improve performance:
Efficient algorithms. For many tasks, the simplest algorithm is not always the best performing one. Consider alternatives that may be more efficient, and measure whether they pay off.
Efficient data types. Remember that certain operations, such as looking up whether an element is contained, may take different amounts of time depending on the data structure. In Python, a query like x in xs
takes (mostly) constant time if xs
is a set, but linear time if xs
is a list; these differences become significant as the size of xs
grows.
Efficient modules. In Python, most frequently used modules (or at least parts of) are implemented in C, which is way more efficient than plain Python. Rely on existing modules whenever possible. Or implement your own, after having measured that this may pay off.
These are all things you can already use during programming – and also set up your code such that exchanging, say, one data type by another will still be possible later. This is best achieved by hiding implementation details (such as the used data types) behind an abstract interface used by your clients.
But beyond these points, remember the famous words by Donald J. Knuth:
from bookutils import quiz
quiz('Donald J. Knuth said: "Premature optimization..."',
[
"... is the root of all evil",
"... requires lots of experience",
"... should be left to assembly programmers",
"... is the reason why TeX is so fast",
], 'len("METAFONT") - len("TeX") - len("CWEB")')
This quote should always remind us that after a good design, you should always first measure and then optimize.
Having discussed profilers from a user perspective, let us now dive into how they are actually implemented. It turns out we can use most of our existing infrastructure to implement a simple tracing profiler with only a few lines of code.
The program we will apply our profiler on is – surprise! – our ongoing example, remove_html_markup()
. Our aim is to understand how much time is spent in each line of the code (such that we have a new feature on top of Python cProfile
).
from Intro_Debugging import remove_html_markup
# ignore
from typing import Any, Optional, Type, Dict, Tuple, List
# ignore
from bookutils import print_content
# ignore
import inspect
print_content(inspect.getsource(remove_html_markup), '.py',
start_line_number=238)
238 def remove_html_markup(s): # type: ignore 239 tag = False 240 quote = False 241 out = "" 242 243 for c in s: 244 assert tag or not quote 245 246 if c == '<' and not quote: 247 tag = True 248 elif c == '>' and not quote: 249 tag = False 250 elif (c == '"' or c == "'") and tag: 251 quote = not quote 252 elif not tag: 253 out = out + c 254 255 return out
We introduce a class PerformanceTracer
that tracks, for each line in the code:
hits
), andtime
).To this end, we make use of our Timer
class, which measures time, and the Tracer
class from the chapter on tracing, which allows us to track every line of the program as it is being executed.
from Tracer import Tracer
In PerformanceTracker
, the attributes hits
and time
are mappings indexed by unique locations – that is, pairs of function name and line number.
Location = Tuple[str, int]
class PerformanceTracer(Tracer):
"""Trace time and #hits for individual program lines"""
def __init__(self) -> None:
"""Constructor."""
super().__init__()
self.reset_timer()
self.hits: Dict[Location, int] = {}
self.time: Dict[Location, float] = {}
def reset_timer(self) -> None:
self.timer = Timer.Timer()
As common in this book, we want to use PerformanceTracer
in a with
-block around the function call(s) to be tracked:
with PerformanceTracer() as perf_tracer:
function(...)
When entering the with
block (__enter__()
), we reset all timers. Also, coming from the __enter__()
method of the superclass Tracer
, we enable tracing through the traceit()
method.
from types import FrameType
class PerformanceTracer(PerformanceTracer):
def __enter__(self) -> Any:
"""Enter a `with` block."""
super().__enter__()
self.reset_timer()
return self
The traceit()
method extracts the current location. It increases the corresponding hits
value by 1, and adds the elapsed time to the corresponding time
.
class PerformanceTracer(PerformanceTracer):
def traceit(self, frame: FrameType, event: str, arg: Any) -> None:
"""Tracing function; called for every line."""
t = self.timer.elapsed_time()
location = (frame.f_code.co_name, frame.f_lineno)
self.hits.setdefault(location, 0)
self.time.setdefault(location, 0.0)
self.hits[location] += 1
self.time[location] += t
self.reset_timer()
This is it already. We can now determine where most time is spent in remove_html_markup()
. We invoke it 10,000 times such that we can average over runs:
with PerformanceTracer() as perf_tracer:
for i in range(10000):
s = remove_html_markup('<b>foo</b>')
Here are the hits. For every line executed, we see how often it was executed. The most executed line is the for
loop with 110,000 hits – once for each of the 10 characters in <b>foo</b>
, once for the final check, and all of this 10,000 times.
perf_tracer.hits
{('__init__', 17): 1, ('__init__', 19): 1, ('clock', 8): 1, ('clock', 12): 2, ('__init__', 20): 2, ('remove_html_markup', 238): 10000, ('remove_html_markup', 239): 10000, ('remove_html_markup', 240): 10000, ('remove_html_markup', 241): 10000, ('remove_html_markup', 243): 110000, ('remove_html_markup', 244): 100000, ('remove_html_markup', 246): 100000, ('remove_html_markup', 247): 20000, ('remove_html_markup', 248): 80000, ('remove_html_markup', 250): 60000, ('remove_html_markup', 252): 60000, ('remove_html_markup', 249): 20000, ('remove_html_markup', 253): 30000, ('remove_html_markup', 255): 20000}
The time
attribute collects how much time was spent in each line. Within the loop, again, the for
statement takes the most time. The other lines show some variability, though.
perf_tracer.time
{('__init__', 17): 2.2833000002719928e-05, ('__init__', 19): 1.2500004231696948e-06, ('clock', 8): 1.0000003385357559e-06, ('clock', 12): 1.7500005924375728e-06, ('__init__', 20): 1.8329992599319667e-06, ('remove_html_markup', 238): 0.012149665964898304, ('remove_html_markup', 239): 0.01087509404260345, ('remove_html_markup', 240): 0.010530847047448333, ('remove_html_markup', 241): 0.010054877981019672, ('remove_html_markup', 243): 0.09611047714861343, ('remove_html_markup', 244): 0.0851450361078605, ('remove_html_markup', 246): 0.08416355099780048, ('remove_html_markup', 247): 0.01722242398227536, ('remove_html_markup', 248): 0.06797252500382456, ('remove_html_markup', 250): 0.050401491005686694, ('remove_html_markup', 252): 0.05263906708023569, ('remove_html_markup', 249): 0.017042534977917967, ('remove_html_markup', 253): 0.025152493912173668, ('remove_html_markup', 255): 0.016864611041455646}
For a full profiler, these numbers would now be sorted and printed in a table, much like cProfile
does. However, we will borrow some material from previous chapters and annotate our code accordingly.
In the chapter on statistical debugging, we have encountered the CoverageCollector
class, which collects line and function coverage during execution, using a collect()
method that is invoked for every line. We will repurpose this class to collect arbitrary metrics on the lines executed, notably time taken.
from StatisticalDebugger import CoverageCollector, SpectrumDebugger
The MetricCollector
class is an abstract superclass that provides an interface to access a particular metric.
class MetricCollector(CoverageCollector):
"""Abstract superclass for collecting line-specific metrics"""
def metric(self, event: Any) -> Optional[float]:
"""Return a metric for an event, or none."""
return None
def all_metrics(self, func: str) -> List[float]:
"""Return all metric for a function `func`."""
return []
Given these metrics, we can also compute sums and maxima for a single function.
class MetricCollector(MetricCollector):
def total(self, func: str) -> float:
return sum(self.all_metrics(func))
def maximum(self, func: str) -> float:
return max(self.all_metrics(func))
Let us instantiate this superclass into TimeCollector
– a subclass that measures time. This is modeled after our PerformanceTracer
class, above; notably, the time
attribute serves the same role.
class TimeCollector(MetricCollector):
"""Collect time executed for each line"""
def __init__(self) -> None:
"""Constructor"""
super().__init__()
self.reset_timer()
self.time: Dict[Location, float] = {}
self.add_items_to_ignore([Timer.Timer, Timer.clock])
def collect(self, frame: FrameType, event: str, arg: Any) -> None:
"""Invoked for every line executed. Accumulate time spent."""
t = self.timer.elapsed_time()
super().collect(frame, event, arg)
location = (frame.f_code.co_name, frame.f_lineno)
self.time.setdefault(location, 0.0)
self.time[location] += t
self.reset_timer()
def reset_timer(self) -> None:
self.timer = Timer.Timer()
def __enter__(self) -> Any:
super().__enter__()
self.reset_timer()
return self
The metric()
and all_metrics()
methods accumulate the metric (time taken) for an individual function:
class TimeCollector(TimeCollector):
def metric(self, location: Any) -> Optional[float]:
if location in self.time:
return self.time[location]
else:
return None
def all_metrics(self, func: str) -> List[float]:
return [time
for (func_name, lineno), time in self.time.items()
if func_name == func]
Here's how to use TimeCollector()
– again, in a with
block:
with TimeCollector() as collector:
for i in range(100):
s = remove_html_markup('<b>foo</b>')
The time
attribute holds the time spent in each line:
for location, time_spent in collector.time.items():
print(location, time_spent)
('remove_html_markup', 238) 0.00021604699941235594 ('remove_html_markup', 239) 0.00018649800040293485 ('remove_html_markup', 240) 0.00017612200008443324 ('remove_html_markup', 241) 0.00017058100183930947 ('remove_html_markup', 243) 0.001600210991455242 ('remove_html_markup', 244) 0.0015725439980087685 ('remove_html_markup', 246) 0.0014476009782811161 ('remove_html_markup', 247) 0.0002855489965440938 ('remove_html_markup', 248) 0.0011488629961604602 ('remove_html_markup', 250) 0.0008527550062353839 ('remove_html_markup', 252) 0.0008805919842416188 ('remove_html_markup', 249) 0.0002848419944712077 ('remove_html_markup', 253) 0.0004203369908282184 ('remove_html_markup', 255) 0.0002832869949997985
And we can also create a total for an entire function:
collector.total('remove_html_markup')
0.009525828932964941
Let us now go and visualize these numbers in a simple form. The idea is to assign each line a color whose saturation indicates the time spent in that line relative to the time spent in the function overall – the higher the fraction, the darker the line. We create a MetricDebugger
class built as a specialization of SpectrumDebugger
, in which suspiciousness()
and color()
are repurposed to show these metrics.
class MetricDebugger(SpectrumDebugger):
"""Visualize a metric"""
def metric(self, location: Location) -> float:
sum = 0.0
for outcome in self.collectors:
for collector in self.collectors[outcome]:
assert isinstance(collector, MetricCollector)
m = collector.metric(location)
if m is not None:
sum += m # type: ignore
return sum
def total(self, func_name: str) -> float:
total = 0.0
for outcome in self.collectors:
for collector in self.collectors[outcome]:
assert isinstance(collector, MetricCollector)
total += sum(collector.all_metrics(func_name))
return total
def maximum(self, func_name: str) -> float:
maximum = 0.0
for outcome in self.collectors:
for collector in self.collectors[outcome]:
assert isinstance(collector, MetricCollector)
maximum = max(maximum,
max(collector.all_metrics(func_name)))
return maximum
def suspiciousness(self, location: Location) -> float:
func_name, _ = location
return self.metric(location) / self.total(func_name)
def color(self, location: Location) -> str:
func_name, _ = location
hue = 240 # blue
saturation = 100 # fully saturated
darkness = self.metric(location) / self.maximum(func_name)
lightness = 100 - darkness * 25
return f"hsl({hue}, {saturation}%, {lightness}%)"
def tooltip(self, location: Location) -> str:
return f"{super().tooltip(location)} {self.metric(location)}"
We can now introduce PerformanceDebugger
as a subclass of MetricDebugger
, using an arbitrary MetricCollector
(such as TimeCollector
) to obtain the metric we want to visualize.
class PerformanceDebugger(MetricDebugger):
"""Collect and visualize a metric"""
def __init__(self, collector_class: Type, log: bool = False):
assert issubclass(collector_class, MetricCollector)
super().__init__(collector_class, log=log)
With PerformanceDebugger
, we inherit all the capabilities of SpectrumDebugger
, such as showing the (relative) percentage of time spent in a table. We see that the for
condition and the following assert
take most of the time, followed by the first condition.
with PerformanceDebugger(TimeCollector) as debugger:
for i in range(100):
s = remove_html_markup('<b>foo</b>')
print(debugger)
238 2% def remove_html_markup(s): # type: ignore 239 1% tag = False 240 1% quote = False 241 1% out = "" 242 0% 243 17% for c in s: 244 15% assert tag or not quote 245 0% 246 15% if c == '<' and not quote: 247 3% tag = True 248 12% elif c == '>' and not quote: 249 3% tag = False 250 9% elif (c == '"' or c == "'") and tag: 251 0% quote = not quote 252 9% elif not tag: 253 4% out = out + c 254 0% 255 3% return out
However, we can also visualize these percentages, using shades of blue to indicate those lines most time spent in:
debugger
238 def remove_html_markup(s): # type: ignore
239 tag = False
240 quote = False
241 out = ""
242
243 for c in s:
244 assert tag or not quote
245
246 if c == '<' and not quote:
247 tag = True
248 elif c == '>' and not quote:
249 tag = False
250 elif (c == '"' or c == "'") and tag:
251 quote = not quote
252 elif not tag:
253 out = out + c
254
255 return out
Our framework is flexible enough to collect (and visualize) arbitrary metrics. This HitCollector
class, for instance, collects how often a line is being executed.
class HitCollector(MetricCollector):
"""Collect how often a line is executed"""
def __init__(self) -> None:
super().__init__()
self.hits: Dict[Location, int] = {}
def collect(self, frame: FrameType, event: str, arg: Any) -> None:
super().collect(frame, event, arg)
location = (frame.f_code.co_name, frame.f_lineno)
self.hits.setdefault(location, 0)
self.hits[location] += 1
def metric(self, location: Location) -> Optional[int]:
if location in self.hits:
return self.hits[location]
else:
return None
def all_metrics(self, func: str) -> List[float]:
return [hits
for (func_name, lineno), hits in self.hits.items()
if func_name == func]
We can plug in this class into PerformanceDebugger
to obtain a distribution of lines executed:
with PerformanceDebugger(HitCollector) as debugger:
for i in range(100):
s = remove_html_markup('<b>foo</b>')
In total, during this call to remove_html_markup()
, there are 6,400 lines executed:
debugger.total('remove_html_markup')
6400.0
Again, we can visualize the distribution as a table and using colors. We can see how the shade gets lighter in the lower part of the loop as individual conditions have been met.
print(debugger)
238 1% def remove_html_markup(s): # type: ignore 239 1% tag = False 240 1% quote = False 241 1% out = "" 242 0% 243 17% for c in s: 244 15% assert tag or not quote 245 0% 246 15% if c == '<' and not quote: 247 3% tag = True 248 12% elif c == '>' and not quote: 249 3% tag = False 250 9% elif (c == '"' or c == "'") and tag: 251 0% quote = not quote 252 9% elif not tag: 253 4% out = out + c 254 0% 255 3% return out
debugger
238 def remove_html_markup(s): # type: ignore
239 tag = False
240 quote = False
241 out = ""
242
243 for c in s:
244 assert tag or not quote
245
246 if c == '<' and not quote:
247 tag = True
248 elif c == '>' and not quote:
249 tag = False
250 elif (c == '"' or c == "'") and tag:
251 quote = not quote
252 elif not tag:
253 out = out + c
254
255 return out
Besides identifying causes for performance issues in the code, one may also search for causes in the input, using Delta Debugging. This can be useful if one does not immediately want to embark into investigating the code, but maybe first determine external influences that are related to performance issues.
Here is a variant of remove_html_markup()
that introduces a (rather obvious) performance issue.
import time
def remove_html_markup_ampersand(s: str) -> str:
tag = False
quote = False
out = ""
for c in s:
assert tag or not quote
if c == '&':
time.sleep(0.1) # <-- the obvious performance issue
if c == '<' and not quote:
tag = True
elif c == '>' and not quote:
tag = False
elif (c == '"' or c == "'") and tag:
quote = not quote
elif not tag:
out = out + c
return out
We can easily trigger this issue by measuring time taken:
with Timer.Timer() as t:
remove_html_markup_ampersand('&&&')
t.elapsed_time()
0.3131859590002932
Let us set up a test that checks whether the performance issue is present.
def remove_html_test(s: str) -> None:
with Timer.Timer() as t:
remove_html_markup_ampersand(s)
assert t.elapsed_time() < 0.1
We can now apply delta debugging to determine a minimum input that causes the failure:
s_fail = '<b>foo&</b>'
with DeltaDebugger.DeltaDebugger() as dd:
remove_html_test(s_fail)
dd.min_args()
{'s': '&'}
For performance issues, however, a minimal input is often not enough to highlight the failure cause. This is because short inputs tend to take less processing time than longer inputs, which increases the risks of a spurious diagnosis. A better alternative is to compute a maximum input where the issue does not occur:
s_pass = dd.max_args()
s_pass
{'s': '<b>fooamp;</b>'}
We see that the culprit character (the &
) is removed. This tells us the failure-inducing difference – or, more precisely, the cause for the performance issue.
This chapter provides a class PerformanceDebugger
that allows measuring and visualizing the time taken per line in a function.
with PerformanceDebugger(TimeCollector) as debugger:
for i in range(100):
s = remove_html_markup('<b>foo</b>')
The distribution of executed time within each function can be obtained by printing out the debugger:
print(debugger)
238 2% def remove_html_markup(s): # type: ignore 239 2% tag = False 240 1% quote = False 241 1% out = "" 242 0% 243 17% for c in s: 244 15% assert tag or not quote 245 0% 246 14% if c == '<' and not quote: 247 2% tag = True 248 11% elif c == '>' and not quote: 249 3% tag = False 250 8% elif (c == '"' or c == "'") and tag: 251 0% quote = not quote 252 9% elif not tag: 253 5% out = out + c 254 0% 255 2% return out
The sum of all percentages in a function should always be 100%.
These percentages can also be visualized, where darker shades represent higher percentage values:
debugger
238 def remove_html_markup(s): # type: ignore
239 tag = False
240 quote = False
241 out = ""
242
243 for c in s:
244 assert tag or not quote
245
246 if c == '<' and not quote:
247 tag = True
248 elif c == '>' and not quote:
249 tag = False
250 elif (c == '"' or c == "'") and tag:
251 quote = not quote
252 elif not tag:
253 out = out + c
254
255 return out
The abstract MetricCollector
class allows subclassing to build more collectors, such as HitCollector
.
# ignore
from ClassDiagram import display_class_hierarchy
# ignore
display_class_hierarchy([PerformanceDebugger, TimeCollector, HitCollector],
public_methods=[
PerformanceDebugger.__init__,
],
project='debuggingbook')
This chapter concludes the part on abstracting failures. The next part will focus on
Scalene is a high-performance, high-precision CPU, GPU, and memory profiler for Python. In contrast to the standard Python cProfile
profiler, it uses sampling instead of instrumentation or relying on Python's tracing facilities; and it also supports line-by-line profiling. Scalene might be the tool of choice if you want to go beyond basic profiling.
The Wikipedia articles on profiling and performance analysis tools provide several additional resources on profiling tools and how to apply them in practice.
The Python tracemalloc
module allows tracking memory usage during execution. Between tracemalloc.start()
and tracemalloc.end()
, use tracemalloc.get_traced_memory()
to obtain how much memory is currently being consumed:
import tracemalloc
tracemalloc.start()
current_size, peak_size = tracemalloc.get_traced_memory()
current_size
20256
tracemalloc.stop()
Create a subclass of MetricCollector
named MemoryCollector
. Make it measure the memory consumption before and after each line executed (0 if negative), and visualize the impact of individual lines on memory. Create an appropriate test program that (temporarily) consumes larger amounts of memory.
In a similar way as we integrated a binary "performance test" with delta debugging, we can also integrate such a test with other techniques. Combining a performance test with Statistical Debugging, for instance, will highlight those lines whose execution correlates with low performance. But then, the performance test need not be binary, as with functional pass/fail tests – you can also weight individual lines by how much they impact performance. Create a variant of StatisticalDebugger
that reflects the impact of individual lines on an arbitrary (summarized) performance metric.