-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
210 lines (167 loc) · 6.57 KB
/
benchmark.py
File metadata and controls
210 lines (167 loc) · 6.57 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import time
import random
import psutil
import os
from datetime import datetime, timedelta
from time_based_storage import TimeBasedStorage, TimeBasedStorageHeap
def generate_random_events(num_events: int) -> list:
"""Generate a list of random events for testing."""
base_time = datetime.now()
events = []
for i in range(num_events):
# Generate random time within the last 24 hours with microsecond
# precision
random_hours = random.uniform(0, 24)
random_minutes = random.uniform(0, 60)
random_seconds = random.uniform(0, 60)
random_microseconds = random.randint(0, 1000000)
timestamp = base_time - timedelta(
hours=random_hours,
minutes=random_minutes,
seconds=random_seconds,
microseconds=random_microseconds
)
events.append((timestamp, i))
return events
def get_memory_usage():
"""Get current memory usage of the process."""
process = psutil.Process(os.getpid())
return process.memory_info().rss / 1024 / 1024 # Convert to MB
def benchmark_insertion(storage: TimeBasedStorage,
heap_storage: TimeBasedStorageHeap, num_events: int):
"""Benchmark event insertion performance."""
events = generate_random_events(num_events)
# Test TimeBasedStorage
start_time = time.time()
for timestamp, value in events:
storage.add(timestamp, value)
storage_time = time.time() - start_time
# Test TimeBasedStorageHeap
start_time = time.time()
for timestamp, value in events:
heap_storage.add(timestamp, value)
heap_time = time.time() - start_time
return storage_time, heap_time
def benchmark_range_queries(storage: TimeBasedStorage,
heap_storage: TimeBasedStorageHeap, num_queries: int):
"""Benchmark range query performance."""
# Generate random time ranges
ranges = []
for _ in range(num_queries):
start = datetime.now() - timedelta(hours=random.uniform(0, 24))
end = start + timedelta(hours=random.uniform(1, 12))
ranges.append((start, end))
# Test TimeBasedStorage
start_time = time.time()
for start, end in ranges:
storage.get_range(start, end)
storage_time = time.time() - start_time
# Test TimeBasedStorageHeap
start_time = time.time()
for start, end in ranges:
heap_storage.get_range(start, end)
heap_time = time.time() - start_time
return storage_time, heap_time
def benchmark_duration_queries(
storage: TimeBasedStorage, heap_storage: TimeBasedStorageHeap, num_queries: int):
"""Benchmark duration query performance."""
# Generate random durations
durations = [
3600 *
random.uniform(
1,
24) for _ in range(num_queries)] # Duration in seconds
# Test TimeBasedStorage
start_time = time.time()
for duration in durations:
storage.get_duration(duration)
storage_time = time.time() - start_time
# Test TimeBasedStorageHeap
start_time = time.time()
for duration in durations:
heap_storage.get_duration(duration)
heap_time = time.time() - start_time
return storage_time, heap_time
def benchmark_earliest_latest(
storage: TimeBasedStorage, heap_storage: TimeBasedStorageHeap, num_queries: int):
"""Benchmark earliest/latest event retrieval performance."""
# Test TimeBasedStorage
start_time = time.time()
for _ in range(num_queries):
storage.get_all() # Get all values to simulate latest event access
storage_time = time.time() - start_time
# Test TimeBasedStorageHeap
start_time = time.time()
for _ in range(num_queries):
heap_storage.get_all() # Get all values to simulate earliest/latest event access
heap_time = time.time() - start_time
return storage_time, heap_time
def benchmark_timestamp_collisions(storage: TimeBasedStorage, num_events: int):
"""Benchmark handling of timestamp collisions."""
base_time = datetime.now()
collisions = 0
start_time = time.time()
for i in range(num_events):
try:
storage.add(base_time, i)
except ValueError:
collisions += 1
end_time = time.time()
return end_time - start_time, collisions
def main():
"""Run all benchmarks with different dataset sizes."""
sizes = [1000, 10000, 100000, 1000000]
num_queries = 1000
print("Starting benchmarks...")
print("-" * 80)
for size in sizes:
print(f"\nDataset size: {size:,} events")
print("-" * 40)
# Initialize storage systems
storage = TimeBasedStorage[int]()
heap_storage = TimeBasedStorageHeap[int]()
# Run benchmarks
print("Running insertion benchmark...")
storage_insert, heap_insert = benchmark_insertion(
storage, heap_storage, size)
print("Running range queries benchmark...")
storage_range, heap_range = benchmark_range_queries(
storage, heap_storage, num_queries)
print("Running duration queries benchmark...")
storage_duration, heap_duration = benchmark_duration_queries(
storage, heap_storage, num_queries)
print("Running earliest/latest benchmark...")
storage_earliest, heap_earliest = benchmark_earliest_latest(
storage, heap_storage, num_queries)
print("Running timestamp collision benchmark...")
collision_time, collisions = benchmark_timestamp_collisions(
TimeBasedStorage[int](), size)
# Get memory usage
memory_usage = get_memory_usage()
# Print results
print("\nResults:")
print(
f"Insertion: TimeBasedStorage ({
storage_insert:.4f}s) vs TimeBasedStorageHeap ({
heap_insert:.4f}s)")
print(
f"Range Queries: TimeBasedStorage ({
storage_range:.4f}s) vs TimeBasedStorageHeap ({
heap_range:.4f}s)")
print(
f"Duration Queries: TimeBasedStorage ({
storage_duration:.4f}s) vs TimeBasedStorageHeap ({
heap_duration:.4f}s)")
print(
f"Earliest/Latest: TimeBasedStorage ({
storage_earliest:.4f}s) vs TimeBasedStorageHeap ({
heap_earliest:.4f}s)")
print(
f"Timestamp Collisions: {
collisions:, } collisions in {
collision_time:.4f}s")
print(f"Memory Usage: {memory_usage:.2f}MB")
print("-" * 40)
print("\nBenchmark completed successfully!")
if __name__ == "__main__":
main()