forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_checker.py
151 lines (119 loc) · 3.99 KB
/
touch_checker.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
#!/usr/bin/env python3
from typing import Callable, List, Tuple, Optional
import sys
import glob
import os
import shutil
import statistics
import subprocess
import textwrap
import time
def print_offset(text: str, indent_length: int = 4) -> None:
print()
print(textwrap.indent(text, ' ' * indent_length))
print()
def delete_folder(folder_path: str) -> None:
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
def execute(command: List[str]) -> None:
proc = subprocess.Popen(
' '.join(command),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True)
stdout_bytes, stderr_bytes = proc.communicate() # type: Tuple[bytes, bytes]
stdout, stderr = stdout_bytes.decode('utf-8'), stderr_bytes.decode('utf-8')
if proc.returncode != 0:
print('EXECUTED COMMAND:', repr(command))
print('RETURN CODE:', proc.returncode)
print()
print('STDOUT:')
print_offset(stdout)
print('STDERR:')
print_offset(stderr)
print()
Command = Callable[[], None]
def test(setup: Command, command: Command, teardown: Command) -> float:
setup()
start = time.time()
command()
end = time.time() - start
teardown()
return end
def make_touch_wrappers(filename: str) -> Tuple[Command, Command]:
def setup() -> None:
execute(["touch", filename])
def teardown() -> None:
pass
return setup, teardown
def make_change_wrappers(filename: str) -> Tuple[Command, Command]:
copy = None # type: Optional[str]
def setup() -> None:
nonlocal copy
with open(filename, 'r') as stream:
copy = stream.read()
with open(filename, 'a') as stream:
stream.write('\n\nfoo = 3')
def teardown() -> None:
assert copy is not None
with open(filename, 'w') as stream:
stream.write(copy)
# Re-run to reset cache
execute(["python3", "-m", "mypy", "-i", "mypy"]),
return setup, teardown
def main() -> None:
if len(sys.argv) != 2 or sys.argv[1] not in {'touch', 'change'}:
print("First argument should be 'touch' or 'change'")
return
if sys.argv[1] == 'touch':
make_wrappers = make_touch_wrappers
verb = "Touching"
elif sys.argv[1] == 'change':
make_wrappers = make_change_wrappers
verb = "Changing"
else:
raise AssertionError()
print("Setting up...")
baseline = test(
lambda: None,
lambda: execute(["python3", "-m", "mypy", "mypy"]),
lambda: None)
print("Baseline: {}".format(baseline))
cold = test(
lambda: delete_folder(".mypy_cache"),
lambda: execute(["python3", "-m", "mypy", "-i", "mypy"]),
lambda: None)
print("Cold cache: {}".format(cold))
warm = test(
lambda: None,
lambda: execute(["python3", "-m", "mypy", "-i", "mypy"]),
lambda: None)
print("Warm cache: {}".format(warm))
print()
deltas = []
for filename in glob.iglob("mypy/**/*.py", recursive=True):
print("{} {}".format(verb, filename))
setup, teardown = make_wrappers(filename)
delta = test(
setup,
lambda: execute(["python3", "-m", "mypy", "-i", "mypy"]),
teardown)
print(" Time: {}".format(delta))
deltas.append(delta)
print()
print("Initial:")
print(" Baseline: {}".format(baseline))
print(" Cold cache: {}".format(cold))
print(" Warm cache: {}".format(warm))
print()
print("Aggregate:")
print(" Times: {}".format(deltas))
print(" Mean: {}".format(statistics.mean(deltas)))
print(" Median: {}".format(statistics.median(deltas)))
print(" Stdev: {}".format(statistics.stdev(deltas)))
print(" Min: {}".format(min(deltas)))
print(" Max: {}".format(max(deltas)))
print(" Total: {}".format(sum(deltas)))
print()
if __name__ == '__main__':
main()