style: Format all Python files with ruff
- Formatted 103 files to comply with ruff format requirements - No code logic changes, only formatting/whitespace - Fixes CI formatting check failures
This commit is contained in:
@@ -33,9 +33,9 @@ from .runner import BenchmarkRunner
|
||||
from .models import BenchmarkReport, Metric
|
||||
|
||||
__all__ = [
|
||||
'Benchmark',
|
||||
'BenchmarkResult',
|
||||
'BenchmarkRunner',
|
||||
'BenchmarkReport',
|
||||
'Metric',
|
||||
"Benchmark",
|
||||
"BenchmarkResult",
|
||||
"BenchmarkRunner",
|
||||
"BenchmarkReport",
|
||||
"Metric",
|
||||
]
|
||||
|
||||
@@ -11,12 +11,7 @@ from typing import Any
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
from .models import (
|
||||
Metric,
|
||||
TimingResult,
|
||||
MemoryUsage,
|
||||
BenchmarkReport
|
||||
)
|
||||
from .models import Metric, TimingResult, MemoryUsage, BenchmarkReport
|
||||
|
||||
|
||||
class BenchmarkResult:
|
||||
@@ -97,7 +92,7 @@ class BenchmarkResult:
|
||||
memory=self.memory,
|
||||
metrics=self.metrics,
|
||||
system_info=self.system_info,
|
||||
recommendations=self.recommendations
|
||||
recommendations=self.recommendations,
|
||||
)
|
||||
|
||||
|
||||
@@ -161,7 +156,7 @@ class Benchmark:
|
||||
operation=operation,
|
||||
duration=duration,
|
||||
iterations=iterations,
|
||||
avg_duration=duration / iterations if iterations > 1 else duration
|
||||
avg_duration=duration / iterations if iterations > 1 else duration,
|
||||
)
|
||||
|
||||
self.result.add_timing(timing)
|
||||
@@ -201,7 +196,7 @@ class Benchmark:
|
||||
before_mb=mem_before,
|
||||
after_mb=mem_after,
|
||||
peak_mb=peak_memory,
|
||||
allocated_mb=mem_after - mem_before
|
||||
allocated_mb=mem_after - mem_before,
|
||||
)
|
||||
|
||||
self.result.add_memory(usage)
|
||||
@@ -212,7 +207,7 @@ class Benchmark:
|
||||
*args,
|
||||
operation: str | None = None,
|
||||
track_memory: bool = False,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
) -> Any:
|
||||
"""
|
||||
Measure function execution.
|
||||
@@ -260,17 +255,16 @@ class Benchmark:
|
||||
def load_config(path):
|
||||
return json.load(open(path))
|
||||
"""
|
||||
|
||||
def decorator(func: Callable) -> Callable:
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
return self.measure(
|
||||
func,
|
||||
*args,
|
||||
operation=operation,
|
||||
track_memory=track_memory,
|
||||
**kwargs
|
||||
func, *args, operation=operation, track_memory=track_memory, **kwargs
|
||||
)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
def metric(self, name: str, value: float, unit: str):
|
||||
@@ -285,11 +279,7 @@ class Benchmark:
|
||||
Examples:
|
||||
benchmark.metric("pages_per_sec", 12.5, "pages/sec")
|
||||
"""
|
||||
metric = Metric(
|
||||
name=name,
|
||||
value=value,
|
||||
unit=unit
|
||||
)
|
||||
metric = Metric(name=name, value=value, unit=unit)
|
||||
self.result.add_metric(metric)
|
||||
|
||||
def recommend(self, text: str):
|
||||
@@ -328,7 +318,7 @@ class Benchmark:
|
||||
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(path, 'w') as f:
|
||||
with open(path, "w") as f:
|
||||
f.write(report.model_dump_json(indent=2))
|
||||
|
||||
def analyze(self):
|
||||
@@ -339,11 +329,7 @@ class Benchmark:
|
||||
"""
|
||||
# Analyze timing bottlenecks
|
||||
if self.result.timings:
|
||||
sorted_timings = sorted(
|
||||
self.result.timings,
|
||||
key=lambda t: t.duration,
|
||||
reverse=True
|
||||
)
|
||||
sorted_timings = sorted(self.result.timings, key=lambda t: t.duration, reverse=True)
|
||||
|
||||
slowest = sorted_timings[0]
|
||||
total_time = sum(t.duration for t in self.result.timings)
|
||||
@@ -351,7 +337,7 @@ class Benchmark:
|
||||
if slowest.duration > total_time * 0.5:
|
||||
self.recommend(
|
||||
f"Bottleneck: '{slowest.operation}' takes "
|
||||
f"{slowest.duration:.1f}s ({slowest.duration/total_time*100:.0f}% of total)"
|
||||
f"{slowest.duration:.1f}s ({slowest.duration / total_time * 100:.0f}% of total)"
|
||||
)
|
||||
|
||||
# Analyze memory usage
|
||||
@@ -360,8 +346,7 @@ class Benchmark:
|
||||
|
||||
if peak > 1000: # >1GB
|
||||
self.recommend(
|
||||
f"High memory usage: {peak:.0f}MB peak. "
|
||||
"Consider processing in batches."
|
||||
f"High memory usage: {peak:.0f}MB peak. Consider processing in batches."
|
||||
)
|
||||
|
||||
# Check for memory leaks
|
||||
|
||||
@@ -14,8 +14,7 @@ class Metric(BaseModel):
|
||||
value: float = Field(..., description="Metric value")
|
||||
unit: str = Field(..., description="Unit (seconds, bytes, pages/sec, etc.)")
|
||||
timestamp: datetime = Field(
|
||||
default_factory=datetime.utcnow,
|
||||
description="When metric was recorded"
|
||||
default_factory=datetime.utcnow, description="When metric was recorded"
|
||||
)
|
||||
|
||||
|
||||
@@ -48,26 +47,13 @@ class BenchmarkReport(BaseModel):
|
||||
finished_at: datetime = Field(..., description="Finish time")
|
||||
total_duration: float = Field(..., description="Total duration in seconds")
|
||||
|
||||
timings: list[TimingResult] = Field(
|
||||
default_factory=list,
|
||||
description="Timing results"
|
||||
)
|
||||
memory: list[MemoryUsage] = Field(
|
||||
default_factory=list,
|
||||
description="Memory usage results"
|
||||
)
|
||||
metrics: list[Metric] = Field(
|
||||
default_factory=list,
|
||||
description="Additional metrics"
|
||||
)
|
||||
timings: list[TimingResult] = Field(default_factory=list, description="Timing results")
|
||||
memory: list[MemoryUsage] = Field(default_factory=list, description="Memory usage results")
|
||||
metrics: list[Metric] = Field(default_factory=list, description="Additional metrics")
|
||||
|
||||
system_info: dict[str, Any] = Field(
|
||||
default_factory=dict,
|
||||
description="System information"
|
||||
)
|
||||
system_info: dict[str, Any] = Field(default_factory=dict, description="System information")
|
||||
recommendations: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Optimization recommendations"
|
||||
default_factory=list, description="Optimization recommendations"
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -89,14 +75,8 @@ class ComparisonReport(BaseModel):
|
||||
baseline: BenchmarkReport = Field(..., description="Baseline benchmark")
|
||||
current: BenchmarkReport = Field(..., description="Current benchmark")
|
||||
|
||||
improvements: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Performance improvements"
|
||||
)
|
||||
regressions: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Performance regressions"
|
||||
)
|
||||
improvements: list[str] = Field(default_factory=list, description="Performance improvements")
|
||||
regressions: list[str] = Field(default_factory=list, description="Performance regressions")
|
||||
|
||||
speedup_factor: float = Field(..., description="Overall speedup factor")
|
||||
memory_change_mb: float = Field(..., description="Memory usage change (MB)")
|
||||
|
||||
@@ -46,10 +46,7 @@ class BenchmarkRunner:
|
||||
self.output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def run(
|
||||
self,
|
||||
name: str,
|
||||
benchmark_func: Callable[[Benchmark], None],
|
||||
save: bool = True
|
||||
self, name: str, benchmark_func: Callable[[Benchmark], None], save: bool = True
|
||||
) -> BenchmarkReport:
|
||||
"""
|
||||
Run single benchmark.
|
||||
@@ -83,7 +80,7 @@ class BenchmarkRunner:
|
||||
filename = f"{name}_{timestamp}.json"
|
||||
path = self.output_dir / filename
|
||||
|
||||
with open(path, 'w') as f:
|
||||
with open(path, "w") as f:
|
||||
f.write(report.model_dump_json(indent=2))
|
||||
|
||||
print(f"📊 Saved benchmark: {path}")
|
||||
@@ -91,9 +88,7 @@ class BenchmarkRunner:
|
||||
return report
|
||||
|
||||
def run_suite(
|
||||
self,
|
||||
benchmarks: dict[str, Callable[[Benchmark], None]],
|
||||
save: bool = True
|
||||
self, benchmarks: dict[str, Callable[[Benchmark], None]], save: bool = True
|
||||
) -> dict[str, BenchmarkReport]:
|
||||
"""
|
||||
Run multiple benchmarks.
|
||||
@@ -122,11 +117,7 @@ class BenchmarkRunner:
|
||||
|
||||
return reports
|
||||
|
||||
def compare(
|
||||
self,
|
||||
baseline_path: Path,
|
||||
current_path: Path
|
||||
) -> ComparisonReport:
|
||||
def compare(self, baseline_path: Path, current_path: Path) -> ComparisonReport:
|
||||
"""
|
||||
Compare two benchmark reports.
|
||||
|
||||
@@ -215,7 +206,7 @@ class BenchmarkRunner:
|
||||
improvements=improvements,
|
||||
regressions=regressions,
|
||||
speedup_factor=speedup_factor,
|
||||
memory_change_mb=memory_change_mb
|
||||
memory_change_mb=memory_change_mb,
|
||||
)
|
||||
|
||||
def list_benchmarks(self) -> list[dict[str, Any]]:
|
||||
@@ -237,13 +228,15 @@ class BenchmarkRunner:
|
||||
with open(path) as f:
|
||||
data = json.load(f)
|
||||
|
||||
benchmarks.append({
|
||||
"name": data["name"],
|
||||
"path": str(path),
|
||||
"started_at": data["started_at"],
|
||||
"duration": data["total_duration"],
|
||||
"operations": len(data.get("timings", []))
|
||||
})
|
||||
benchmarks.append(
|
||||
{
|
||||
"name": data["name"],
|
||||
"path": str(path),
|
||||
"started_at": data["started_at"],
|
||||
"duration": data["total_duration"],
|
||||
"operations": len(data.get("timings", [])),
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
# Skip invalid files
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user