-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrandom_bench.py
58 lines (44 loc) · 1.74 KB
/
random_bench.py
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# pyre-strict
import random
import threading
from collections.abc import Callable
from ft_utils.benchmark_utils import BenchmarkProvider, execute_benchmarks
from ft_utils.local import BatchExecutor, LocalWrapper
from ft_utils.synchronization import IntervalLock, RWLock, RWWriteContext
class RandomBenchmarkProvider(BenchmarkProvider):
def __init__(self, operations: int) -> None:
self._operations = operations
self._ilock = IntervalLock()
self._lock = threading.Lock()
self._rwlock = RWLock()
self._batch_executor = BatchExecutor(lambda: random.randint(1, 100), 10000)
def benchmark_random_direct(self) -> None:
rr = LocalWrapper(random.randint)
for _ in range(self._operations):
_ = rr(1, 100)
def benchmark_interval_locked(self) -> None:
rr = LocalWrapper(random.randint)
poll = LocalWrapper(self._ilock.poll)
with self._ilock:
for _ in range(self._operations):
_ = rr(1, 100)
poll()
def benchmark_batch_executor(self) -> None:
be = LocalWrapper(self._batch_executor.load)
for _ in range(self._operations):
_ = be()
def benchmark_simple_locked(self) -> None:
rr = LocalWrapper(random.randint)
with self._lock:
for _ in range(self._operations):
_ = rr(1, 100)
def benchmark_rw_locked(self) -> None:
rr = LocalWrapper(random.randint)
with RWWriteContext(self._rwlock):
for _ in range(self._operations):
_ = rr(1, 100)
def invoke_main() -> None:
execute_benchmarks(RandomBenchmarkProvider)
if __name__ == "__main__":
invoke_main()