Skip to content

Commit 1ab18e4

Browse files
benjefferymergify-bot
authored andcommitted
Add tracemalloc to stress_lowlevel.py
1 parent ecb8654 commit 1ab18e4

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

python/stress_lowlevel.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
any memory leaks or error handling issues.
44
"""
55
import argparse
6+
import curses
67
import logging
78
import os
89
import random
910
import resource
10-
import sys
1111
import time
12+
import tracemalloc
1213
import unittest
1314

1415
import tests.test_dict_encoding as test_dict_encoding
@@ -25,7 +26,7 @@
2526
import tests.test_vcf as test_vcf
2627

2728

28-
def main():
29+
def main(stdscr):
2930
modules = {
3031
"highlevel": test_highlevel,
3132
"lowlevel": test_lowlevel,
@@ -57,13 +58,14 @@ def main():
5758
# Need to do this to silence the errors from the file_format tests.
5859
logging.basicConfig(level=logging.ERROR)
5960

60-
print("iter\ttests\terr\tfail\tskip\tRSS\tmin\tmax\tmax@iter")
6161
max_rss = 0
6262
max_rss_iter = 0
6363
min_rss = 1e100
6464
iteration = 0
6565
last_print = time.time()
6666
devnull = open(os.devnull, "w")
67+
tracemalloc.start()
68+
memory_start = None
6769
while True:
6870
# We don't want any random variation in the amount of memory
6971
# used from test-to-test.
@@ -73,7 +75,10 @@ def main():
7375
for mod in test_modules[1:]:
7476
suite.addTests(testloader.loadTestsFromModule(mod))
7577
runner = unittest.TextTestRunner(verbosity=0, stream=devnull)
78+
if memory_start is None:
79+
memory_start = tracemalloc.take_snapshot()
7680
result = runner.run(suite)
81+
memory_current = tracemalloc.take_snapshot()
7782
rusage = resource.getrusage(resource.RUSAGE_SELF)
7883
if max_rss < rusage.ru_maxrss:
7984
max_rss = rusage.ru_maxrss
@@ -83,24 +88,46 @@ def main():
8388

8489
# We don't want to flood stdout, so we rate-limit to 1 per second.
8590
if time.time() - last_print > 1:
86-
print(
87-
iteration,
88-
result.testsRun,
89-
len(result.failures),
90-
len(result.errors),
91-
len(result.skipped),
92-
rusage.ru_maxrss,
93-
min_rss,
94-
max_rss,
95-
max_rss_iter,
96-
sep="\t",
97-
end="\r",
91+
stdscr.clear()
92+
stdscr.addstr(0, 0, "iter\ttests\terr\tfail\tskip\tRSS\tmin\tmax\tmax@iter")
93+
stdscr.addstr(
94+
1,
95+
0,
96+
"\t".join(
97+
map(
98+
str,
99+
[
100+
iteration,
101+
result.testsRun,
102+
len(result.failures),
103+
len(result.errors),
104+
len(result.skipped),
105+
rusage.ru_maxrss,
106+
min_rss,
107+
max_rss,
108+
max_rss_iter,
109+
],
110+
)
111+
),
98112
)
113+
stats = memory_current.compare_to(memory_start, "traceback")
114+
rows, cols = stdscr.getmaxyx()
115+
for i, stat in enumerate(stats[: rows - 3], 1):
116+
stdscr.addstr(i + 2, 0, str(stat))
99117
last_print = time.time()
100-
sys.stdout.flush()
118+
stdscr.refresh()
101119

102120
iteration += 1
103121

104122

105123
if __name__ == "__main__":
106-
main()
124+
stdscr = curses.initscr()
125+
curses.noecho()
126+
curses.cbreak()
127+
128+
try:
129+
main(stdscr)
130+
finally:
131+
curses.echo()
132+
curses.nocbreak()
133+
curses.endwin()

0 commit comments

Comments
 (0)