Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tools/perfstat.py
2723 views
1
#!/usr/bin/python3
2
# This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
3
4
# Given a profile dump, this tool displays top functions based on the stacks listed in the profile
5
6
import argparse
7
8
class Node:
9
def __init__(self):
10
self.function = ""
11
self.source = ""
12
self.line = 0
13
self.hier_ticks = 0
14
self.self_ticks = 0
15
16
def title(self):
17
if self.line > 0:
18
return "{} ({}:{})".format(self.function, self.source, self.line)
19
else:
20
return self.function
21
22
argumentParser = argparse.ArgumentParser(description='Display summary statistics from Luau sampling profiler dumps')
23
argumentParser.add_argument('source_file', type=open)
24
argumentParser.add_argument('--limit', dest='limit', type=int, default=10, help='Display top N functions')
25
26
arguments = argumentParser.parse_args()
27
28
dump = arguments.source_file.readlines()
29
30
stats = {}
31
total = 0
32
total_gc = 0
33
34
for l in dump:
35
ticks, stack = l.strip().split(" ", 1)
36
hier = {}
37
38
for f in reversed(stack.split(";")):
39
source, function, line = f.split(",")
40
node = stats.setdefault(f, Node())
41
42
node.function = function
43
node.source = source
44
node.line = int(line) if len(line) > 0 else 0
45
46
if not node in hier:
47
node.hier_ticks += int(ticks)
48
hier[node] = True
49
50
total += int(ticks)
51
node.self_ticks += int(ticks)
52
53
if node.source == "GC":
54
total_gc += int(ticks)
55
56
if total > 0:
57
print(f"Runtime: {total:,} usec ({100.0 * total_gc / total:.2f}% GC)")
58
print()
59
print("Top functions (self time):")
60
for n in sorted(stats.values(), key=lambda node: node.self_ticks, reverse=True)[:arguments.limit]:
61
print(f"{n.self_ticks:12,} usec ({100.0 * n.self_ticks / total:.2f}%): {n.title()}")
62
print()
63
print("Top functions (total time):")
64
for n in sorted(stats.values(), key=lambda node: node.hier_ticks, reverse=True)[:arguments.limit]:
65
print(f"{n.hier_ticks:12,} usec ({100.0 * n.hier_ticks / total:.2f}%): {n.title()}")
66
67