forked from louiezzang/faiss-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_sample.py
205 lines (157 loc) · 6.7 KB
/
client_sample.py
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
import click
import grpc
import numpy as np
import faiss_pb2 as pb2
import faiss_pb2_grpc as pb2_grpc
@click.group()
def cli():
pass
@click.command()
@click.option('--dim', type=int, help='dimension')
@click.option('--host', default='localhost', help='server host')
@click.option('--port', default=50051, help='server port')
def test(host, port, dim):
print("host: %s:%d" % (host, port))
channel = grpc.insecure_channel("%s:%d" % (host, port))
stub = pb2_grpc.ServerStub(channel)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
embedding = list(np.random.random(dim).astype('float32'))
print(embedding)
id = 1
response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
print("response: %s" % response.message)
embedding = list(np.random.random(dim).astype('float32'))
id = 1
response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
print("response: %s" % response.message)
embedding = list(np.random.random(dim).astype('float32'))
id = 2
response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
print("response: %s" % response.message)
embedding = list(np.random.random(dim).astype('float32'))
id = 3
response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
print("response: %s" % response.message)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
id = 2
response = stub.Search(pb2.SearchRequest(id=id, count=5))
print("response: %s, %s" % (response.ids, response.scores))
response = stub.Remove(pb2.IdRequest(id=2))
print("response: %s" % response.message)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
id = 2
response = stub.Search(pb2.SearchRequest(id=id, count=5))
print("response: %s, %s" % (response.ids, response.scores))
response = stub.Remove(pb2.IdRequest(id=1))
response = stub.Remove(pb2.IdRequest(id=3))
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
@click.command("test_key")
@click.option('--dim', type=int, help='dimension')
@click.option('--host', default='localhost', help='server host')
@click.option('--port', default=50051, help='server port')
def test_key(host, port, dim):
print("host: %s:%d" % (host, port))
channel = grpc.insecure_channel("%s:%d" % (host, port))
stub = pb2_grpc.ServerStub(channel)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
embedding = list(np.random.random(dim).astype('float32'))
print(embedding)
key = "k1"
response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
print("response: %s" % response.message)
embedding = list(np.random.random(dim).astype('float32'))
key = "k1"
response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
print("response: %s" % response.message)
embedding = list(np.random.random(dim).astype('float32'))
key = "k2"
response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
print("response: %s" % response.message)
embedding = list(np.random.random(dim).astype('float32'))
key = "k3"
response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
print("response: %s" % response.message)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
key = "k1"
response = stub.Search(pb2.SearchRequest(key=key, count=5))
print("response: %s, %s" % (response.ids, response.scores))
key = "k2"
response = stub.Search(pb2.SearchRequest(key=key, count=5))
print("response: %s, %s" % (response.ids, response.scores))
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
@click.command('import')
@click.argument('embs-path')
@click.argument('ids-path')
@click.argument('keys_path')
@click.option('-h', '--host', default='localhost:50051', help='server host:port')
def imports(host, embs_path, ids_path, keys_path):
print("host: %s" % host)
channel = grpc.insecure_channel(host)
stub = pb2_grpc.ServerStub(channel)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
response = stub.Import(pb2.ImportRequest(embs_path=embs_path, ids_path=ids_path, keys_path=keys_path))
print("response: %s" % response.message)
response = stub.Total(pb2.EmptyRequest())
print("total: %d" % response.count)
@click.command()
@click.argument('id', type=int)
@click.option('-h', '--host', default='localhost:50051', help='server host:port')
@click.option('--count', default=10, help='server limit count')
@click.option('-t', '--timeout', default=0.5, help='request timeout')
def search(host, id, count, timeout):
with grpc.insecure_channel(host) as channel:
stub = pb2_grpc.ServerStub(channel)
response = stub.Search(pb2.SearchRequest(id=id, count=count), timeout=timeout)
print("response: %s, %s" % (response.ids, response.scores))
@click.command()
@click.argument('key', type=str)
@click.option('-h', '--host', default='localhost:50051', help='server host:port')
@click.option('--count', default=10, help='server limit count')
@click.option('-t', '--timeout', default=0.1, help='request timeout')
def search_by_key(host, key, count, timeout):
print("host: %s" % host)
with grpc.insecure_channel(host) as channel:
response = _search_by_key(host, key, count, timeout, channel)
print("response: %s, %s" % (response.keys, response.scores))
def _search_by_key(host, key, count, timeout, channel):
stub = pb2_grpc.ServerStub(channel)
return stub.Search(pb2.SearchRequest(key=key, count=count))
@click.command()
@click.argument('keys-path', type=str)
@click.option('-h', '--host', default='localhost:50051', help='server host:port')
@click.option('--count', default=10, help='server limit count')
@click.option('-t', '--timeout', default=0.1, help='request timeout')
def test_search_perform(host, keys_path, count, timeout):
print("host: %s" % host)
from time import time
import pandas as pd
from gevent.pool import Pool
p = Pool(100)
keys = pd.read_csv(keys_path, header=None, squeeze=True, dtype=('str'))
channel = grpc.insecure_channel(host)
def search_fn(key):
#print(key)
t = time()
response = _search_by_key(host, key, count, timeout, channel)
return time() - t
t = time()
result = p.imap_unordered(search_fn, keys.sample(100).values)
result = list(result)
print(time() - t)
print(np.array(result).mean())
if __name__ == '__main__':
cli.add_command(test)
cli.add_command(test_key)
cli.add_command(imports)
cli.add_command(search)
cli.add_command(search_by_key)
cli.add_command(test_search_perform)
cli()