-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_fast_api_test.py
More file actions
134 lines (106 loc) · 4.38 KB
/
15_fast_api_test.py
File metadata and controls
134 lines (106 loc) · 4.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
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
#!/usr/bin/env python3
"""Test the high-performance insert_batch_fast API."""
import numpy as np
import sys
import time
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent / "src"))
from sochdb.vector import VectorIndex
def test_fast_api():
print("=" * 80)
print("TESTING HIGH-PERFORMANCE insert_batch_fast API")
print("=" * 80)
np.random.seed(42)
n_vectors = 1000
dimension = 768
# Create vectors with EXACT layout requirements
ids = np.arange(n_vectors, dtype=np.uint64) # Must be uint64
vectors = np.random.randn(n_vectors, dimension).astype(np.float32) # Must be float32
vectors = np.ascontiguousarray(vectors) # Must be C-contiguous
print(f"Testing {n_vectors} vectors of dimension {dimension}")
print(f"IDs dtype: {ids.dtype}, contiguous: {ids.flags['C_CONTIGUOUS']}")
print(f"Vectors dtype: {vectors.dtype}, contiguous: {vectors.flags['C_CONTIGUOUS']}")
print(f"Vectors shape: {vectors.shape}")
# Test with ultra-fast settings
index = VectorIndex(dimension=dimension, ef_construction=25, max_connections=8)
start = time.perf_counter()
result = index.insert_batch_fast(ids, vectors)
insert_time = time.perf_counter() - start
throughput = n_vectors / insert_time
print(f"\nResults:")
print(f" Time: {insert_time:.3f}s")
print(f" Throughput: {throughput:.0f} vec/s")
print(f" Inserted: {result}/{n_vectors}")
print(f" Target: 1500+ vec/s")
if throughput >= 1500:
print(f" ✅ ACHIEVED TARGET! {throughput:.0f} vec/s >= 1500 vec/s")
else:
print(f" ❌ Below target by {1500/throughput:.2f}x")
# Test search to verify correctness
search_results = index.search(vectors[0], k=5)
found_self = search_results and search_results[0][0] == 0
print(f" Self-recall: {found_self}")
return throughput
def compare_apis():
print("\n" + "=" * 80)
print("COMPARING insert_batch vs insert_batch_fast")
print("=" * 80)
np.random.seed(42)
n_vectors = 1000
dimension = 768
# Prepare data once
ids = np.arange(n_vectors, dtype=np.uint64)
vectors = np.random.randn(n_vectors, dimension).astype(np.float32)
vectors = np.ascontiguousarray(vectors)
results = {}
# Test regular API
print("\n1. Regular insert_batch:")
index1 = VectorIndex(dimension=dimension, ef_construction=25, max_connections=8)
start = time.perf_counter()
result1 = index1.insert_batch(list(ids), vectors)
time1 = time.perf_counter() - start
throughput1 = n_vectors / time1
results['regular'] = throughput1
print(f" Throughput: {throughput1:.0f} vec/s")
print(f" Result: {result1}")
# Test fast API
print("\n2. Fast insert_batch_fast:")
index2 = VectorIndex(dimension=dimension, ef_construction=25, max_connections=8)
start = time.perf_counter()
result2 = index2.insert_batch_fast(ids, vectors)
time2 = time.perf_counter() - start
throughput2 = n_vectors / time2
results['fast'] = throughput2
print(f" Throughput: {throughput2:.0f} vec/s")
print(f" Result: {result2}")
speedup = throughput2 / throughput1 if throughput1 > 0 else 0
print(f"\n📊 SPEEDUP: {speedup:.2f}x faster with insert_batch_fast")
return results
if __name__ == '__main__':
try:
# Test the high-performance API
throughput = test_fast_api()
# Compare both APIs
results = compare_apis()
print("\n" + "=" * 80)
print("SUMMARY")
print("=" * 80)
print(f"""
🚀 HIGH-PERFORMANCE API RESULTS:
insert_batch_fast: {results.get('fast', 0):.0f} vec/s
insert_batch: {results.get('regular', 0):.0f} vec/s
🎯 BENCHMARK ISSUE RESOLVED:
The benchmark was using insert_batch_fast (high-perf API)
while our tests used insert_batch (regular API).
This explains the performance gap:
- Benchmark claims: 851 vec/s (using fast API with balanced settings)
- Our profiling: 120 vec/s (using regular API with ef=100)
💡 RECOMMENDATION:
- Use insert_batch_fast for production (proper array layout required)
- Update documentation to recommend the fast API
- Potentially deprecate the slower insert_batch API
""")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()