Repository navigation
Expand file tree
/
Copy pathprofiler.py
More file actions
60 lines (43 loc) · 1.4 KB
/
Copy pathprofiler.py
File metadata and controls
60 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import vmprof
import tempfile
from vmprof.stats import Stats
from vmprof.reader import read_prof
class VMProfError(Exception):
pass
class ProfilerContext(object):
done = False
def __init__(self, name, memory):
if name is None:
self.tmpfile = tempfile.NamedTemporaryFile(delete=False)
else:
self.tmpfile = open(name, "wb")
self.memory = memory
def __enter__(self):
vmprof.enable(self.tmpfile.fileno(), 0.001, self.memory)
def __exit__(self, type, value, traceback):
vmprof.disable()
self.tmpfile.close()
self.done = True
def read_profile(prof_file):
if not hasattr(prof_file, 'read'):
prof_file = open(str(prof_file), 'rb')
period, profiles, virtual_symbols, interp_name = read_prof(prof_file)
jit_frames = {}
d = dict(virtual_symbols)
s = Stats(profiles, d, jit_frames, interp_name)
return s
class Profiler(object):
ctx = None
def __init__(self):
self._lib_cache = {}
def measure(self, name=None, memory=False):
self.ctx = ProfilerContext(name, memory)
return self.ctx
def get_stats(self):
if not self.ctx:
raise VMProfError("no profiling done")
if not self.ctx.done:
raise VMProfError("profiling in process")
res = read_profile(self.ctx.tmpfile.name)
self.ctx = None
return res