fix: giltracer in jupyter

This commit is contained in:
Maarten A. Breddels 2020-12-17 19:22:01 +01:00
parent 67c25d37e5
commit 78ffee2936
3 changed files with 10 additions and 8 deletions

View File

@ -217,9 +217,9 @@ First, load the magics
%load_ext per4m.cellmagic %load_ext per4m.cellmagic
``` ```
Run a cell with the `%%giltrace` cell magic. Run a cell with the `%%giltracer` cell magic.
``` ```
%%giltrace %%giltracer
import threading import threading
import time import time
import time import time
@ -254,8 +254,8 @@ Saving report to /home/maartenbreddels/github/maartenbreddels/per4m/result.html
Dumping trace data to json, total entries: 167, estimated json file size: 19.6KiB Dumping trace data to json, total entries: 167, estimated json file size: 19.6KiB
Generating HTML report Generating HTML report
Report saved. Report saved.
Download result.html Download viztracer.html
Open result.html in new tab (might not work due to security issue) Open viztracer.html in new tab (might not work due to security issue)
``` ```
Click the download link to get the results. Click the download link to get the results.

View File

@ -11,22 +11,23 @@ from IPython.core.magic import (cell_magic,
) )
from .giltracer import GilTracer from .giltracer import PerfRecordGIL
@magics_class @magics_class
class GilTraceMagic(Magics): class GilTraceMagic(Magics):
@needs_local_scope @needs_local_scope
@cell_magic @cell_magic
def giltrace(self, line, cell, local_ns): def giltracer(self, line, cell, local_ns):
temp_dir = tempfile.mkdtemp() temp_dir = tempfile.mkdtemp()
perf_path = os.path.join(temp_dir, 'perf.data') perf_path = os.path.join(temp_dir, 'perf.data')
viz_path = os.path.join(temp_dir, 'viztracer.json') viz_path = os.path.join(temp_dir, 'viztracer.json')
gil_path = os.path.join(temp_dir, 'giltracer.json') gil_path = os.path.join(temp_dir, 'giltracer.json')
out_path = 'result.html' out_path = 'giltracer.html'
code = self.shell.transform_cell(cell) code = self.shell.transform_cell(cell)
with GilTracer(perf_path, gil_path) as gt: with PerfRecordGIL(perf_path, gil_path) as gt:
with viztracer.VizTracer(output_file=viz_path): with viztracer.VizTracer(output_file=viz_path):
exec(code, local_ns, local_ns) exec(code, local_ns, local_ns)
gt.post_process()
builder = ReportBuilder([viz_path, gil_path]) builder = ReportBuilder([viz_path, gil_path])
builder.save(output_file=out_path) builder.save(output_file=out_path)

View File

@ -34,6 +34,7 @@ class PerfRecord:
def __enter__(self): def __enter__(self):
self.start() self.start()
return self
def start(self): def start(self):
pid = os.getpid() pid = os.getpid()