-
Notifications
You must be signed in to change notification settings - Fork 160
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 #257 from vidartf/fixes
Various fixes and minor adjustments
- Loading branch information
Showing
9 changed files
with
141 additions
and
41 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
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
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
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,107 @@ | ||
|
||
"""Tools for profiling diff/merge performance. | ||
Sometimes diffing times can become unacceptably long. | ||
Then the tools in this module can help you profile and figure out | ||
where the time is spent. For a more rigorous profiling, consider using | ||
cPython, but for initial considerations these should be helpful. | ||
Typical profiling usage: | ||
Add some statements like | ||
from nbdime.profiling import timer | ||
with timer.time('Key to identify this segment'): | ||
<code to time> | ||
Then, launch `python -m nbdime.profiling notebookA.ipynb notebookB.ipynb`. | ||
Example case: | ||
Output diffing is slow. By cPython/timer, the time spent has been narrowed | ||
to comparison of MIME types (`_compare_mimedata()`). To figure out which | ||
MIME type is taking all the time, the following is added to that function: | ||
with timer.time(mimetype): | ||
<existing function body> | ||
The output becomes: | ||
Key Calls Time | ||
---------- ------- ------------ | ||
text/html 6 74.9686 | ||
text/plain 12 0.000500202 | ||
image/png 5 0 | ||
From this we can see that the HTML comparison is really blowing up! | ||
Further inspection reveals that there are two html outputs in each | ||
notebook that contain an embedded base64 image. These caused the text | ||
diff to blow up. | ||
""" | ||
|
||
import time | ||
import contextlib | ||
from tabulate import tabulate | ||
|
||
|
||
def _sort_time(value): | ||
time = value[1]['time'] | ||
return -time | ||
|
||
|
||
class TimePaths(object): | ||
def __init__(self, verbose=False, enabled=True): | ||
self.verbose = verbose | ||
self.map = {} | ||
self.enabled = enabled | ||
|
||
@contextlib.contextmanager | ||
def time(self, key): | ||
start = time.time() | ||
yield | ||
end = time.time() | ||
secs = end - start | ||
if not self.enabled: | ||
return | ||
if key in self.map: | ||
self.map[key]['time'] += secs | ||
self.map[key]['calls'] += 1 | ||
else: | ||
self.map[key] = dict(time=secs, calls=1) | ||
|
||
@contextlib.contextmanager | ||
def enable(self): | ||
old = self.enabled | ||
self.enabled = True | ||
yield | ||
self.enabled = old | ||
|
||
@contextlib.contextmanager | ||
def disable(self): | ||
old = self.enabled | ||
self.enabled = False | ||
yield | ||
self.enabled = old | ||
|
||
def __str__(self): | ||
# First, sort by path | ||
items = sorted(self.map.items(), key=_sort_time) | ||
lines = [] | ||
for key, data in items: | ||
time = data['time'] | ||
calls = data['calls'] | ||
lines.append((key, calls, time)) | ||
return tabulate(lines, headers=['Key', 'Calls', 'Time']) | ||
|
||
|
||
timer = TimePaths(enabled=False) | ||
|
||
|
||
def profile_diff_paths(args=None): | ||
import nbdime.nbdiffapp | ||
import nbdime.profiling | ||
nbdime.nbdiffapp.main(args) | ||
data = str(nbdime.profiling.timer) | ||
print(data) | ||
|
||
|
||
if __name__ == "__main__": | ||
profile_diff_paths() |
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