1- from IPython .core .magic import Magics , magics_class , line_magic
1+ from IPython .core .magic import Magics , line_magic , magics_class
2+
3+ from .cell_history import CellHistory
24from .performance_monitor import PerformanceMonitor
35from .performance_visualizer import PerformanceVisualizer
4- from .cell_history import CellHistory
56
67_perfmonitor_magics = None
78
9+
810@magics_class
911class perfmonitorMagics (Magics ):
1012 def __init__ (self , shell ):
@@ -16,7 +18,7 @@ def __init__(self, shell):
1618
1719 def pre_run_cell (self , info ):
1820 self .cell_history .start_cell (info .raw_cell )
19-
21+
2022 def post_run_cell (self , result ):
2123 self .cell_history .end_cell (result .result )
2224 if self .monitor and self .print_perfreports :
@@ -55,19 +57,20 @@ def perfmonitor_start(self, line):
5557 if self .monitor and self .monitor .running :
5658 print ("Performance monitoring already running" )
5759 return
58-
60+
5961 interval = 1.0
6062 if line :
6163 try :
6264 interval = float (line )
6365 except ValueError :
6466 print (f"Invalid interval value: { line } " )
6567 return
66-
68+
6769 self .monitor = PerformanceMonitor (interval = interval )
6870 self .monitor .start ()
6971 self .visualizer = PerformanceVisualizer (
70- self .monitor .cpu_handles , self .monitor .memory , self .monitor .gpu_memory )
72+ self .monitor .cpu_handles , self .monitor .memory , self .monitor .gpu_memory
73+ )
7174
7275 @line_magic
7376 def perfmonitor_stop (self , line ):
@@ -81,16 +84,16 @@ def perfmonitor_plot(self, line):
8184 if not self .monitor :
8285 print ("No active performance monitoring session" )
8386 return
84-
87+
8588 cell_marks = self ._parse_cell_number (line )
8689 if cell_marks is False : # Error parsing
8790 return
88-
91+
8992 df = self .monitor .data .to_dataframe ()
9093 if cell_marks :
9194 start_mark , end_mark = cell_marks
92- df = df [(df [' time' ] >= start_mark ) & (df [' time' ] <= end_mark )]
93- df [' time' ] -= self .monitor .start_time
95+ df = df [(df [" time" ] >= start_mark ) & (df [" time" ] <= end_mark )]
96+ df [" time" ] -= self .monitor .start_time
9497
9598 if df .empty :
9699 print ("No performance data available" )
@@ -110,13 +113,13 @@ def _perfreport(self, cell_marks=None):
110113
111114 if not cell_marks :
112115 cell_marks = self .cell_history .cell_timestamps [- 1 ]
113-
116+
114117 start_mark , end_mark = cell_marks
115118 duration = end_mark - start_mark
116119 print (f"Duration: { duration :.2f} s" )
117120
118121 df = self .monitor .data .to_dataframe ()
119- df = df [(df [' time' ] >= start_mark ) & (df [' time' ] <= end_mark )]
122+ df = df [(df [" time" ] >= start_mark ) & (df [" time" ] <= end_mark )]
120123 if df .empty :
121124 print ("No performance data available" )
122125 return
@@ -126,20 +129,23 @@ def _perfreport(self, cell_marks=None):
126129 ("CPU Util (Across CPUs)" , "cpu_util_avg" , "-" ),
127130 ("Memory (GB)" , "memory_usage_gb" , f"{ self .monitor .memory :.2f} " ),
128131 ("GPU Util (Across GPUs)" , "gpu_util_avg" , "-" ),
129- ("GPU Memory (GB)" , "gpu_mem_avg" , f"{ self .monitor .gpu_memory :.2f} " )
132+ ("GPU Memory (GB)" , "gpu_mem_avg" , f"{ self .monitor .gpu_memory :.2f} " ),
130133 ]
131-
134+
132135 print (f"{ 'Metric' :<25} { 'AVG' :<8} { 'MIN' :<8} { 'MAX' :<8} { 'TOTAL' :<8} " )
133136 print ("-" * 65 )
134137 for name , col , total in metrics :
135138 if col in df .columns :
136- print (f"{ name :<25} { df [col ].mean ():<8.2f} { df [col ].min ():<8.2f} { df [col ].max ():<8.2f} { total :<8} " )
137-
139+ print (
140+ f"{ name :<25} { df [col ].mean ():<8.2f} "
141+ f"{ df [col ].min ():<8.2f} { df [col ].max ():<8.2f} { total :<8} "
142+ )
143+
138144 @line_magic
139145 def perfmonitor_enable_perfreports (self , line ):
140146 self .print_perfreports = True
141147 print ("Performance reports enabled for each cell" )
142-
148+
143149 @line_magic
144150 def perfmonitor_disable_perfreports (self , line ):
145151 self .print_perfreports = False
@@ -157,14 +163,14 @@ def perfmonitor_export_perfdata(self, line):
157163 if not self .monitor :
158164 print ("No active performance monitoring session" )
159165 return
160- filename = line .strip () or ' performance_data.csv'
166+ filename = line .strip () or " performance_data.csv"
161167 self .monitor .data .export (filename )
162168 print (f"Performance data exported to { filename } " )
163169
164170 @line_magic
165171 def perfmonitor_export_cell_history (self , line ):
166172 """Export cell history"""
167- filename = line .strip () or ' cell_history.json'
173+ filename = line .strip () or " cell_history.json"
168174 self .cell_history .export (filename )
169175 print (f"Cell history exported to { filename } " )
170176
@@ -174,33 +180,35 @@ def perfmonitor_help(self, line):
174180 commands = [
175181 "perfmonitor_help -- show this help" ,
176182 "perfmonitor_resources -- show available hardware" ,
177- "cell_history -- show cell execution history" ,
183+ "cell_history -- show cell execution history" ,
178184 "perfmonitor_start [seconds] -- start monitoring" ,
179185 "perfmonitor_stop -- stop monitoring" ,
180186 "perfmonitor_perfreport [cell] -- show performance report" ,
181187 "perfmonitor_plot [cell] -- plot performance data" ,
182188 "perfmonitor_enable_perfreports -- enable auto-reports" ,
183189 "perfmonitor_disable_perfreports -- disable auto-reports" ,
184190 "perfmonitor_export_perfdata [filename] -- export data to CSV" ,
185- "perfmonitor_export_cell_history [filename] -- export history to JSON"
191+ "perfmonitor_export_cell_history [filename] -- export history to JSON" ,
186192 ]
187193 print ("Available commands:" )
188194 for cmd in commands :
189195 print (f" %{ cmd } " )
190196
197+
191198def load_ipython_extension (ipython ):
192199 global _perfmonitor_magics
193200 _perfmonitor_magics = perfmonitorMagics (ipython )
194- ipython .events .register (' pre_run_cell' , _perfmonitor_magics .pre_run_cell )
195- ipython .events .register (' post_run_cell' , _perfmonitor_magics .post_run_cell )
201+ ipython .events .register (" pre_run_cell" , _perfmonitor_magics .pre_run_cell )
202+ ipython .events .register (" post_run_cell" , _perfmonitor_magics .post_run_cell )
196203 ipython .register_magics (_perfmonitor_magics )
197204 print ("Perfmonitor extension loaded." )
198205
206+
199207def unload_ipython_extension (ipython ):
200208 global _perfmonitor_magics
201209 if _perfmonitor_magics :
202- ipython .events .unregister (' pre_run_cell' , _perfmonitor_magics .pre_run_cell )
203- ipython .events .unregister (' post_run_cell' , _perfmonitor_magics .post_run_cell )
210+ ipython .events .unregister (" pre_run_cell" , _perfmonitor_magics .pre_run_cell )
211+ ipython .events .unregister (" post_run_cell" , _perfmonitor_magics .post_run_cell )
204212 if _perfmonitor_magics .monitor :
205213 _perfmonitor_magics .monitor .stop ()
206214 _perfmonitor_magics = None
0 commit comments