forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathvector_deserialize.py
More file actions
366 lines (303 loc) · 11.8 KB
/
Copy pathvector_deserialize.py
File metadata and controls
366 lines (303 loc) · 11.8 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
# 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.
"""
Benchmark for VectorType deserialization performance.
Tests different optimization strategies:
1. Current implementation (Python with struct.unpack/numpy)
2. Python struct.unpack only
3. Numpy frombuffer + tolist()
4. Cython DesVectorType deserializer
Run with: python benchmarks/vector_deserialize.py
"""
import os
import sys
import time
import struct
# Add project root to path so the benchmark can be run from any directory
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
from cassandra.cqltypes import FloatType, DoubleType, Int32Type, LongType, ShortType
from cassandra.marshal import (
float_pack,
double_pack,
int32_pack,
int64_pack,
int16_pack,
)
def create_test_data(vector_size, element_type):
"""Create serialized test data for a vector."""
if element_type == FloatType:
values = [float(i * 0.1) for i in range(vector_size)]
pack_fn = float_pack
elif element_type == DoubleType:
values = [float(i * 0.1) for i in range(vector_size)]
pack_fn = double_pack
elif element_type == Int32Type:
values = list(range(vector_size))
pack_fn = int32_pack
elif element_type == LongType:
values = list(range(vector_size))
pack_fn = int64_pack
elif element_type == ShortType:
values = [i % 32767 for i in range(vector_size)]
pack_fn = int16_pack
else:
raise ValueError(f"Unsupported element type: {element_type}")
# Serialize the vector
serialized = b"".join(pack_fn(v) for v in values)
return serialized, values
def benchmark_current_implementation(vector_type, serialized_data, iterations=10000):
"""Benchmark the current VectorType.deserialize implementation."""
protocol_version = 4
start = time.perf_counter()
for _ in range(iterations):
result = vector_type.deserialize(serialized_data, protocol_version)
end = time.perf_counter()
elapsed = end - start
per_op = (elapsed / iterations) * 1_000_000 # microseconds
return elapsed, per_op, result
def benchmark_struct_optimization(vector_type, serialized_data, iterations=10000):
"""Benchmark struct.unpack optimization."""
vector_size = vector_type.vector_size
subtype = vector_type.subtype
# Determine format string - subtype is a class, use identity or issubclass
if subtype is FloatType or (
isinstance(subtype, type) and issubclass(subtype, FloatType)
):
format_str = f">{vector_size}f"
elif subtype is DoubleType or (
isinstance(subtype, type) and issubclass(subtype, DoubleType)
):
format_str = f">{vector_size}d"
elif subtype is Int32Type or (
isinstance(subtype, type) and issubclass(subtype, Int32Type)
):
format_str = f">{vector_size}i"
elif subtype is LongType or (
isinstance(subtype, type) and issubclass(subtype, LongType)
):
format_str = f">{vector_size}q"
elif subtype is ShortType or (
isinstance(subtype, type) and issubclass(subtype, ShortType)
):
format_str = f">{vector_size}h"
else:
return None, None, None
start = time.perf_counter()
for _ in range(iterations):
result = list(struct.unpack(format_str, serialized_data))
end = time.perf_counter()
elapsed = end - start
per_op = (elapsed / iterations) * 1_000_000 # microseconds
return elapsed, per_op, result
def benchmark_numpy_optimization(vector_type, serialized_data, iterations=10000):
"""Benchmark numpy.frombuffer optimization."""
try:
import numpy as np
except ImportError:
return None, None, None
vector_size = vector_type.vector_size
subtype = vector_type.subtype
# Determine dtype
if subtype is FloatType or (
isinstance(subtype, type) and issubclass(subtype, FloatType)
):
dtype = ">f4"
elif subtype is DoubleType or (
isinstance(subtype, type) and issubclass(subtype, DoubleType)
):
dtype = ">f8"
elif subtype is Int32Type or (
isinstance(subtype, type) and issubclass(subtype, Int32Type)
):
dtype = ">i4"
elif subtype is LongType or (
isinstance(subtype, type) and issubclass(subtype, LongType)
):
dtype = ">i8"
elif subtype is ShortType or (
isinstance(subtype, type) and issubclass(subtype, ShortType)
):
dtype = ">i2"
else:
return None, None, None
start = time.perf_counter()
for _ in range(iterations):
arr = np.frombuffer(serialized_data, dtype=dtype, count=vector_size)
result = arr.tolist()
end = time.perf_counter()
elapsed = end - start
per_op = (elapsed / iterations) * 1_000_000 # microseconds
return elapsed, per_op, result
def benchmark_cython_deserializer(vector_type, serialized_data, iterations=10000):
"""Benchmark Cython DesVectorType deserializer.
This benchmark requires the Cython deserializers extension to be compiled.
When the extension is not available, or the type does not have a dedicated
DesVectorType deserializer, the benchmark is silently skipped (returns None).
"""
try:
from cassandra.deserializers import find_deserializer
except ImportError:
return None, None, None
protocol_version = 4
# Get the Cython deserializer
deserializer = find_deserializer(vector_type)
# Check if we got the Cython deserializer
if deserializer.__class__.__name__ != "DesVectorType":
return None, None, None
start = time.perf_counter()
for _ in range(iterations):
result = deserializer.deserialize_bytes(serialized_data, protocol_version)
end = time.perf_counter()
elapsed = end - start
per_op = (elapsed / iterations) * 1_000_000 # microseconds
return elapsed, per_op, result
def verify_results(expected, *results):
"""Verify that all results match expected values."""
for i, result in enumerate(results):
if result is None:
continue
if len(result) != len(expected):
print(f" ❌ Result {i} length mismatch: {len(result)} vs {len(expected)}")
return False
for j, (a, b) in enumerate(zip(result, expected)):
# Use relative tolerance for floating point comparison
if isinstance(a, float) and isinstance(b, float):
# Allow 0.01% relative error for floats
if abs(a - b) > max(abs(a), abs(b)) * 1e-4 + 1e-7:
print(f" ❌ Result {i} value mismatch at index {j}: {a} vs {b}")
return False
elif abs(a - b) > 1e-9:
print(f" ❌ Result {i} value mismatch at index {j}: {a} vs {b}")
return False
return True
def run_benchmark_suite(vector_size, element_type, type_name, iterations=10000):
"""Run complete benchmark suite for a given vector configuration."""
print(f"\n{'=' * 80}")
print(f"Benchmark: Vector<{type_name}, {vector_size}>")
print(f"{'=' * 80}")
print(f"Iterations: {iterations:,}")
# Create test data
from cassandra.cqltypes import lookup_casstype
cass_typename = f"org.apache.cassandra.db.marshal.{element_type.__name__}"
vector_typename = (
f"org.apache.cassandra.db.marshal.VectorType({cass_typename}, {vector_size})"
)
vector_type = lookup_casstype(vector_typename)
serialized_data, expected_values = create_test_data(vector_size, element_type)
data_size = len(serialized_data)
print(f"Serialized size: {data_size:,} bytes")
print()
# Run benchmarks
results = []
# 1. Current implementation (baseline)
print("1. Current implementation (baseline)...")
elapsed, per_op, result_current = benchmark_current_implementation(
vector_type, serialized_data, iterations
)
results.append(result_current)
print(f" Total: {elapsed:.4f}s, Per-op: {per_op:.2f} μs")
baseline_time = per_op
# 2. Struct optimization
print("2. Python struct.unpack optimization...")
elapsed, per_op, result_struct = benchmark_struct_optimization(
vector_type, serialized_data, iterations
)
results.append(result_struct)
if per_op is not None:
speedup = baseline_time / per_op
print(
f" Total: {elapsed:.4f}s, Per-op: {per_op:.2f} μs, Speedup: {speedup:.2f}x"
)
else:
print(" Not applicable for this type")
# 3. Numpy with tolist()
print("3. Numpy frombuffer + tolist()...")
elapsed, per_op, result_numpy = benchmark_numpy_optimization(
vector_type, serialized_data, iterations
)
results.append(result_numpy)
if per_op is not None:
speedup = baseline_time / per_op
print(
f" Total: {elapsed:.4f}s, Per-op: {per_op:.2f} μs, Speedup: {speedup:.2f}x"
)
else:
print(" Numpy not available")
# 4. Cython deserializer
print("4. Cython DesVectorType deserializer...")
elapsed, per_op, result_cython = benchmark_cython_deserializer(
vector_type, serialized_data, iterations
)
if per_op is not None:
results.append(result_cython)
speedup = baseline_time / per_op
print(
f" Total: {elapsed:.4f}s, Per-op: {per_op:.2f} μs, Speedup: {speedup:.2f}x"
)
else:
print(" Cython deserializers not available")
# Verify results
print("\nVerifying results...")
if verify_results(expected_values, *results):
print(" ✓ All results match!")
else:
print(" ✗ Result mismatch detected!")
return baseline_time
def main():
"""Run all benchmarks."""
# Pin to single CPU core for consistent measurements
try:
import os
os.sched_setaffinity(0, {0}) # Pin to CPU core 0
print("Pinned to CPU core 0 for consistent measurements")
except (AttributeError, OSError) as e:
print(f"Could not pin to single core: {e}")
print("Running without CPU affinity...")
print("=" * 80)
print("VectorType Deserialization Performance Benchmark")
print("=" * 80)
# Test configurations: (vector_size, element_type, type_name, iterations)
test_configs = [
# Small vectors
(3, FloatType, "float", 50000),
(4, FloatType, "float", 50000),
# Medium vectors (common in ML)
(128, FloatType, "float", 10000),
(384, FloatType, "float", 10000),
# Large vectors (embeddings)
(768, FloatType, "float", 5000),
(1536, FloatType, "float", 2000),
# Other types (smaller iteration counts)
(128, DoubleType, "double", 10000),
(768, DoubleType, "double", 5000),
(1536, DoubleType, "double", 2000),
(64, Int32Type, "int", 15000),
(128, Int32Type, "int", 10000),
]
summary = []
for vector_size, element_type, type_name, iterations in test_configs:
baseline = run_benchmark_suite(vector_size, element_type, type_name, iterations)
summary.append((f"Vector<{type_name}, {vector_size}>", baseline))
# Print summary
print("\n" + "=" * 80)
print("SUMMARY - Current Implementation Performance")
print("=" * 80)
for config, baseline_time in summary:
print(f"{config:30s}: {baseline_time:8.2f} μs")
print("\n" + "=" * 80)
print("Benchmark complete!")
print("=" * 80)
if __name__ == "__main__":
main()