-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
276 lines (240 loc) · 8.51 KB
/
test.cpp
File metadata and controls
276 lines (240 loc) · 8.51 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
/*
* Curated by @PsyChip
* root@psychip.net
* April 2026
*
* test.cpp - test client for vec
*
* Usage: test <name> <dim> [port]
*
* Connects to vec via TCP, pushes vectors with labels via bpush,
* queries with text (pull/cpull) and binary (bpull/bcpull),
* tests label override, verifies results.
*
* Build:
* cl /O2 /EHsc test.cpp /Fe:test.exe ws2_32.lib
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DEFAULT_PORT 1920
#define NUM_VECTORS 1000
#define BUF_SIZE (1 << 20)
static int sock_readline(SOCKET s, char *buf, int buf_size) {
int total = 0;
while (total < buf_size - 1) {
int r = recv(s, buf + total, 1, 0);
if (r <= 0) break;
total++;
if (buf[total - 1] == '\n') break;
}
buf[total] = '\0';
return total;
}
static int sock_command(SOCKET s, const char *cmd, char *resp, int resp_size) {
int len = (int)strlen(cmd);
send(s, cmd, len, 0);
return sock_readline(s, resp, resp_size);
}
static float randf() {
return ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
}
static void build_query_cmd(char *cmd, const char *prefix, const float *query, int dim) {
char *p = cmd;
p += sprintf(p, "%s ", prefix);
for (int d = 0; d < dim; d++) {
if (d > 0) *p++ = ',';
p += sprintf(p, "%.6f", query[d]);
}
*p++ = '\n';
*p = '\0';
}
static void print_results(const char *resp, const char *pick_label) {
char buf[4096];
strncpy(buf, resp, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
printf(" rank label/index distance\n");
printf(" ---- ----------------------- ----------\n");
int rank = 1;
char *tok = strtok(buf, ",\n");
while (tok) {
char *last_colon = strrchr(tok, ':');
if (!last_colon) { tok = strtok(NULL, ",\n"); continue; }
*last_colon = '\0';
const char *key = tok;
float dist = (float)atof(last_colon + 1);
int is_match = (pick_label && strcmp(key, pick_label) == 0);
printf(" %4d %-23s %.6f%s\n", rank, key, dist, is_match ? " <-- MATCH" : "");
rank++;
tok = strtok(NULL, ",\n");
}
}
int main(int argc, char **argv) {
if (argc < 3) {
fprintf(stderr, "Usage: test <name> <dim> [host:port]\n");
return 1;
}
const char *name = argv[1];
int dim = atoi(argv[2]);
char host[256] = "127.0.0.1";
int port = DEFAULT_PORT;
if (argc >= 4) {
strncpy(host, argv[3], sizeof(host) - 1);
char *colon = strchr(host, ':');
if (colon) {
*colon = '\0';
port = atoi(colon + 1);
}
}
if (dim <= 0) {
fprintf(stderr, "ERROR: invalid dim\n");
return 1;
}
printf("name=%s dim=%d host=%s port=%d vectors=%d\n", name, dim, host, port, NUM_VECTORS);
srand((unsigned)time(NULL));
size_t total_floats = (size_t)NUM_VECTORS * dim;
float *vectors = (float *)malloc(total_floats * sizeof(float));
for (size_t i = 0; i < total_floats; i++) vectors[i] = randf();
int pick = rand() % NUM_VECTORS;
float *query = (float *)malloc(dim * sizeof(float));
memcpy(query, vectors + (size_t)pick * dim, dim * sizeof(float));
for (int d = 0; d < dim; d++) query[d] += randf() * 0.0001f;
/* generate labels for each vector */
char pick_label[128];
snprintf(pick_label, sizeof(pick_label), "test/vector_%d?dim=%d", pick, dim);
printf("query is noisy copy of vector #%d (label=%s)\n\n", pick, pick_label);
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
fprintf(stderr, "ERROR: socket() failed\n");
return 1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (inet_pton(AF_INET, host, &addr.sin_addr) != 1) {
struct hostent *he = gethostbyname(host);
if (!he) {
fprintf(stderr, "ERROR: cannot resolve '%s'\n", host);
closesocket(s);
WSACleanup();
return 1;
}
memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length);
}
addr.sin_port = htons((unsigned short)port);
if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
fprintf(stderr, "ERROR: connect() failed - is vec running on %s:%d?\n", host, port);
closesocket(s);
WSACleanup();
return 1;
}
printf("connected to %s:%d\n", host, port);
char *cmd = (char *)malloc(BUF_SIZE);
char resp[4096];
printf("pushing %d vectors via bpush...\n", NUM_VECTORS);
LARGE_INTEGER freq, t0, t1;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t0);
for (int i = 0; i < NUM_VECTORS; i++) {
/* send: bpush label\n<dim*4 bytes> */
char header[256];
int hlen = sprintf(header, "bpush test/vector_%d?dim=%d\n", i, dim);
send(s, header, hlen, 0);
const char *vec_data = (const char *)(vectors + i * dim);
int vec_bytes = dim * (int)sizeof(float);
int sent = 0;
while (sent < vec_bytes) {
int r = send(s, vec_data + sent, vec_bytes - sent, 0);
if (r <= 0) { fprintf(stderr, "ERROR: send failed\n"); goto done; }
sent += r;
}
sock_readline(s, resp, sizeof(resp));
if (strncmp(resp, "err", 3) == 0) {
fprintf(stderr, "ERROR on bpush %d: %s", i, resp);
goto done;
}
}
QueryPerformanceCounter(&t1);
printf("pushed %d vectors in %.1f ms\n\n", NUM_VECTORS, (double)(t1.QuadPart - t0.QuadPart) * 1000.0 / freq.QuadPart);
build_query_cmd(cmd, "pull", query, dim);
printf("[L2] pull:\n");
QueryPerformanceCounter(&t0);
sock_command(s, cmd, resp, sizeof(resp));
QueryPerformanceCounter(&t1);
print_results(resp, pick_label);
printf(" time: %.3f ms\n\n", (double)(t1.QuadPart - t0.QuadPart) * 1000.0 / freq.QuadPart);
build_query_cmd(cmd, "cpull", query, dim);
printf("[COSINE] cpull:\n");
QueryPerformanceCounter(&t0);
sock_command(s, cmd, resp, sizeof(resp));
QueryPerformanceCounter(&t1);
print_results(resp, pick_label);
printf(" time: %.3f ms\n\n", (double)(t1.QuadPart - t0.QuadPart) * 1000.0 / freq.QuadPart);
/* --- bpull: binary L2 query --- */
{
printf("[L2] bpull (binary):\n");
send(s, "bpull\n", 6, 0);
int vec_bytes = dim * (int)sizeof(float);
int sent = 0;
while (sent < vec_bytes) {
int r = send(s, (const char *)query + sent, vec_bytes - sent, 0);
if (r <= 0) { fprintf(stderr, "ERROR: send failed\n"); goto done; }
sent += r;
}
QueryPerformanceCounter(&t0);
sock_readline(s, resp, sizeof(resp));
QueryPerformanceCounter(&t1);
print_results(resp, pick_label);
printf(" time: %.3f ms\n\n", (double)(t1.QuadPart - t0.QuadPart) * 1000.0 / freq.QuadPart);
}
/* --- bcpull: binary cosine query --- */
{
printf("[COSINE] bcpull (binary):\n");
send(s, "bcpull\n", 7, 0);
int vec_bytes = dim * (int)sizeof(float);
int sent = 0;
while (sent < vec_bytes) {
int r = send(s, (const char *)query + sent, vec_bytes - sent, 0);
if (r <= 0) { fprintf(stderr, "ERROR: send failed\n"); goto done; }
sent += r;
}
QueryPerformanceCounter(&t0);
sock_readline(s, resp, sizeof(resp));
QueryPerformanceCounter(&t1);
print_results(resp, pick_label);
printf(" time: %.3f ms\n\n", (double)(t1.QuadPart - t0.QuadPart) * 1000.0 / freq.QuadPart);
}
/* --- label override test --- */
{
printf("label override test:\n");
char lcmd[256];
sprintf(lcmd, "label %d relabeled/vector_%d\n", pick, pick);
sock_command(s, lcmd, resp, sizeof(resp));
printf(" label %d -> %s", pick, resp);
/* re-query to see updated label */
build_query_cmd(cmd, "pull", query, dim);
sock_command(s, cmd, resp, sizeof(resp));
char relabel[128];
snprintf(relabel, sizeof(relabel), "relabeled/vector_%d", pick);
print_results(resp, relabel);
printf("\n");
}
sock_command(s, "size\n", resp, sizeof(resp));
printf("size: %s", resp);
done:
closesocket(s);
WSACleanup();
free(vectors);
free(query);
free(cmd);
printf("done.\n");
return 0;
}