-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
82 lines (62 loc) · 2.34 KB
/
benchmark.py
File metadata and controls
82 lines (62 loc) · 2.34 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
#!/usr/bin/env python3
"""Simple benchmark to demonstrate performance improvements."""
import time
from scopedstats import Recorder, incr, timer
def benchmark_basic_operations():
"""Benchmark basic increment operations."""
recorder = Recorder()
# Test with no tags (fastest path)
start = time.perf_counter()
with recorder.record():
for i in range(10000):
incr("no_tags")
no_tags_time = time.perf_counter() - start
# Test with tags (cached path)
start = time.perf_counter()
with recorder.record():
for i in range(10000):
incr("with_tags", tags={"type": "benchmark", "batch": i // 1000})
with_tags_time = time.perf_counter() - start
print("✨ Performance Benchmark Results:")
print(f" No tags: {no_tags_time:.4f}s ({10000 / no_tags_time:.0f} ops/sec)")
print(f" With tags: {with_tags_time:.4f}s ({10000 / with_tags_time:.0f} ops/sec)")
print(f" Tag overhead: {((with_tags_time / no_tags_time - 1) * 100):.1f}%")
results = recorder.get_result()
print(
f" Final counts: no_tags={results['no_tags']}, with_tags={results['with_tags']}"
)
def benchmark_memory_usage():
"""Show memory efficiency with __slots__."""
import sys
recorder = Recorder()
print("🧠 Memory Usage:")
print(f" Recorder size: {sys.getsizeof(recorder)} bytes")
print(" Uses __slots__ for memory efficiency")
@timer
def sample_timed_function():
"""Sample function to show timing works."""
time.sleep(0.001) # 1ms
return "completed"
def demo_timing():
"""Demonstrate timing functionality."""
recorder = Recorder()
with recorder.record():
for _ in range(5):
sample_timed_function()
results = recorder.get_result()
avg_time = results.get("calls.sample_timed_function.total_dur", 0) / results.get(
"calls.sample_timed_function.count", 1
)
print("⏱️ Timing Demo:")
print(f" Calls: {results.get('calls.sample_timed_function.count', 0)}")
print(
f" Total time: {results.get('calls.sample_timed_function.total_dur', 0):.4f}s"
)
print(f" Average: {avg_time:.4f}s per call")
if __name__ == "__main__":
print("🚀 ScopedStats Performance Benchmark\n")
benchmark_basic_operations()
print()
benchmark_memory_usage()
print()
demo_timing()