forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathbench_session_cluster_cache.py
More file actions
72 lines (53 loc) · 1.81 KB
/
Copy pathbench_session_cluster_cache.py
File metadata and controls
72 lines (53 loc) · 1.81 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
#!/usr/bin/env python
"""
Benchmark: caching self.session.cluster as a local variable.
Measures the cost of repeated self.session.cluster double-lookups
vs. a single local assignment.
"""
import sys
import time
ITERS = 5_000_000
class FakeCluster:
class control_connection:
_tablets_routing_v1 = True
protocol_version = 5
class metadata:
class _tablets:
@staticmethod
def add_tablet(ks, tbl, tablet):
pass
class FakeSession:
cluster = FakeCluster()
class FakeResponseFuture:
def __init__(self):
self.session = FakeSession()
def bench_double_lookup(rf, n):
"""Simulates 3 accesses to self.session.cluster (tablet routing block)."""
t0 = time.perf_counter_ns()
for _ in range(n):
_ = rf.session.cluster.control_connection
_ = rf.session.cluster.protocol_version
_ = rf.session.cluster.metadata
return (time.perf_counter_ns() - t0) / n
def bench_cached_local(rf, n):
"""Simulates caching session.cluster in a local."""
t0 = time.perf_counter_ns()
for _ in range(n):
cluster = rf.session.cluster
_ = cluster.control_connection
_ = cluster.protocol_version
_ = cluster.metadata
return (time.perf_counter_ns() - t0) / n
def main():
print(f"Python {sys.version}\n")
rf = FakeResponseFuture()
ns_old = bench_double_lookup(rf, ITERS)
ns_new = bench_cached_local(rf, ITERS)
saving = ns_old - ns_new
speedup = ns_old / ns_new if ns_new else float('inf')
print(f"=== self.session.cluster caching ({ITERS:,} iters) ===\n")
print(f" 3x self.session.cluster (old): {ns_old:.1f} ns")
print(f" 1x local + 3x local (new): {ns_new:.1f} ns")
print(f" Saving: {saving:.1f} ns ({speedup:.2f}x)")
if __name__ == "__main__":
main()