-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
50 lines (39 loc) · 1.33 KB
/
runner.py
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
import attr
import sys
import onyx.utils as u
from onyx.vm.interpreter import Interpreter
@attr.s
class Profiler:
tally = attr.ib(factory=dict)
invokes = attr.ib(factory=dict)
bill = object()
def save_method(self, vm, k):
marks = vm.marks
marks[self.bill] = marks.get(self.bill, []) + [k]
def get_active_methods(self, vm):
v = []
if self.bill in vm.marks:
v.append(vm.marks[self.bill])
v += vm.stack.find_marks(self.bill, None)
return v
def on_method_invoke(self, event):
klass = event.klass
name = event.method.name
k = (klass.name, event.method.name)
self.invokes[k] = self.invokes.get(k, 0) + 1
self.save_method(event.vm, k)
def on_step(self, event):
frames = self.get_active_methods(event.vm)
for ks in frames:
for k in ks:
self.tally[k] = self.tally.get(k, 0) + 1
vm = Interpreter()
prof = Profiler()
vm.listen_for(u.MethodInvoke, prof.on_method_invoke)
vm.listen_for(u.Step, prof.on_step)
vm.run_script(sys.argv[1])
c = reversed(sorted([(v,k) for k,v in prof.tally.items()]))
for count, name in c:
if name[0] != 'BlockClosure':
r = float(count) / prof.invokes[name]
print('{0:>10} {1:>10} {2:>10.2f} {3}'.format(count, prof.invokes[name], r, name))