-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconcurrent_dict_bench.py
97 lines (78 loc) · 2.85 KB
/
concurrent_dict_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
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# pyre-strict
import os
import uuid
from typing import Optional
from ft_utils.benchmark_utils import BenchmarkProvider, execute_benchmarks, ft_randint
from ft_utils.concurrency import ConcurrentDict
from ft_utils.local import LocalWrapper
class ConcurretDictBenchmarkProvider(BenchmarkProvider):
def __init__(self, operations: int) -> None:
self._operations = operations
self._cdct: ConcurrentDict | None = None
self._dct: dict[int | str, int] | None = None
def set_up(self) -> None:
self._cdct = ConcurrentDict(os.cpu_count())
self._dct = {}
def benchmark_insert(self) -> None:
lw = LocalWrapper(self._cdct)
for _ in range(self._operations):
x = ft_randint(0, 1048576)
lw[x] = str(x)
def benchmark_insert_dict(self) -> None:
lw = LocalWrapper(self._dct)
for _ in range(self._operations):
x = ft_randint(0, 1048576)
lw[x] = str(x)
def benchmark_update(self) -> None:
lw = LocalWrapper(self._cdct)
what = [ft_randint(0, 1024) for _ in range(self._operations // 3)]
prefix = str(uuid.uuid4())
for x in what:
lw[f"{prefix}{x}"] = x
lw[f"{prefix}{x}"]
del lw[f"{prefix}{x}"]
def benchmark_update_dict(self) -> None:
lw = LocalWrapper(self._dct)
what = [ft_randint(0, 1024) for _ in range(self._operations // 3)]
prefix = str(uuid.uuid4())
for x in what:
lw[f"{prefix}{x}"] = x
lw[f"{prefix}{x}"]
del lw[f"{prefix}{x}"]
def benchmark_read(self) -> None:
lw = LocalWrapper(self._cdct)
what = [ft_randint(0, 1024) for _ in range(1024)]
for x in what:
lw[f"{x}"] = x
for x in range(self._operations):
x = what[x % 1024]
lw[f"{x}"]
def benchmark_read_dict(self) -> None:
lw = LocalWrapper(self._dct)
what = [ft_randint(0, 1024) for _ in range(1024)]
for x in what:
lw[f"{x}"] = x
for x in range(self._operations):
x = what[x % 1024]
lw[f"{x}"]
def benchmark_in(self) -> None:
lw = LocalWrapper(self._cdct)
what = [ft_randint(0, 1024) for _ in range(1024)]
for x in what:
lw[f"{x}"] = x
for x in range(self._operations):
x = what[x % 1024]
f"{x}" in lw
def benchmark_in_dict(self) -> None:
lw = LocalWrapper(self._dct)
what = [ft_randint(0, 1024) for _ in range(1024)]
for x in what:
lw[f"{x}"] = x
for x in range(self._operations):
x = what[x % 1024]
f"{x}" in lw
def invoke_main() -> None:
execute_benchmarks(ConcurretDictBenchmarkProvider)
if __name__ == "__main__":
invoke_main()