| 1 |
""" |
| 2 |
This is bound very closely to the current implementation of |
| 3 |
the tests in `test/line_attribution_tests. |
| 4 |
|
| 5 |
The two things that matter are the number of loops, the size |
| 6 |
of the allocations, and the exact line numbers. |
| 7 |
|
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
expected_md5_sums = { |
| 12 |
"test/line_attribution_tests/line_after_final_alloc.py": "2d457078fae8c2a7b498cf7dd87324bc", |
| 13 |
"test/line_attribution_tests/loop_below_threshold.py": "96f9e1c086ecdd522a6c565e0e751df6", |
| 14 |
"test/line_attribution_tests/loop_with_multiple_lines.py": "ed6d9802f31ef9d7a8636e6d0d728013", |
| 15 |
"test/line_attribution_tests/loop_with_one_alloc.py": "fe952422358a8070c8049fdab1c512af", |
| 16 |
"test/line_attribution_tests/loop_with_two_allocs.py": "9ca21ee9f9a768c4d05f3c4ba23444e9", |
| 17 |
} |
| 18 |
|
| 19 |
import subprocess |
| 20 |
import tempfile |
| 21 |
import sys |
| 22 |
from pathlib import Path |
| 23 |
from hashlib import md5 |
| 24 |
from scalene.scalene_json import ScaleneJSONSchema |
| 25 |
|
| 26 |
N_LOOPS = 31 |
| 27 |
LOOP_ALLOC_LINENO = 5 |
| 28 |
OUT_OF_LOOP_ALLOC_LINENO = 8 |
| 29 |
|
| 30 |
|
| 31 |
def check_for_changes(): |
| 32 |
errors = [] |
| 33 |
for fname, expected_sum in expected_md5_sums.items(): |
| 34 |
with open(fname, "rb") as f: |
| 35 |
digest = md5(f.read()).hexdigest() |
| 36 |
if digest != expected_sum: |
| 37 |
errors.append(fname) |
| 38 |
assert len(errors) == 0, f'Detected change in file(s) {",".join(errors)}' |
| 39 |
|
| 40 |
|
| 41 |
def get_line(scalene_profile: ScaleneJSONSchema, lineno: int): |
| 42 |
files = list(scalene_profile.files.keys()) |
| 43 |
assert len(files) == 1 |
| 44 |
filename = files[0] |
| 45 |
return scalene_profile.files[filename].lines[lineno - 1] |
| 46 |
|
| 47 |
|
| 48 |
def get_profile(test_stem, outdir_p, test_dir): |
| 49 |
proc = subprocess.run( |
| 50 |
[ |
| 51 |
sys.executable, |
| 52 |
"-m", |
| 53 |
"scalene", |
| 54 |
"--cli", |
| 55 |
"--json", |
| 56 |
"--outfile", |
| 57 |
outdir_p / f"{test_stem}.json", |
| 58 |
test_dir / f"{test_stem}.py", |
| 59 |
], |
| 60 |
|
| 61 |
check=True, |
| 62 |
) |
| 63 |
with open(outdir_p / f"{test_stem}.json", "r") as f: |
| 64 |
profile = ScaleneJSONSchema.model_validate_json(f.read()) |
| 65 |
return (test_stem, profile) |
| 66 |
|
| 67 |
|
| 68 |
def main(): |
| 69 |
check_for_changes() |
| 70 |
test_dir = Path(__file__).parent / "line_attribution_tests" |
| 71 |
with tempfile.TemporaryDirectory() as outdir: |
| 72 |
outdir_p = Path(outdir) |
| 73 |
one_alloc = get_profile("loop_with_one_alloc", outdir_p, test_dir) |
| 74 |
two_on_one_line = get_profile("loop_with_two_allocs", outdir_p, test_dir) |
| 75 |
below_threshold = get_profile("loop_below_threshold", outdir_p, test_dir) |
| 76 |
line_after_final_alloc = get_profile( |
| 77 |
"line_after_final_alloc", outdir_p, test_dir |
| 78 |
) |
| 79 |
errors = [] |
| 80 |
for stem, profile in [one_alloc, two_on_one_line, line_after_final_alloc]: |
| 81 |
line = get_line(profile, LOOP_ALLOC_LINENO) |
| 82 |
if not line.n_mallocs == N_LOOPS: |
| 83 |
errors.append( |
| 84 |
f"Expected {N_LOOPS} distinct lines on {stem}, got {line.n_mallocs} on line {LOOP_ALLOC_LINENO}" |
| 85 |
) |
| 86 |
|
| 87 |
bt_stem, bt_prof = below_threshold |
| 88 |
bt_line = get_line(bt_prof, LOOP_ALLOC_LINENO) |
| 89 |
if not bt_line.n_mallocs < N_LOOPS: |
| 90 |
errors.append( |
| 91 |
f"{bt_stem} makes smaller allocations than the allocation sampling window, so fewer than {N_LOOPS} allocations on {LOOP_ALLOC_LINENO} should be reported. Got {bt_line.n_mallocs} mallocs" |
| 92 |
) |
| 93 |
|
| 94 |
for stem, profile in [ |
| 95 |
one_alloc, |
| 96 |
two_on_one_line, |
| 97 |
below_threshold, |
| 98 |
line_after_final_alloc, |
| 99 |
]: |
| 100 |
line = get_line(profile, OUT_OF_LOOP_ALLOC_LINENO) |
| 101 |
if not line.n_mallocs == 1: |
| 102 |
errors.append( |
| 103 |
f"Line {OUT_OF_LOOP_ALLOC_LINENO} in {stem} makes a large allocation, so it should be reported." |
| 104 |
) |
| 105 |
|
| 106 |
if len(errors) > 0: |
| 107 |
for error in errors: |
| 108 |
print(f"ERROR: {error}") |
| 109 |
for profile in [ |
| 110 |
one_alloc, |
| 111 |
two_on_one_line, |
| 112 |
below_threshold, |
| 113 |
line_after_final_alloc, |
| 114 |
]: |
| 115 |
print("\n\n\n\n") |
| 116 |
print(profile[1].model_dump_json(indent=4)) |
| 117 |
exit(1) |
| 118 |
else: |
| 119 |
print("PASS") |
| 120 |
exit(0) |
| 121 |
|
| 122 |
|
| 123 |
if __name__ == "__main__": |
| 124 |
main() |