Repository navigation
Expand file tree
/
Copy pathcollect_benchmark_data_script.py
More file actions
102 lines (91 loc) · 4.21 KB
/
Copy pathcollect_benchmark_data_script.py
File metadata and controls
102 lines (91 loc) · 4.21 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
from __future__ import annotations
import argparse
import json
import asyncio
from typing import List
from pathlib import Path
import rwlocker
from benchmarks.benchmark_base import BenchmarkConfig, BenchmarkDataHandler
from benchmarks.benchmark_scenario import (
BaseScenario, AsyncBaseScenario,
IOBoundScenario, AsyncIOBoundScenario,
PureOverheadScenario, AsyncPureOverheadScenario
)
from benchmarks import (
thread_rwlock_benchmark,
async_rwlock_benchmark,
thread_rwcondition_benchmark,
async_rwcondition_benchmark
)
locks = [
thread_rwlock_benchmark.StandardLockWrapper,
thread_rwlock_benchmark.StandardRLockWrapper,
rwlocker.RWLockWrite,
rwlocker.RWLockWriteReentrantWriter,
rwlocker.RWLockRead,
rwlocker.RWLockReadReentrantWriter,
rwlocker.RWLockReaderPhaseFair,
rwlocker.RWLockReaderPhaseFairReentrantWriter,
rwlocker.RWLockFair,
rwlocker.RWLockFairReentrantWriter
]
conditions = [
thread_rwcondition_benchmark.StandardConditionWrapper,
thread_rwcondition_benchmark.RWConditionWriteWrapper,
thread_rwcondition_benchmark.RWConditionReadWrapper,
thread_rwcondition_benchmark.RWConditionReaderPhaseFairWrapper,
thread_rwcondition_benchmark.RWConditionFairWrapper
]
async_locks = [
async_rwlock_benchmark.AsyncStandardLockWrapper,
rwlocker.AsyncRWLockWrite, rwlocker.AsyncRWLockWriteReentrantWriter,
rwlocker.AsyncRWLockRead, rwlocker.AsyncRWLockReadReentrantWriter,
rwlocker.AsyncRWLockReaderPhaseFair, rwlocker.AsyncRWLockReaderPhaseFairReentrantWriter,
rwlocker.AsyncRWLockFair, rwlocker.AsyncRWLockFairReentrantWriter
]
async_conditions = [
async_rwcondition_benchmark.AsyncStandardConditionWrapper,
async_rwcondition_benchmark.AsyncRWConditionWriteWrapper,
async_rwcondition_benchmark.AsyncRWConditionReadWrapper,
async_rwcondition_benchmark.AsyncRWConditionReaderPhaseFairWrapper,
async_rwcondition_benchmark.AsyncRWConditionFairWrapper
]
class BenchmarkDataCollector:
"""
A modular utility to collect benchmark data silently and export to JSON.
Designed to be called via subprocess or orchestrators for different Python environments.
"""
def __init__(self, filepath: Path, config: BenchmarkConfig | None = None):
self.filepath = filepath
self.data_handler = BenchmarkDataHandler()
self.config = config or BenchmarkConfig.reporting()
def collect(self, sync_scenarios: List[BaseScenario], async_scenarios: List[AsyncBaseScenario]):
print(f"[{self.filepath}] Data collection has begun...")
thread_rwlock_benchmark.benchmark(sync_scenarios, locks, data_handler=self.data_handler, config=self.config)
asyncio.run(async_rwlock_benchmark.benchmark(async_scenarios, async_locks, data_handler=self.data_handler, config=self.config))
thread_rwcondition_benchmark.benchmark(sync_scenarios, conditions, data_handler=self.data_handler, config=self.config)
asyncio.run(async_rwcondition_benchmark.benchmark(async_scenarios, async_conditions, data_handler=self.data_handler, config=self.config))
print(f"[{self.filepath}] Data collection is complete.")
self._save_to_file()
def _save_to_file(self):
with open(self.filepath, 'w', encoding='utf-8') as f:
json.dump(self.data_handler.history, f, indent=4, ensure_ascii=False)
print(f"[{self.filepath}] Data has been successfully saved.")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Collect RWLock benchmark data")
parser.add_argument("output", nargs="?", type=Path, default=Path("figures/test_data.json"))
parser.add_argument("--profile", choices=("io", "overhead"), default="io")
args = parser.parse_args()
target_path = args.output
target_path.parent.mkdir(parents=True, exist_ok=True)
collector = BenchmarkDataCollector(target_path)
if args.profile == "overhead":
sync_scenarios = [PureOverheadScenario(iterations=1_000)]
async_scenarios = [AsyncPureOverheadScenario(iterations=1_000)]
else:
sync_scenarios = [IOBoundScenario()]
async_scenarios = [AsyncIOBoundScenario()]
collector.collect(
sync_scenarios=sync_scenarios,
async_scenarios=async_scenarios,
)