forked from scylladb/python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_cache_apply_parameters.py
More file actions
78 lines (62 loc) · 2.38 KB
/
Copy pathbench_cache_apply_parameters.py
File metadata and controls
78 lines (62 loc) · 2.38 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
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Micro-benchmark: apply_parameters caching.
Measures the speedup from caching parameterized type creation
in _CassandraType.apply_parameters().
Run:
python benchmarks/bench_cache_apply_parameters.py
"""
import timeit
from cassandra.cqltypes import (
MapType, SetType, ListType, TupleType,
Int32Type, UTF8Type, FloatType, DoubleType, BooleanType,
_CassandraType,
)
def bench_apply_parameters():
"""Benchmark apply_parameters with cache (repeated calls)."""
cache = _CassandraType._apply_parameters_cache
# Warm up the cache
MapType.apply_parameters([UTF8Type, Int32Type])
SetType.apply_parameters([FloatType])
ListType.apply_parameters([DoubleType])
TupleType.apply_parameters([Int32Type, UTF8Type, BooleanType])
calls = [
(MapType, [UTF8Type, Int32Type]),
(SetType, [FloatType]),
(ListType, [DoubleType]),
(TupleType, [Int32Type, UTF8Type, BooleanType]),
]
def run_cached():
for cls, subtypes in calls:
cls.apply_parameters(subtypes)
# Benchmark cached path
n = 100_000
t_cached = timeit.timeit(run_cached, number=n)
print(f"Cached apply_parameters ({len(calls)} types x {n} iters): "
f"{t_cached:.3f}s ({t_cached / (n * len(calls)) * 1e6:.2f} us/call)")
# Benchmark uncached path (clear cache each iteration)
def run_uncached():
for cls, subtypes in calls:
cache.clear()
cls.apply_parameters(subtypes)
t_uncached = timeit.timeit(run_uncached, number=n)
print(f"Uncached apply_parameters ({len(calls)} types x {n} iters): "
f"{t_uncached:.3f}s ({t_uncached / (n * len(calls)) * 1e6:.2f} us/call)")
speedup = t_uncached / t_cached
print(f"Speedup: {speedup:.1f}x")
def main():
bench_apply_parameters()
if __name__ == '__main__':
main()