-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
359 lines (328 loc) · 14.2 KB
/
Copy pathbenchmark.py
File metadata and controls
359 lines (328 loc) · 14.2 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
"""
Example script that demonstrates running the same code across different Python versions.
This script can be executed with any of the three Python versions to show performance differences.
Tests all 5 different data sizes for each test with 5 repetitions.
Results are saved to CSV format for analysis.
"""
import sys
import platform
import csv
from datetime import datetime
from pathlib import Path
# Import benchmark functions from the tests package
from tests import (
run_fibonacci_benchmark,
run_bubble_sort_benchmark,
run_list_comprehension_benchmark,
run_function_call_benchmark,
run_exception_handling_benchmark,
run_object_instantiation_benchmark,
run_attribute_access_benchmark,
run_multithread_cpu_benchmark,
run_multithread_io_benchmark,
run_concurrent_futures_cpu_benchmark,
run_concurrent_futures_io_benchmark
)
# Test size arrays - 5 different sizes for each test type
FIBONACCI_SIZES = [20, 25, 30, 32, 35]
BUBBLE_SORT_SIZES = [1000, 2000, 3000, 4000, 5000]
LIST_COMPREHENSION_SIZES = [1000, 10000, 100000, 1000000, 10000000]
FUNCTION_CALL_SIZES = [1000, 10000, 100000, 1000000, 10000000]
EXCEPTION_SIZES = [1000, 10000, 100000, 1000000, 10000000]
OBJECT_COUNT_SIZES = [1000, 10000, 100000, 1000000, 5000000]
MULTITHREAD_CPU_SIZES = [2, 4, 8, 16, 32] # Number of threads
MULTITHREAD_IO_SIZES = [2, 4, 8, 16, 32] # Number of threads
MULTITHREAD_CPU_ITERATIONS = [50000, 100000, 200000, 500000, 1000000] # Iterations per thread
MULTITHREAD_IO_DURATIONS = [0.005, 0.01, 0.02, 0.05, 0.1] # Duration per task in seconds
# Size level names for display
SIZE_LEVELS = ["Small", "Medium", "Large", "XLarge", "XXLarge"]
def save_results_to_csv(results: list, filename: str) -> None:
"""
Save benchmark results to CSV file.
Args:
results: List of result dictionaries
filename: Output CSV filename
"""
if not results:
return
# Create results directory if it doesn't exist
results_dir = Path("results")
results_dir.mkdir(exist_ok=True)
csv_path = results_dir / filename
with open(csv_path, 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = [
'test_name', 'test_type', 'size_level', 'size_value', 'python_version',
'platform', 'architecture', 'repeats', 'mean_time', 'min_time', 'max_time',
'std_dev', 'median_time', 'timestamp'
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for result in results:
writer.writerow(result)
print(f"Results saved to: {csv_path}")
def run_benchmarks(repeats: int = 5) -> None:
"""
Run all benchmarks with 5 different data sizes and save results to CSV.
Args:
repeats: Number of times to repeat each test (default: 5)
"""
print("=" * 60)
print("Python Performance Test - Running Benchmarks")
print("=" * 60)
print(f"Python Version: {sys.version}")
print(f"Platform: {platform.platform()}")
print(f"Architecture: {platform.architecture()}")
print(f"Repeats per test: {repeats}")
print("=" * 60)
# Collect all results
all_results = []
timestamp = datetime.now().isoformat()
# Test 1: Fibonacci - test all 5 sizes
print("\nRunning Fibonacci tests...")
for i, size in enumerate(FIBONACCI_SIZES):
fibonacci_results = run_fibonacci_benchmark(size, repeats)
stats = fibonacci_results['statistics']
all_results.append({
'test_name': fibonacci_results['name'],
'test_type': 'Fibonacci',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 2: Bubble Sort - test all 5 sizes
print("Running Bubble Sort tests...")
for i, size in enumerate(BUBBLE_SORT_SIZES):
bubble_sort_results = run_bubble_sort_benchmark(size, repeats)
stats = bubble_sort_results['statistics']
all_results.append({
'test_name': bubble_sort_results['name'],
'test_type': 'Bubble Sort',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 3: List Comprehension - test all 5 sizes
print("Running List Comprehension tests...")
for i, size in enumerate(LIST_COMPREHENSION_SIZES):
list_comp_results = run_list_comprehension_benchmark(size, repeats)
stats = list_comp_results['statistics']
all_results.append({
'test_name': list_comp_results['name'],
'test_type': 'List Comprehension',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 4: Function Call Overhead - test all 5 sizes
print("Running Function Call tests...")
for i, size in enumerate(FUNCTION_CALL_SIZES):
function_call_results = run_function_call_benchmark(size, repeats)
stats = function_call_results['statistics']
all_results.append({
'test_name': function_call_results['name'],
'test_type': 'Function Call',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 5: Exception Handling - test all 5 sizes
print("Running Exception Handling tests...")
for i, size in enumerate(EXCEPTION_SIZES):
exception_results = run_exception_handling_benchmark(size, repeats)
stats = exception_results['statistics']
all_results.append({
'test_name': exception_results['name'],
'test_type': 'Exception Handling',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 6: Object Instantiation - test all 5 sizes
print("Running Object Instantiation tests...")
for i, size in enumerate(OBJECT_COUNT_SIZES):
object_inst_results = run_object_instantiation_benchmark(size, repeats)
stats = object_inst_results['statistics']
all_results.append({
'test_name': object_inst_results['name'],
'test_type': 'Object Instantiation',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 7: Attribute Access - test all 5 sizes
print("Running Attribute Access tests...")
for i, size in enumerate(OBJECT_COUNT_SIZES):
# Create objects for this size
object_inst_results = run_object_instantiation_benchmark(size, repeats)
# Test attribute access on these objects
attr_access_results = run_attribute_access_benchmark(object_inst_results['result'], repeats)
stats = attr_access_results['statistics']
all_results.append({
'test_name': attr_access_results['name'],
'test_type': 'Attribute Access',
'size_level': SIZE_LEVELS[i],
'size_value': size,
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 8: Multi-thread CPU - test all 5 thread counts
print("Running Multi-thread CPU tests...")
for i, thread_count in enumerate(MULTITHREAD_CPU_SIZES):
iterations = MULTITHREAD_CPU_ITERATIONS[i]
multithread_cpu_results = run_multithread_cpu_benchmark(thread_count, iterations, repeats)
stats = multithread_cpu_results['statistics']
all_results.append({
'test_name': multithread_cpu_results['name'],
'test_type': 'Multi-thread CPU',
'size_level': SIZE_LEVELS[i],
'size_value': f"{thread_count} threads, {iterations:,} iter/thread",
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 9: Multi-thread I/O - test all 5 thread counts
print("Running Multi-thread I/O tests...")
for i, thread_count in enumerate(MULTITHREAD_IO_SIZES):
duration = MULTITHREAD_IO_DURATIONS[i]
multithread_io_results = run_multithread_io_benchmark(thread_count, duration, repeats)
stats = multithread_io_results['statistics']
all_results.append({
'test_name': multithread_io_results['name'],
'test_type': 'Multi-thread I/O',
'size_level': SIZE_LEVELS[i],
'size_value': f"{thread_count} threads, {duration}s/task",
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 10: Concurrent Futures CPU - test all 5 thread counts
print("Running Concurrent Futures CPU tests...")
for i, thread_count in enumerate(MULTITHREAD_CPU_SIZES):
iterations = MULTITHREAD_CPU_ITERATIONS[i]
concurrent_cpu_results = run_concurrent_futures_cpu_benchmark(thread_count, iterations, repeats)
stats = concurrent_cpu_results['statistics']
all_results.append({
'test_name': concurrent_cpu_results['name'],
'test_type': 'Concurrent Futures CPU',
'size_level': SIZE_LEVELS[i],
'size_value': f"{thread_count} threads, {iterations:,} iter/thread",
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Test 11: Concurrent Futures I/O - test all 5 thread counts
print("Running Concurrent Futures I/O tests...")
for i, thread_count in enumerate(MULTITHREAD_IO_SIZES):
duration = MULTITHREAD_IO_DURATIONS[i]
concurrent_io_results = run_concurrent_futures_io_benchmark(thread_count, duration, repeats)
stats = concurrent_io_results['statistics']
all_results.append({
'test_name': concurrent_io_results['name'],
'test_type': 'Concurrent Futures I/O',
'size_level': SIZE_LEVELS[i],
'size_value': f"{thread_count} threads, {duration}s/task",
'python_version': sys.version.split()[0],
'platform': platform.platform(),
'architecture': platform.architecture()[0],
'repeats': repeats,
'mean_time': stats['mean'],
'min_time': stats['min'],
'max_time': stats['max'],
'std_dev': stats['std_dev'],
'median_time': stats['median'],
'timestamp': timestamp
})
# Save results to CSV
python_version = sys.version.split()[0].replace('.', '_')
filename = f"benchmark_results_{python_version}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
save_results_to_csv(all_results, filename)
print("\n" + "=" * 60)
print("Benchmark completed!")
print(f"Total tests run: {len(all_results)}")
print("=" * 60)
if __name__ == "__main__":
run_benchmarks()