forked from docarray/docarray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarking.py
More file actions
311 lines (256 loc) · 8.87 KB
/
benchmarking.py
File metadata and controls
311 lines (256 loc) · 8.87 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
import argparse
import functools
import random
from time import perf_counter
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from docarray import Document, DocumentArray
from rich.console import Console
from rich.table import Table
n_index_values = [10_000, 100_000, 1000_000]
n_query = 1
D = 128
TENSOR_SHAPE = (512, 256)
K = 10
n_vector_queries = 1000
np.random.seed(123)
parser = argparse.ArgumentParser()
parser.add_argument(
'--default-hnsw',
help='Whether to use default HNSW configurations',
action='store_true',
)
args = parser.parse_args()
times = {}
def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = perf_counter()
res = func(*args, **kwargs)
return (perf_counter() - start, res)
return wrapper
@timer
def create(da, docs):
da.extend(docs)
@timer
def read(da, ids):
da[ids]
@timer
def update(da, docs):
da[[d.id for d in docs]] = docs
@timer
def delete(da, ids):
del da[ids]
@timer
def find_by_condition(da, query):
da.find(query)
@timer
def find_by_vector(da, query):
return da.find(query, limit=K)
def get_docs(n, n_dim, tensor_shape, n_query):
return [
Document(
embedding=np.random.rand(n_dim),
# tensor=np.random.rand(*tensor_shape),
tags={'i': int(i / n_query)},
)
for i in range(n)
]
def fmt(value, unit):
return '{:.3f} {}'.format(value, unit)
def recall(predicted, relevant, eval_at):
if eval_at == 0:
return 0.0
predicted_at_k = predicted[:eval_at]
n_predicted_and_relevant = len(
set(predicted_at_k[:, 'id']).intersection(set(relevant[:, 'id']))
)
return n_predicted_and_relevant / len(relevant)
if args.default_hnsw:
storage_backends = [
('memory', None),
('sqlite', None),
(
'annlite',
{'n_dim': D},
),
('qdrant', {'n_dim': D, 'scroll_batch_size': 8}),
('weaviate', {'n_dim': D}),
('elasticsearch', {'n_dim': D}),
]
else:
storage_backends = [
('memory', None),
('sqlite', None),
(
'annlite',
{
'n_dim': D,
'ef_construction': 100,
'ef_search': 100,
'max_connection': 16,
},
),
('qdrant', {'n_dim': D, 'scroll_batch_size': 8, 'ef_construct': 100, 'm': 16}),
(
'weaviate',
{'n_dim': D, 'ef': 100, 'ef_construction': 100, 'max_connections': 16},
),
('elasticsearch', {'n_dim': D, 'ef_construction': 100, 'm': 16}),
]
table = Table(
title=f'DocArray Benchmarking n_index={n_index_values[-1]} n_query={n_query} D={D} K={K}'
)
benchmark_df = pd.DataFrame(
{
'Storage Backend': [],
'Indexing time (C)': [],
'Query (R)': [],
'Update (U)': [],
'Delete (D)': [],
'Find by vector': [],
f'Recall at k={K} for vector search': [],
'Find by condition': [],
}
)
for col in benchmark_df.columns:
table.add_column(col)
console = Console()
find_by_vector_values = {str(n_index): [] for n_index in n_index_values}
create_values = {str(n_index): [] for n_index in n_index_values}
for idx, n_index in enumerate(n_index_values):
console.print(f'generating {n_index} docs...')
docs = get_docs(n_index, D, TENSOR_SHAPE, n_query)
docs_to_delete = random.sample(docs, n_query)
docs_to_update = random.sample(docs, n_query)
vector_queries = [np.random.rand(n_query, D) for _ in range(n_vector_queries)]
ground_truth = []
for backend, config in storage_backends:
try:
console.print('Backend:', backend.title())
# for n_i in n_index:
if not config:
da = DocumentArray(storage=backend)
else:
da = DocumentArray(storage=backend, config=config)
console.print(f'indexing {n_index} docs ...')
create_time, _ = create(da, docs)
# for n_q in n_query:
console.print(f'reading {n_query} docs ...')
read_time, _ = read(
da,
random.sample([d.id for d in docs], n_query),
)
console.print(f'updating {n_query} docs ...')
update_time, _ = update(da, docs_to_update)
console.print(f'deleting {n_query} docs ...')
delete_time, _ = delete(da, [d.id for d in docs_to_delete])
console.print(
f'finding {n_query} docs by vector averaged {n_vector_queries} times ...'
)
if backend == 'sqlite':
find_by_vector_time, result = find_by_vector(da, vector_queries[0])
recall_at_k = recall(result, ground_truth[0], K)
else:
recall_at_k_values = []
find_by_vector_times = []
for i, query in enumerate(vector_queries):
find_by_vector_time, results = find_by_vector(da, query)
find_by_vector_times.append(find_by_vector_time)
if backend == 'memory':
ground_truth.append(results)
recall_at_k_values.append(1)
else:
recall_at_k_values.append(recall(results, ground_truth[i], K))
recall_at_k = sum(recall_at_k_values) / len(recall_at_k_values)
find_by_vector_time = sum(find_by_vector_times) / len(
find_by_vector_times
)
console.print(f'finding {n_query} docs by condition ...')
find_by_condition_time, _ = find_by_condition(da, {'tags__i': {'$eq': 0}})
if idx == len(n_index_values) - 1:
table.add_row(
backend.title(),
fmt(create_time, 's'),
fmt(read_time * 1000, 'ms'),
fmt(update_time * 1000, 'ms'),
fmt(delete_time * 1000, 'ms'),
fmt(find_by_vector_time, 's'),
'{:.3f}'.format(recall_at_k),
fmt(find_by_condition_time, 's'),
)
benchmark_df.append(
pd.DataFrame(
[
[
backend.title(),
create_time,
read_time,
update_time,
delete_time,
find_by_vector_time,
recall_at_k,
find_by_condition_time,
]
],
columns=benchmark_df.columns,
)
)
find_by_vector_values[str(n_index)].append(find_by_vector_time)
create_values[str(n_index)].append(create_time)
except Exception as e:
console.print(f'Storage Backend {backend} failed: {e}')
find_df = pd.DataFrame(find_by_vector_values)
find_df.index = [backend for backend, _ in storage_backends]
find_df = find_df.drop(['sqlite'])
print(find_df)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(17, 5))
find_df.plot(
kind="bar",
ax=ax1,
fontsize=16,
color=sns.color_palette('muted')[1:4],
# title='Find by vector per backend and dataset size',
# ylabel='seconds',
rot=0,
)
ax1.set_ylabel('seconds', fontsize=18)
ax1.set_title('Find by vector per backend and dataset size', fontsize=18)
threshold = 0.3
ax1.hlines(y=threshold, xmin=-20, xmax=20, linewidth=2, color='r', linestyle='--')
create_df = pd.DataFrame(create_values)
create_df.index = [backend for backend, _ in storage_backends]
create_df = create_df.drop(['memory'])
print(create_df)
create_df.plot(
kind="bar",
ax=ax2,
fontsize=16,
color=sns.color_palette('muted')[1:4],
# title='Indexing per backend and dataset size',
# ylabel='seconds',
rot=0,
)
ax2.set_ylabel('seconds', fontsize=18)
ax2.set_title('Indexing per backend and dataset size', fontsize=18)
plt.tight_layout()
ax1.legend(fontsize=15)
ax2.legend(fontsize=15)
plt.savefig('benchmark.svg')
console.print(table)
benchmark_df.to_csv('benchmark-seconds.csv')
benchmark_df['Indexing time (C)'] = benchmark_df['Indexing time (C)'].apply(
lambda value: 1_000_000 / value
)
benchmark_df['Query (R)'] = benchmark_df['Query (R)'].apply(lambda value: 1 / value)
benchmark_df['Update (U)'] = benchmark_df['Update (U)'].apply(lambda value: 1 / value)
benchmark_df['Delete (D)'] = benchmark_df['Delete (D)'].apply(lambda value: 1 / value)
benchmark_df['Find by vector'] = benchmark_df['Find by vector'].apply(
lambda value: 1 / value
)
benchmark_df['Find by condition'] = benchmark_df['Find by condition'].apply(
lambda value: 1 / value
)
benchmark_df.to_csv('benchmark-qps.csv')