Skip to content

Commit a19149c

Browse files
committed
tweaks for py3.4 compatibility
1 parent 443ee34 commit a19149c

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ Tested on
156156
* IPython 2.1 with Python 2.7 on Linux 64bit
157157
* IPython 2.1 with Python 2.7 on Windows 64bit (no `perf` support)
158158
* IPython 2.1 with Python 2.7 on OS X 10.10 Yosemite (no `perf` support)
159+
* IPython 2.2 with Python 3.4 on LInux 64bit
160+
161+
TO FIX
162+
======
163+
164+
* merge perf variation into the main variation as some sort of plugin (so it doesn't interfere if per not installed or available)
165+
* possibly try to add a counter for the size of the garbage collector, to see how many temp objects are made (disable gc first) on each command?
159166

160167
Problems
161168
========

ipython_memory_usage_perf.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import time
1010
import memory_profiler
11+
from IPython import get_ipython
1112
import perf_process
1213

1314
# keep a global accounting for the last known memory usage
@@ -18,12 +19,39 @@
1819
peak_memory_usage = -1
1920
perf_proc = None
2021

22+
watching_memory = True
23+
input_cells = get_ipython().user_ns['In']
24+
25+
26+
def start_watching_memory():
27+
"""Register memory profiling tools to IPython instance."""
28+
global watching_memory
29+
watching_memory = True
30+
ip = get_ipython()
31+
ip.events.register("post_run_cell", watch_memory)
32+
ip.events.register("pre_run_cell", pre_run_cell)
33+
34+
35+
def stop_watching_memory():
36+
"""Unregister memory profiling tools from IPython instance."""
37+
global watching_memory
38+
watching_memory = False
39+
ip = get_ipython()
40+
try:
41+
ip.events.unregister("post_run_cell", watch_memory)
42+
except ValueError:
43+
pass
44+
try:
45+
ip.events.unregister("pre_run_cell", pre_run_cell)
46+
except ValueError:
47+
pass
2148

2249
def watch_memory():
2350
import time
2451
# bring in the global memory usage value from the previous iteration
25-
global previous_call_memory_usage, peak_memory_usage, keep_watching, perf_proc
26-
nbr_commands = len(In)
52+
global previous_call_memory_usage, peak_memory_usage, keep_watching, perf_proc, \
53+
watching_memory, input_cells
54+
#nbr_commands = len(In)
2755
new_memory_usage = memory_profiler.memory_usage()[0]
2856
memory_delta = new_memory_usage - previous_call_memory_usage
2957
keep_watching = False
@@ -42,7 +70,8 @@ def watch_memory():
4270
time.sleep(MIN_TIME_TO_GET_PERF_SAMPLE) # pause until at least 0.1s has passed
4371
# if we have a valid perf running then capture that information
4472
perf_values = perf_process.finish_perf(perf_proc)
45-
cmd = In[nbr_commands-1]
73+
74+
cmd = "" #In[nbr_commands-1]
4675
# convert the results into a pretty string
4776
#output_template = "'{cmd}' used {memory_delta:0.4f} MiB RAM in {time_delta:0.2f}s, peaked {peaked_memory_usage:0.2f} MiB above current, total RAM usage {memory_usage:0.2f} MiB"
4877
output_template = "Used {memory_delta:0.4f} MiB RAM in {time_delta:0.2f}s, peaked {peaked_memory_usage:0.2f} MiB above current, total RAM usage {memory_usage:0.2f} MiB"
@@ -100,16 +129,4 @@ def pre_run_cell():
100129
ipython_memory_usage_thread.start()
101130

102131
pid = os.getpid()
103-
perf_proc = perf_process.run_capture_perf(pid)
104-
105-
106-
107-
if __name__ == "__main__":
108-
if 'In' not in dir():
109-
script_name = os.path.split(__file__)[1]
110-
raise ValueError("You must run this from IPython interactively using e.g. '%run -i {}'".format(script_name))
111-
112-
ip = get_ipython()
113-
# http://ipython.org/ipython-doc/dev/api/generated/IPython.core.events.html
114-
ip.events.register("post_run_cell", watch_memory)
115-
ip.events.register("pre_run_cell", pre_run_cell)
132+
perf_proc = perf_process.run_capture_perf(pid)

perf_process.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import unicode_literals # force unadorned strings "" to be unicode without prepending u""
66
import subprocess
77
import unittest
8+
import os
89

910
FIXTURE0 = """ 0.100167119 3,183 cache-misses """
1011
ANSWER0 = 3183
@@ -93,13 +94,14 @@ def finish_perf(proc):
9394
# 0.200387519 4,232 cache-misses
9495
# 0.300540762 5,277 cache-misses
9596
# 0.400778748 3,916 cache-misses
97+
stderrdata = stderrdata.decode('ascii') # assume ascii
9698
values = process_lines(stderrdata)
9799
return values
98100

99101

100102
if __name__ == "__main__":
101103
# simple test for a hardcoded pid gathered over 0.5 seconds
102-
pid = 4583
104+
pid = os.getpid()
103105
print("Using pid:", pid)
104106
proc = run_capture_perf(pid)
105107
import time

0 commit comments

Comments
 (0)