-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_reporting.py
More file actions
490 lines (455 loc) · 22.2 KB
/
test_reporting.py
File metadata and controls
490 lines (455 loc) · 22.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
from __future__ import annotations
import json
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from python_lsp_compare.cli import main
from python_lsp_compare.report_csv import build_csv_rows
from python_lsp_compare.report_markdown import render_markdown_report
class ReportingTests(unittest.TestCase):
def test_render_markdown_report_sorts_benchmark_tables_by_fastest_average(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
fast_report_path = temp_path / "fast.json"
slow_report_path = temp_path / "slow.json"
summary_path = temp_path / "summary.json"
def build_metric(duration_ms: float, completion_items: int, phase: str = "measured") -> dict[str, object]:
return {
"kind": "request",
"method": "textDocument/completion",
"duration_ms": duration_ms,
"result_summary": {
"present": True,
"empty": False,
"completion_item_count": completion_items,
},
"context": {"phase": phase},
}
def build_report(total_duration_ms: float, mean_ms: float, measured_duration_ms: float, completion_items: int) -> dict[str, object]:
return {
"benchmark_reports": [
{
"name": "fixture_suite",
"success": True,
"total_duration_ms": total_duration_ms,
"points": [
{
"label": "completion fixture",
"method": "textDocument/completion",
"file_path": "fixture.py",
"line": 1,
"character": 1,
"success": True,
"summary": {
"mean_ms": mean_ms,
"p95_ms": mean_ms,
"validation": {"passed": True, "failure_count": 0},
},
"metrics": [
build_metric(measured_duration_ms, completion_items),
build_metric(measured_duration_ms, completion_items),
],
}
],
}
]
}
fast_report_path.write_text(
json.dumps(build_report(total_duration_ms=10.0, mean_ms=1.5, measured_duration_ms=1.0, completion_items=2)),
encoding="utf-8",
)
slow_report_path.write_text(
json.dumps(build_report(total_duration_ms=30.0, mean_ms=4.5, measured_duration_ms=5.0, completion_items=4)),
encoding="utf-8",
)
summary_path.write_text(
json.dumps(
{
"generated_at": "20260320T000000Z",
"baseline_server": "slow",
"requested_benchmarks": ["fixture_suite"],
"servers": [
{
"id": "slow",
"display_name": "Slow Server",
"output_path": str(slow_report_path),
"success": True,
},
{
"id": "fast",
"display_name": "Fast Server",
"output_path": str(fast_report_path),
"success": True,
},
],
}
),
encoding="utf-8",
)
markdown = render_markdown_report(summary_path, baseline_server_id="slow")
overview_start = markdown.index("## Overview")
benchmark_start = markdown.index("## Benchmark: fixture_suite")
point_start = markdown.index("### completion fixture")
overview_section = markdown[overview_start:benchmark_start]
benchmark_section = markdown[benchmark_start:point_start]
point_section = markdown[point_start:]
self.assertLess(overview_section.index("| Fast Server |"), overview_section.index("| Slow Server |"))
self.assertLess(benchmark_section.index("| Fast Server |"), benchmark_section.index("| Slow Server |"))
self.assertLess(point_section.index("| Fast Server |"), point_section.index("| Slow Server |"))
def test_bench_servers_writes_markdown_comparison_with_result_differences(self) -> None:
server_script = Path(__file__).parent / "fixtures" / "fake_lsp_server.py"
benchmark_root = Path(__file__).parent / "fixtures"
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
config_path = temp_path / "servers.json"
output_dir = temp_path / "results"
markdown_path = temp_path / "comparison.md"
csv_path = temp_path / "comparison.csv"
latest_results_path = temp_path / "latest-results.md"
config_path.write_text(
json.dumps(
{
"servers": [
{
"id": "small-results",
"displayName": "Small Results",
"launch": {
"command": sys.executable,
"args": [
str(server_script),
"--completion-items",
"1",
"--hover-text",
"short",
],
},
},
{
"id": "large-results",
"displayName": "Large Results",
"launch": {
"command": sys.executable,
"args": [
str(server_script),
"--completion-items",
"3",
"--hover-text",
"this is a much longer hover payload",
],
},
},
]
}
),
encoding="utf-8",
)
exit_code = main(
[
"bench-servers",
"--config",
str(config_path),
"--benchmark-root",
str(benchmark_root),
"--output-dir",
str(output_dir),
"--markdown-output",
str(markdown_path),
"--csv-output",
str(csv_path),
"--baseline-server",
"large-results",
]
)
self.assertEqual(exit_code, 0)
markdown = markdown_path.read_text(encoding="utf-8")
self.assertIn("# Python LSP Benchmark Comparison", markdown)
self.assertIn("Small Results", markdown)
self.assertIn("Large Results", markdown)
self.assertIn("Baseline server: Large Results (large-results)", markdown)
self.assertIn("## Server Versions", markdown)
self.assertIn("Completions found", markdown)
self.assertIn("Avg measured ms", markdown)
self.assertIn("Result Differences", markdown)
self.assertIn("completion fixture: result differences detected", markdown)
self.assertIn("Delta vs Large Results", markdown)
self.assertNotIn("Avg result chars", markdown)
csv_text = csv_path.read_text(encoding="utf-8")
self.assertIn("baseline_server_id", csv_text)
self.assertIn("large-results", csv_text)
self.assertIn("completion_item_count", csv_text)
self.assertNotIn("avg_result_chars", csv_text)
self.assertTrue(latest_results_path.exists())
latest_results = latest_results_path.read_text(encoding="utf-8")
self.assertIn("# Python LSP Benchmark Comparison", latest_results)
summary = json.loads(next(output_dir.glob("summary-*.json")).read_text(encoding="utf-8"))
self.assertEqual(summary["baseline_server"], "large-results")
self.assertIn("version", summary["servers"][0])
report_path = output_dir / Path(summary["servers"][0]["output_path"]).name
report = json.loads(report_path.read_text(encoding="utf-8"))
completion_point = next(
point
for point in report["benchmark_reports"][0]["points"]
if point["method"] == "textDocument/completion"
)
result_metrics = completion_point["summary"]["result_summary"]["metrics"]
self.assertEqual(result_metrics["completion_item_count"]["mean"], 1.0)
self.assertEqual(completion_point["summary"]["result_summary"]["non_empty_count"], 2)
def test_render_markdown_report_shows_tsp_type_name_differences(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
first_report_path = temp_path / "first.json"
second_report_path = temp_path / "second.json"
summary_path = temp_path / "summary.json"
def build_tsp_metric(duration_ms: float, type_name: str) -> dict[str, object]:
return {
"kind": "request",
"method": "typeServer/getComputedType",
"duration_ms": duration_ms,
"result_summary": {
"present": True,
"empty": False,
"type_name": type_name,
"size_chars": 25,
},
"context": {"phase": "measured"},
}
def build_report(type_name: str) -> dict[str, object]:
return {
"benchmark_reports": [
{
"name": "tsp_fixture",
"success": True,
"total_duration_ms": 10.0,
"points": [
{
"label": "narrowed type",
"method": "typeServer/getComputedType",
"file_path": "flow.py",
"line": 1,
"character": 1,
"success": True,
"summary": {
"mean_ms": 1.0,
"p95_ms": 1.0,
"validation": {"passed": True, "failure_count": 0},
},
"metrics": [build_tsp_metric(1.0, type_name), build_tsp_metric(1.0, type_name)],
}
],
}
]
}
first_report_path.write_text(json.dumps(build_report("int")), encoding="utf-8")
second_report_path.write_text(json.dumps(build_report("str")), encoding="utf-8")
summary_path.write_text(
json.dumps(
{
"generated_at": "20260320T000000Z",
"baseline_server": "baseline",
"requested_benchmarks": ["tsp_fixture"],
"servers": [
{"id": "baseline", "display_name": "Baseline", "output_path": str(first_report_path), "success": True},
{"id": "candidate", "display_name": "Candidate", "output_path": str(second_report_path), "success": True},
],
}
),
encoding="utf-8",
)
markdown = render_markdown_report(summary_path, baseline_server_id="baseline")
rows = build_csv_rows(summary_path, baseline_server_id="baseline")
self.assertIn("Type name", markdown)
self.assertIn("int", markdown)
self.assertIn("str", markdown)
self.assertTrue(any(row["result_metric_name"] == "type_name" for row in rows))
def test_render_markdown_report_shows_friendly_semantic_token_method_name(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
report_path = temp_path / "semantic.json"
summary_path = temp_path / "summary.json"
report_path.write_text(
json.dumps(
{
"benchmark_reports": [
{
"name": "tsp_semantic",
"success": True,
"total_duration_ms": 10.0,
"points": [
{
"label": "django semantic tokens",
"method": "typeServer/semanticTokens",
"file_path": "semantic.py",
"line": 1,
"character": 1,
"success": True,
"summary": {
"mean_ms": 1.0,
"p95_ms": 1.0,
"validation": {"passed": True, "failure_count": 0},
},
"metrics": [
{
"kind": "request",
"method": "typeServer/semanticTokens",
"duration_ms": 1.0,
"result_summary": {
"present": True,
"empty": False,
"top_level_count": 12,
},
"context": {"phase": "measured"},
}
],
}
],
}
]
}
),
encoding="utf-8",
)
summary_path.write_text(
json.dumps(
{
"generated_at": "20260320T000000Z",
"baseline_server": "pyrefly",
"requested_benchmarks": ["tsp_semantic"],
"servers": [
{"id": "pyrefly", "display_name": "Pyrefly", "output_path": str(report_path), "success": True},
],
}
),
encoding="utf-8",
)
markdown = render_markdown_report(summary_path, baseline_server_id="pyrefly")
self.assertIn("semantic token impl using typeServer/getComputedType", markdown)
self.assertNotIn("Method: `typeServer/semanticTokens`", markdown)
def test_render_markdown_report_includes_server_only_tsp_benchmark(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
pyrefly_report_path = temp_path / "pyrefly.json"
other_report_path = temp_path / "other.json"
summary_path = temp_path / "summary.json"
def build_tsp_report() -> dict[str, object]:
return {
"benchmark_reports": [
{
"name": "tsp_core",
"success": True,
"total_duration_ms": 10.0,
"points": [
{
"label": "narrowed type",
"method": "typeServer/getComputedType",
"file_path": "flow.py",
"line": 1,
"character": 1,
"success": True,
"summary": {
"mean_ms": 1.0,
"p95_ms": 1.0,
"validation": {"passed": True, "failure_count": 0},
},
"metrics": [
{
"kind": "request",
"method": "typeServer/getComputedType",
"duration_ms": 1.0,
"result_summary": {"present": True, "empty": False, "type_name": "int"},
"context": {"phase": "measured"},
}
],
}
],
}
]
}
pyrefly_report_path.write_text(json.dumps(build_tsp_report()), encoding="utf-8")
other_report_path.write_text(json.dumps({"benchmark_reports": []}), encoding="utf-8")
summary_path.write_text(
json.dumps(
{
"generated_at": "20260320T000000Z",
"baseline_server": "pyright",
"requested_benchmarks": ["data_science", "web"],
"servers": [
{"id": "pyrefly", "display_name": "Pyrefly", "output_path": str(pyrefly_report_path), "success": True},
{"id": "pyright", "display_name": "Pyright", "output_path": str(other_report_path), "success": True},
],
}
),
encoding="utf-8",
)
markdown = render_markdown_report(summary_path, baseline_server_id="pyright")
self.assertIn("## Benchmark: tsp_core", markdown)
def test_render_report_rebuilds_markdown_from_summary_json(self) -> None:
server_script = Path(__file__).parent / "fixtures" / "fake_lsp_server.py"
benchmark_root = Path(__file__).parent / "fixtures"
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
config_path = temp_path / "servers.json"
output_dir = temp_path / "results"
summary_path = temp_path / "summary.json"
markdown_path = temp_path / "rerendered.md"
csv_path = temp_path / "rerendered.csv"
latest_results_path = temp_path / "latest-results.md"
config_path.write_text(
json.dumps(
{
"servers": [
{
"id": "demo",
"displayName": "Demo",
"launch": {
"command": sys.executable,
"args": [str(server_script)],
},
}
]
}
),
encoding="utf-8",
)
exit_code = main(
[
"bench-servers",
"--config",
str(config_path),
"--benchmark-root",
str(benchmark_root),
"--output-dir",
str(output_dir),
"--summary-output",
str(summary_path),
]
)
self.assertEqual(exit_code, 0)
render_exit = main(
[
"render-report",
"--summary",
str(summary_path),
"--output",
str(markdown_path),
"--csv-output",
str(csv_path),
"--title",
"Custom Comparison",
]
)
self.assertEqual(render_exit, 0)
markdown = markdown_path.read_text(encoding="utf-8")
csv_text = csv_path.read_text(encoding="utf-8")
self.assertIn("# Custom Comparison", markdown)
self.assertIn("Demo", markdown)
self.assertIn("Avg measured ms", markdown)
self.assertIn("server_id", csv_text)
self.assertIn("demo", csv_text)
self.assertTrue(latest_results_path.exists())
latest_md = latest_results_path.read_text(encoding="utf-8")
self.assertIn("# Custom Comparison", latest_md)
self.assertIn("Demo", latest_md)
if __name__ == "__main__":
unittest.main()