Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(weave): Add import time benchmarking script #3234

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions scripts/benchmark_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

import statistics
import subprocess
import sys

from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TimeElapsedColumn
from rich.table import Table


def run_single_import():
cmd = """
import time
start = time.perf_counter()
import weave
end = time.perf_counter()
print(end - start)
"""
result = subprocess.run([sys.executable, "-c", cmd], capture_output=True, text=True)
return float(result.stdout)


def benchmark(iterations=10):
console = Console()
times = []

with Progress(
SpinnerColumn(),
*Progress.get_default_columns(),
TimeElapsedColumn(),
console=console,
) as progress:
task = progress.add_task("[cyan]Running import tests...", total=iterations)

for _ in range(iterations):
times.append(run_single_import())
progress.advance(task)

# Display results in a nice table
table = Table(title="Import Time Benchmark Results")
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green")

table.add_row("Mean import time", f"{statistics.mean(times):.4f}s")
table.add_row("Median import time", f"{statistics.median(times):.4f}s")
table.add_row("Std dev", f"{statistics.stdev(times):.4f}s")
table.add_row("Min time", f"{min(times):.4f}s")
table.add_row("Max time", f"{max(times):.4f}s")

console.print("\n")
console.print(table)

# Show individual times
times_table = Table(title="Individual Import Times")
times_table.add_column("Run #", style="cyan")
times_table.add_column("Time (seconds)", style="green")

for i, t in enumerate(times, 1):
times_table.add_row(str(i), f"{t:.4f}")

console.print("\n")
console.print(times_table)


if __name__ == "__main__":
benchmark()
13 changes: 13 additions & 0 deletions tests/trace/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pathlib import Path


def test_import_not_slow(monkeypatch):
root_dir = Path.cwd().parent.parent # root dir of repo
monkeypatch.setenv("PYTHONPATH", str(root_dir))

from scripts.benchmark_import import run_single_import

import_time = run_single_import()

# Ideally the import takes < 1s, but in CI it can take up to 3s.
assert import_time < 3, f"Import time was {import_time} seconds"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make configurable? also pr description says 2s

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated description

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

Loading