Skip to content

Commit 5c5db26

Browse files
committed
xxxx
1 parent f559d31 commit 5c5db26

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

ai-track-docs/perf-baseline.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Performance Baseline
2+
3+
## Function
4+
5+
- Module: `samples/book-app-project/books.py`
6+
- Target: `BookCollection.list_books()`
7+
- Measurement approach: standalone micro-benchmark in `samples/book-app-project/benchmark_list_books.py`
8+
9+
## Command
10+
11+
```bash
12+
cd samples/book-app-project
13+
python benchmark_list_books.py
14+
```
15+
16+
## Baseline Results
17+
18+
Run 1
19+
20+
- Iterations: 10000
21+
- Book count: 1000
22+
- Mean: 0.122 us
23+
- Median: 0.100 us
24+
- Min: 0.000 us
25+
- Max: 2.100 us
26+
27+
Run 2
28+
29+
- Iterations: 10000
30+
- Book count: 1000
31+
- Mean: 0.098 us
32+
- Median: 0.100 us
33+
- Min: 0.000 us
34+
- Max: 3.700 us
35+
36+
## Variance Notes
37+
38+
- The median stayed at 0.100 us across both runs, which matches the simple in-memory return path.
39+
- The mean moved from 0.122 us to 0.098 us and the max moved from 2.100 us to 3.700 us, which is consistent with small scheduler or interpreter noise.
40+
- Re-run the benchmark from a quiet shell if you want to compare future changes against a cleaner baseline.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import statistics
2+
import time
3+
4+
from books import Book, BookCollection
5+
6+
7+
def run_benchmark(iterations: int = 10000, book_count: int = 1000) -> dict[str, float]:
8+
collection = BookCollection()
9+
collection.books = [
10+
Book(title=f"Book {index}", author="Benchmark Author", year=2000 + (index % 20))
11+
for index in range(book_count)
12+
]
13+
14+
samples = []
15+
for _ in range(iterations):
16+
start = time.perf_counter()
17+
collection.list_books()
18+
samples.append((time.perf_counter() - start) * 1_000_000)
19+
20+
return {
21+
"iterations": iterations,
22+
"book_count": book_count,
23+
"mean_us": statistics.mean(samples),
24+
"median_us": statistics.median(samples),
25+
"min_us": min(samples),
26+
"max_us": max(samples),
27+
}
28+
29+
30+
if __name__ == "__main__":
31+
results = run_benchmark()
32+
print("Benchmark: list_books")
33+
print(f"Iterations: {results['iterations']}")
34+
print(f"Book count: {results['book_count']}")
35+
print(f"Mean: {results['mean_us']:.3f} us")
36+
print(f"Median: {results['median_us']:.3f} us")
37+
print(f"Min: {results['min_us']:.3f} us")
38+
print(f"Max: {results['max_us']:.3f} us")

samples/book-app-project/book_app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ def handle_add():
3333
author = input("Author: ").strip()
3434
year_str = input("Year: ").strip()
3535

36+
# Input validation
37+
if not title:
38+
print("\nError: Title cannot be empty.\n")
39+
return
40+
if not author:
41+
print("\nError: Author cannot be empty.\n")
42+
return
3643
try:
3744
year = int(year_str) if year_str else 0
45+
if year < 0 or year > 9999:
46+
raise ValueError("Year must be between 0 and 9999.")
3847
collection.add_book(title, author, year)
3948
print("\nBook added successfully.\n")
4049
except ValueError as e:

samples/book-app-project/tests/test_books.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,31 @@ def test_remove_book_invalid():
6868
collection = BookCollection()
6969
result = collection.remove_book("Nonexistent Book")
7070
assert result is False
71+
72+
def test_handle_add_invalid_input(monkeypatch, capsys):
73+
from book_app import handle_add
74+
75+
# Test empty title
76+
monkeypatch.setattr('builtins.input', lambda _: "")
77+
handle_add()
78+
captured = capsys.readouterr()
79+
assert "Error: Title cannot be empty." in captured.out
80+
81+
# Test empty author
82+
monkeypatch.setattr('builtins.input', lambda prompt: "Title" if "Title" in prompt else "")
83+
handle_add()
84+
captured = capsys.readouterr()
85+
assert "Error: Author cannot be empty." in captured.out
86+
87+
# Test invalid year
88+
monkeypatch.setattr('builtins.input', lambda prompt: "Title" if "Title" in prompt else ("Author" if "Author" in prompt else "-1"))
89+
handle_add()
90+
captured = capsys.readouterr()
91+
assert "Error: Year must be between 0 and 9999." in captured.out
92+
93+
def test_list_books():
94+
collection = BookCollection()
95+
collection.add_book("Test Book", "Author", 2023)
96+
books = collection.list_books()
97+
assert len(books) == 1
98+
assert books[0].title == "Test Book"

0 commit comments

Comments
 (0)