55from datetime import datetime , timedelta
66from time_based_storage import TimeBasedStorage , TimeBasedStorageHeap
77
8+
89def generate_random_events (num_events : int ) -> list :
910 """Generate a list of random events for testing."""
1011 base_time = datetime .now ()
1112 events = []
1213 for i in range (num_events ):
13- # Generate random time within the last 24 hours with microsecond precision
14+ # Generate random time within the last 24 hours with microsecond
15+ # precision
1416 random_hours = random .uniform (0 , 24 )
1517 random_minutes = random .uniform (0 , 60 )
1618 random_seconds = random .uniform (0 , 60 )
@@ -24,149 +26,185 @@ def generate_random_events(num_events: int) -> list:
2426 events .append ((timestamp , i ))
2527 return events
2628
29+
2730def get_memory_usage ():
2831 """Get current memory usage of the process."""
2932 process = psutil .Process (os .getpid ())
3033 return process .memory_info ().rss / 1024 / 1024 # Convert to MB
3134
32- def benchmark_insertion (storage : TimeBasedStorage , heap_storage : TimeBasedStorageHeap , num_events : int ):
35+
36+ def benchmark_insertion (storage : TimeBasedStorage ,
37+ heap_storage : TimeBasedStorageHeap , num_events : int ):
3338 """Benchmark event insertion performance."""
3439 events = generate_random_events (num_events )
35-
40+
3641 # Test TimeBasedStorage
3742 start_time = time .time ()
3843 for timestamp , value in events :
3944 storage .add (timestamp , value )
4045 storage_time = time .time () - start_time
41-
46+
4247 # Test TimeBasedStorageHeap
4348 start_time = time .time ()
4449 for timestamp , value in events :
4550 heap_storage .add (timestamp , value )
4651 heap_time = time .time () - start_time
47-
52+
4853 return storage_time , heap_time
4954
50- def benchmark_range_queries (storage : TimeBasedStorage , heap_storage : TimeBasedStorageHeap , num_queries : int ):
55+
56+ def benchmark_range_queries (storage : TimeBasedStorage ,
57+ heap_storage : TimeBasedStorageHeap , num_queries : int ):
5158 """Benchmark range query performance."""
5259 # Generate random time ranges
5360 ranges = []
5461 for _ in range (num_queries ):
5562 start = datetime .now () - timedelta (hours = random .uniform (0 , 24 ))
5663 end = start + timedelta (hours = random .uniform (1 , 12 ))
5764 ranges .append ((start , end ))
58-
65+
5966 # Test TimeBasedStorage
6067 start_time = time .time ()
6168 for start , end in ranges :
6269 storage .get_range (start , end )
6370 storage_time = time .time () - start_time
64-
71+
6572 # Test TimeBasedStorageHeap
6673 start_time = time .time ()
6774 for start , end in ranges :
6875 heap_storage .get_range (start , end )
6976 heap_time = time .time () - start_time
70-
77+
7178 return storage_time , heap_time
7279
73- def benchmark_duration_queries (storage : TimeBasedStorage , heap_storage : TimeBasedStorageHeap , num_queries : int ):
80+
81+ def benchmark_duration_queries (
82+ storage : TimeBasedStorage , heap_storage : TimeBasedStorageHeap , num_queries : int ):
7483 """Benchmark duration query performance."""
7584 # Generate random durations
76- durations = [3600 * random .uniform (1 , 24 ) for _ in range (num_queries )] # Duration in seconds
77-
85+ durations = [
86+ 3600 *
87+ random .uniform (
88+ 1 ,
89+ 24 ) for _ in range (num_queries )] # Duration in seconds
90+
7891 # Test TimeBasedStorage
7992 start_time = time .time ()
8093 for duration in durations :
8194 storage .get_duration (duration )
8295 storage_time = time .time () - start_time
83-
96+
8497 # Test TimeBasedStorageHeap
8598 start_time = time .time ()
8699 for duration in durations :
87100 heap_storage .get_duration (duration )
88101 heap_time = time .time () - start_time
89-
102+
90103 return storage_time , heap_time
91104
92- def benchmark_earliest_latest (storage : TimeBasedStorage , heap_storage : TimeBasedStorageHeap , num_queries : int ):
105+
106+ def benchmark_earliest_latest (
107+ storage : TimeBasedStorage , heap_storage : TimeBasedStorageHeap , num_queries : int ):
93108 """Benchmark earliest/latest event retrieval performance."""
94109 # Test TimeBasedStorage
95110 start_time = time .time ()
96111 for _ in range (num_queries ):
97112 storage .get_all () # Get all values to simulate latest event access
98113 storage_time = time .time () - start_time
99-
114+
100115 # Test TimeBasedStorageHeap
101116 start_time = time .time ()
102117 for _ in range (num_queries ):
103118 heap_storage .get_all () # Get all values to simulate earliest/latest event access
104119 heap_time = time .time () - start_time
105-
120+
106121 return storage_time , heap_time
107122
123+
108124def benchmark_timestamp_collisions (storage : TimeBasedStorage , num_events : int ):
109125 """Benchmark handling of timestamp collisions."""
110126 base_time = datetime .now ()
111127 collisions = 0
112128 start_time = time .time ()
113-
129+
114130 for i in range (num_events ):
115131 try :
116132 storage .add (base_time , i )
117133 except ValueError :
118134 collisions += 1
119-
135+
120136 end_time = time .time ()
121137 return end_time - start_time , collisions
122138
139+
123140def main ():
124141 """Run all benchmarks with different dataset sizes."""
125142 sizes = [1000 , 10000 , 100000 , 1000000 ]
126143 num_queries = 1000
127-
144+
128145 print ("Starting benchmarks..." )
129146 print ("-" * 80 )
130-
147+
131148 for size in sizes :
132149 print (f"\n Dataset size: { size :,} events" )
133150 print ("-" * 40 )
134-
151+
135152 # Initialize storage systems
136153 storage = TimeBasedStorage [int ]()
137154 heap_storage = TimeBasedStorageHeap [int ]()
138-
155+
139156 # Run benchmarks
140157 print ("Running insertion benchmark..." )
141- storage_insert , heap_insert = benchmark_insertion (storage , heap_storage , size )
142-
158+ storage_insert , heap_insert = benchmark_insertion (
159+ storage , heap_storage , size )
160+
143161 print ("Running range queries benchmark..." )
144- storage_range , heap_range = benchmark_range_queries (storage , heap_storage , num_queries )
145-
162+ storage_range , heap_range = benchmark_range_queries (
163+ storage , heap_storage , num_queries )
164+
146165 print ("Running duration queries benchmark..." )
147- storage_duration , heap_duration = benchmark_duration_queries (storage , heap_storage , num_queries )
148-
166+ storage_duration , heap_duration = benchmark_duration_queries (
167+ storage , heap_storage , num_queries )
168+
149169 print ("Running earliest/latest benchmark..." )
150- storage_earliest , heap_earliest = benchmark_earliest_latest (storage , heap_storage , num_queries )
151-
170+ storage_earliest , heap_earliest = benchmark_earliest_latest (
171+ storage , heap_storage , num_queries )
172+
152173 print ("Running timestamp collision benchmark..." )
153- collision_time , collisions = benchmark_timestamp_collisions (TimeBasedStorage [int ](), size )
154-
174+ collision_time , collisions = benchmark_timestamp_collisions (
175+ TimeBasedStorage [int ](), size )
176+
155177 # Get memory usage
156178 memory_usage = get_memory_usage ()
157-
179+
158180 # Print results
159181 print ("\n Results:" )
160- print (f"Insertion: TimeBasedStorage ({ storage_insert :.4f} s) vs TimeBasedStorageHeap ({ heap_insert :.4f} s)" )
161- print (f"Range Queries: TimeBasedStorage ({ storage_range :.4f} s) vs TimeBasedStorageHeap ({ heap_range :.4f} s)" )
162- print (f"Duration Queries: TimeBasedStorage ({ storage_duration :.4f} s) vs TimeBasedStorageHeap ({ heap_duration :.4f} s)" )
163- print (f"Earliest/Latest: TimeBasedStorage ({ storage_earliest :.4f} s) vs TimeBasedStorageHeap ({ heap_earliest :.4f} s)" )
164- print (f"Timestamp Collisions: { collisions :,} collisions in { collision_time :.4f} s" )
182+ print (
183+ f"Insertion: TimeBasedStorage ({
184+ storage_insert :.4f} s) vs TimeBasedStorageHeap ({
185+ heap_insert :.4f} s)" )
186+ print (
187+ f"Range Queries: TimeBasedStorage ({
188+ storage_range :.4f} s) vs TimeBasedStorageHeap ({
189+ heap_range :.4f} s)" )
190+ print (
191+ f"Duration Queries: TimeBasedStorage ({
192+ storage_duration :.4f} s) vs TimeBasedStorageHeap ({
193+ heap_duration :.4f} s)" )
194+ print (
195+ f"Earliest/Latest: TimeBasedStorage ({
196+ storage_earliest :.4f} s) vs TimeBasedStorageHeap ({
197+ heap_earliest :.4f} s)" )
198+ print (
199+ f"Timestamp Collisions: {
200+ collisions :, } collisions in {
201+ collision_time :.4f} s" )
165202 print (f"Memory Usage: { memory_usage :.2f} MB" )
166-
203+
167204 print ("-" * 40 )
168-
205+
169206 print ("\n Benchmark completed successfully!" )
170207
208+
171209if __name__ == "__main__" :
172- main ()
210+ main ()
0 commit comments