MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / test_2791

Function test_2791

tests/test_memory.py:16–97  ·  view source on GitHub ↗

Check for memory leaks.

()

Source from the content-addressed store, hash-verified

14 return doc
15
16def test_2791():
17 '''
18 Check for memory leaks.
19 '''
20 if os.environ.get('PYODIDE_ROOT'):
21 print('test_2791(): not running on Pyodide - No module named \'psutil\'.')
22 return
23
24 if os.environ.get('PYMUPDF_RUNNING_ON_VALGRIND') == '1':
25 print(f'test_2791(): not running because PYMUPDF_RUNNING_ON_VALGRIND=1.')
26 return
27 if platform.system().startswith('MSYS_NT-'):
28 print(f'test_2791(): not running on msys2 - psutil not available.')
29 return
30 #stat_type = 'tracemalloc'
31 stat_type = 'psutil'
32 if stat_type == 'tracemalloc':
33 import tracemalloc
34 tracemalloc.start(10)
35 def get_stat():
36 current, peak = tracemalloc.get_traced_memory()
37 return current
38 elif stat_type == 'psutil':
39 # We use RSS, as used by mprof.
40 import psutil
41 process = psutil.Process()
42 def get_stat():
43 return process.memory_info().rss
44 else:
45 def get_stat():
46 return 0
47 n = 1000
48 verbose = False
49 if platform.python_implementation() == 'GraalVM':
50 n = 10
51 verbose = True
52 stats = [1] * n
53 for i in range(n):
54 if verbose:
55 print(f'{i+1}/{n}.', flush=1)
56 root = os.path.abspath(f'{__file__}/../../tests/resources')
57 with open(f'{root}/test_2791_content.pdf', 'rb') as content_pdf:
58 with open(f'{root}/test_2791_coverpage.pdf', 'rb') as coverpage_pdf:
59 content = content_pdf.read()
60 coverpage = coverpage_pdf.read()
61 merge_pdf(content, coverpage)
62 sys.stdout.flush()
63
64 gc.collect()
65 stats[i] = get_stat()
66
67 print(f'Memory usage {stat_type=}.')
68 for i, stat in enumerate(stats):
69 sys.stdout.write(f' {stat}')
70 #print(f' {i}: {stat}')
71 sys.stdout.write('\n')
72 first = stats[2]
73 last = stats[-1]

Callers

nothing calls this directly

Calls 5

merge_pdfFunction · 0.85
get_statFunction · 0.85
flushMethod · 0.80
getMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…