forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathbench_col_names_cache.py
More file actions
66 lines (51 loc) · 2.15 KB
/
Copy pathbench_col_names_cache.py
File metadata and controls
66 lines (51 loc) · 2.15 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
# Copyright ScyllaDB, 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: column_names / column_types extraction from metadata.
Measures the cost of building [c[2] for c in metadata] and [c[3] for c in metadata]
vs using pre-cached lists (as done for prepared statements with result_metadata).
Run:
python benchmarks/bench_col_names_cache.py
"""
import sys
import timeit
def make_column_metadata(ncols):
"""Create fake column_metadata tuples like recv_results_metadata produces."""
class FakeType:
pass
return [(f"ks_{i}", f"tbl_{i}", f"col_{i}", FakeType) for i in range(ncols)]
def bench():
for ncols in (5, 10, 20, 50):
metadata = make_column_metadata(ncols)
# Pre-cached (done once at prepare time)
cached_names = [c[2] for c in metadata]
cached_types = [c[3] for c in metadata]
def extract_uncached():
names = [c[2] for c in metadata]
types = [c[3] for c in metadata]
return names, types
def extract_cached():
return cached_names, cached_types
n = 500_000
t_uncached = timeit.timeit(extract_uncached, number=n)
t_cached = timeit.timeit(extract_cached, number=n)
saving_ns = (t_uncached - t_cached) / n * 1e9
speedup = t_uncached / t_cached if t_cached > 0 else float('inf')
print(f" {ncols} cols: uncached={t_uncached / n * 1e9:.1f} ns, "
f"cached={t_cached / n * 1e9:.1f} ns, "
f"saving={saving_ns:.1f} ns ({speedup:.1f}x)")
if __name__ == "__main__":
print(f"Python {sys.version}")
print("\n=== column_names / column_types extraction ===")
bench()