codeflash/tests/test_parse_line_profile_test_output.py

58 lines
2 KiB
Python
Raw Normal View History

2026-02-18 03:37:32 +00:00
import json
from pathlib import Path
from tempfile import TemporaryDirectory
from codeflash.languages import set_current_language
from codeflash.languages.base import Language
2026-03-03 00:00:42 +00:00
from codeflash.languages.java.line_profiler import JavaLineProfiler
2026-02-18 03:37:32 +00:00
def test_parse_line_profile_results_non_python_java_json():
set_current_language(Language.JAVA)
with TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
source_file = tmp_path / "Util.java"
source_file.write_text(
"""public class Util {
2026-02-18 03:37:32 +00:00
public static int f() {
int x = 1;
return x;
}
}
""",
encoding="utf-8",
)
profile_file = tmp_path / "line_profiler_output.json"
profile_data = {
f"{source_file.as_posix()}:3": {
"hits": 6,
"time": 1000,
"file": source_file.as_posix(),
"line": 3,
"content": "int x = 1;",
},
f"{source_file.as_posix()}:4": {
"hits": 6,
"time": 2000,
"file": source_file.as_posix(),
"line": 4,
"content": "return x;",
},
}
profile_file.write_text(json.dumps(profile_data), encoding="utf-8")
2026-02-18 03:37:32 +00:00
results = JavaLineProfiler.parse_results(profile_file)
2026-02-18 03:37:32 +00:00
assert results["unit"] == 1e-9
assert results["str_out"] == (
"# Timer unit: 1e-09 s\n"
"## Function: Util.java\n"
"## Total time: 3e-06 s\n"
"| Hits | Time | Per Hit | % Time | Line Contents |\n"
"|-------:|-------:|----------:|---------:|:----------------|\n"
"| 6 | 1000 | 166.7 | 33.3 | int x = 1; |\n"
"| 6 | 2000 | 333.3 | 66.7 | return x; |\n"
)
assert (source_file.as_posix(), 3, "Util.java") in results["timings"]
assert results["timings"][(source_file.as_posix(), 3, "Util.java")] == [(3, 6, 1000), (4, 6, 2000)]