Repository navigation
Expand file tree
/
Copy pathbench.py
More file actions
executable file
·175 lines (147 loc) · 5.02 KB
/
Copy pathbench.py
File metadata and controls
executable file
·175 lines (147 loc) · 5.02 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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
import sys
import time
from typing import List, Optional
from rich.console import Console
from rich.progress import (
Progress,
SpinnerColumn,
TextColumn,
TimeElapsedColumn,
BarColumn,
TaskID,
)
from benchmarks import cpu, disk, memory, database, git
from benchmarks.reporter import (
compute_score,
print_results,
print_summary,
print_sysinfo,
save_report,
)
from benchmarks.runner import BenchmarkResult
from benchmarks import sysinfo as sysinfo_mod
ALL_CATEGORIES = ["cpu", "disk", "memory", "database", "git"]
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="cli-benchmark",
description="Linux server performance benchmark tool",
)
parser.add_argument(
"--only",
metavar="CATEGORIES",
help="Comma-separated list of categories to run (cpu,disk,memory,database,git)",
)
parser.add_argument(
"--output",
metavar="PATH",
help="Override default JSON report output path",
)
parser.add_argument(
"--quick",
action="store_true",
help="Use 10x smaller workloads for a fast smoke-test run",
)
parser.add_argument(
"--no-color",
action="store_true",
help="Disable color output",
)
parser.add_argument(
"--delay",
metavar="MS",
type=int,
default=0,
help="Add a per-line delay (milliseconds) when printing results",
)
return parser.parse_args()
def _make_progress(console: Console) -> Progress:
return Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
TimeElapsedColumn(),
TextColumn("{task.fields[status]}"),
console=console,
transient=False,
)
def _run_category(
name: str,
runner_fn,
quick: bool,
progress: Progress,
task_id: TaskID,
results: List[BenchmarkResult],
) -> None:
progress.update(task_id, status="[cyan]running…[/cyan]")
try:
cat_results = runner_fn(quick)
results.extend(cat_results)
errors = [r for r in cat_results if r.error and not r.skipped]
skips = [r for r in cat_results if r.skipped]
if errors:
progress.update(task_id, total=1, completed=1, status=f"[red]done ({len(errors)} error(s))[/red]")
elif skips and len(skips) == len(cat_results):
progress.update(task_id, total=1, completed=1, status="[yellow]SKIP[/yellow]")
else:
progress.update(task_id, total=1, completed=1, status="[green]done[/green]")
except Exception as exc:
progress.update(task_id, total=1, completed=1, status=f"[red]error: {exc}[/red]")
def main() -> int:
args = _parse_args()
console = Console(no_color=args.no_color)
categories: List[str] = ALL_CATEGORIES
if args.only:
requested = [c.strip().lower() for c in args.only.split(",")]
unknown = [c for c in requested if c not in ALL_CATEGORIES]
if unknown:
console.print(f"[red]Unknown categories: {', '.join(unknown)}[/red]")
console.print(f"Valid categories: {', '.join(ALL_CATEGORIES)}")
return 1
categories = requested
bench_dir = os.getcwd()
info = sysinfo_mod.collect(bench_dir)
print_sysinfo(console, info)
if args.quick:
console.print("[yellow]Quick mode: using reduced workloads[/yellow]\n")
results: List[BenchmarkResult] = []
wall_start = time.perf_counter()
category_map = {
"cpu": ("CPU benchmarks", cpu.run_all),
"disk": ("Disk I/O benchmarks", disk.run_all),
"memory": ("Memory benchmarks", memory.run_all),
"database": ("Database benchmarks", database.run_all),
"git": ("Git benchmarks", git.run_all),
}
try:
with _make_progress(console) as progress:
tasks = {}
for cat in categories:
label, _ = category_map[cat]
task_id = progress.add_task(label, status="[dim]waiting…[/dim]")
tasks[cat] = task_id
for cat in categories:
task_id = tasks[cat]
_, runner_fn = category_map[cat]
_run_category(cat, runner_fn, args.quick, progress, task_id, results)
except KeyboardInterrupt:
console.print("\n[yellow]Interrupted by user.[/yellow]")
return 130
except Exception:
console.print_exception()
return 1
wall_time = time.perf_counter() - wall_start
console.print()
print_results(console, results, delay_ms=args.delay)
score = compute_score(results)
try:
report_path = save_report(results, info, score, args.output)
except Exception as exc:
console.print(f"[red]Failed to save report: {exc}[/red]")
report_path = "(not saved)"
print_summary(console, results, wall_time, report_path, score, delay_ms=args.delay)
return 0
if __name__ == "__main__":
sys.exit(main())