1 #include "redisearch.h"
2 #include "module.h"
3 #include "version.h"
4 #include "redisearch_api.h"
5 #include "../cpptests/redismock/redismock.h"
6 
7 #include <cstdio>
8 #include <cstdlib>
9 #include <chrono>
10 #include <cstring>
11 #include <cassert>
12 
13 #define NUM_DOCS 5000000UL  // 10M
14 #define NUM_ITER 100UL
15 
16 REDISEARCH_API_INIT_SYMBOLS();
17 
18 extern "C" {
19 
my_OnLoad(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)20 static int my_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
21 
22   if (RedisModule_Init(ctx, REDISEARCH_MODULE_NAME, REDISEARCH_MODULE_VERSION, REDISMODULE_APIVER_1) ==
23       REDISMODULE_ERR)
24     return REDISMODULE_ERR;
25   return RediSearch_InitModuleInternal(ctx, argv, argc);
26 }
27 
28 }
29 
main(int,char **)30 int main(int, char **) {
31   const char *arguments[] = {"SAFEMODE", "NOGC"};
32   RMCK_Bootstrap(my_OnLoad, arguments, 2);
33   RediSearch_Initialize();
34   auto options = RediSearch_CreateIndexOptions();
35   RediSearch_IndexOptionsSetFlags(options, RSIDXOPT_DOCTBLSIZE_UNLIMITED);
36   auto idx = RediSearch_CreateIndex("ix", options);
37   RediSearch_FreeIndexOptions(options);
38 
39   RediSearch_CreateField(idx, "f1", RSFLDTYPE_FULLTEXT, RSFLDOPT_NONE);
40   // Ok so far..
41   for (size_t ii = 0; ii < NUM_DOCS; ++ii) {
42     auto d = RediSearch_CreateDocument(&ii, sizeof ii, 1.0, NULL);
43     RediSearch_DocumentAddFieldCString(d, "f1", "hello", RSFLDTYPE_DEFAULT);
44     RediSearch_SpecAddDocument(idx, d);
45 
46     if ((ii + 1) % 10000 == 0) {
47       printf("\r%lu/%lu done        ", ii + 1, NUM_DOCS);
48       fflush(stdout);
49     }
50   }
51   printf("\n");
52 
53   // so far so good?
54   // now, execute the query
55   using std::chrono::duration_cast;
56   using std::chrono::microseconds;
57   using std::chrono::system_clock;
58 
59   microseconds elapsed;
60   for (size_t ii = 0; ii < NUM_ITER; ++ii) {
61     auto qn = RediSearch_CreateTokenNode(idx, "f1", "hello");
62     auto it = RediSearch_GetResultsIterator(qn, idx);
63     assert(it);
64     size_t ndummy = 0;
65 
66     auto begin = system_clock::now();
67     size_t n = 0;
68     while (RediSearch_ResultsIteratorNext(it, idx, &ndummy)) {
69       n++;
70     }
71     elapsed += duration_cast<microseconds>(system_clock::now() - begin);
72     // elapsed += system_clock::now() - begin;
73     assert(n == NUM_DOCS);
74     RediSearch_ResultsIteratorFree(it);
75     if ((ii + 1) % 10 == 0) {
76       printf("\r%lu/%lu queries performed", ii + 1, NUM_ITER);
77       fflush(stdout);
78     }
79   }
80 
81   printf("\n");
82   printf(s/query: %llu\n", (unsigned long long)elapsed.count() / NUM_ITER);
83   printf("ms/query: %llu\n",
84          (unsigned long long)duration_cast<std::chrono::milliseconds>(elapsed).count() / NUM_ITER);
85 }
86