forked from asottile/importtime-waterfall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importtime_waterfall.py
175 lines (143 loc) · 4.98 KB
/
importtime_waterfall.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import annotations
import argparse
import datetime
import json
import subprocess
import sys
import time
from typing import Any
from typing import NamedTuple
from typing import Sequence
IMPORT_TIME = 'import time:'
class Import(NamedTuple):
name: str
self_time: int
children: Any # List['Import'] # python/mypy#731
def graph(root: Import) -> int:
def pp(x: Import, depth: int = 0) -> None:
print(f'{" " * depth}{x.name} ({x.self_time})')
for imp in x.children:
pp(imp, depth=depth + 1)
for imp in root.children:
pp(imp)
return 0
def har(root: Import) -> int:
dt = datetime.datetime(1991, 7, 5)
har_out = {
'log': {
'version': '1.2',
'creator': {'name': 'python', 'version': sys.version},
'browser': {'name': 'python', 'version': sys.version},
'pages': [{
'startedDateTime': dt.isoformat(timespec='milliseconds') + 'Z',
'id': 'page0',
'title': f'`import {root.children[-1].name}` {sys.version}',
'pageTimings': {'onContentLoad': -1, 'onLoad': -1},
}],
'entries': [],
},
}
def visit(x: Import) -> None:
nonlocal dt
start = dt
dt += datetime.timedelta(milliseconds=1)
for imp in x.children:
visit(imp)
dt += datetime.timedelta(milliseconds=x.self_time - 1)
diff = dt - start
time = diff.seconds * 1000 + diff.microseconds // 1000
assert isinstance(har_out['log']['entries'], list) # trust me mypy
har_out['log']['entries'].append({
'page_ref': 'page0',
'startedDateTime': start.isoformat(timespec='milliseconds') + 'Z',
'time': time,
'request': {
'method': 'GET',
'url': x.name,
'httpVersion': 'HTTP/0.0',
'cookies': [],
'headers': [],
'queryString': [],
'headersSize': 0,
'bodySize': 0,
},
'response': {
'status': 200,
'statusText': 'OK',
'httpVersion': 'HTTP/0.0',
'cookies': [],
'headers': [],
'content': {'size': 0, 'mimeType': 'text/x-python'},
'redirectURL': '',
'headersSize': 0,
'bodySize': 0,
},
'cache': {},
'timings': {
'blocked': 0,
'dns': 0,
'connect': 0,
'ssl': 0,
'send': 0,
'wait': time - x.self_time,
'receive': x.self_time,
},
'serverIpAddress': '0.0.0.0',
})
for imp in root.children:
visit(imp)
print(json.dumps(har_out, separators=(',', ':')))
return 0
def best_of(mod: str, *, n: int = 5) -> subprocess.CompletedProcess[str]:
def run() -> tuple[float, subprocess.CompletedProcess[str]]:
before = time.time()
ret = subprocess.run(
(sys.executable, '-Ximporttime', '-c', f'import {mod}'),
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
encoding='UTF-8',
)
return time.time() - before, ret
best_time, best = run()
for _ in range(n):
candidate_time, candidate = run()
if candidate_time < best_time:
best_time, best = candidate_time, candidate
return best
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('module')
parser.add_argument('--include-interpreter-startup', action='store_true')
mut = parser.add_mutually_exclusive_group()
mut.add_argument('--graph', dest='fmt', action='store_const', const=None)
mut.add_argument('--har', dest='fmt', action='store_const', const='har')
args = parser.parse_args()
res = best_of(args.module)
root = Import(name='root', self_time=0, children=[])
import_stack = [root]
for line in reversed(res.stderr.splitlines()):
if line.startswith(IMPORT_TIME):
line = line[len(IMPORT_TIME):]
self_time_s, _, name_s = line.split('|')
try:
self_time = int(self_time_s)
except ValueError: # headers
continue
name = name_s.lstrip()
imp = Import(name=name, self_time=self_time, children=[])
depth = (len(name_s) - len(name) - 1) / 2 + 1
while len(import_stack) > depth:
import_stack.pop()
import_stack[-1].children.insert(0, imp)
import_stack.append(imp)
root.children[:] = [
imp for imp in root.children
if imp.name == args.module or args.include_interpreter_startup
]
if args.fmt == 'har':
return har(root)
else:
return graph(root)
if __name__ == '__main__':
exit(main())