1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <sys/time.h>
6 #include <time.h>
7 #include "time_sample.h"
8 
9 #include <hiredis/hiredis.h>
10 #include <hiredis/async.h>
11 #include <hiredis/adapters/libev.h>
12 
13 #define PORT 6379
14 #define N 500000
fill_first()15 void fill_first()
16 {
17     redisContext* c = redisConnect("127.0.0.1", PORT);
18     redisCommand(c, "FLUSHDB");
19 
20     for (int i = 0; i < N; i++)
21     {
22         int n = (rand() % 24) + 1; // some random size
23         char tag[n], name[n];
24 
25         const char alphanum[] = { "abcdefghijklmnopqrstuvwxyz" };
26 
27         uint8_t rand_idx[n * 2];
28         for (int xx = 0; xx < n*2; xx++) {
29             rand_idx[xx] = rand() % 0xff;
30         }
31 
32         for (int i = 0; i < n; i ++)
33             tag[i] = alphanum[rand_idx[i] % sizeof(alphanum)]; // fill the array with alphanum chars
34 
35         for (int i = n; i < n * 2; i ++)
36             name[i - n] = alphanum[rand_idx[i] % sizeof(alphanum)]; // fill the array with alphanum chars
37 
38         redisCommand(c, "FT.SUGADD userslex %b:%b %d", tag, n, name, n, i);
39     }
40 
41     redisFree(c);
42 }
43 
add_delete(const char * variant)44 void add_delete(const char* variant)
45 {
46     redisContext* c = redisConnect("127.0.0.1", PORT);
47 
48     for (int i = 0; i < N; i++)
49         redisCommand(c, "FT.SUGADD userslex %s%d %d", variant, i, i);
50     printf("Deleting!\n");
51     for (int i = 0; i < N; i++)
52         redisCommand(c, "FT.SUGDEL userslex %s%d", variant, i);
53 
54     redisFree(c);
55 }
56 
search(const char * str)57 void search(const char* str)
58 {
59     redisContext* c = redisConnect("127.0.0.1", PORT);
60     redisCommand(c, "FT.SUGGET userslex %b MAX 10 FUZZY", str, strlen(str));
61     redisFree(c);
62 }
63 
64 // typedef struct {
65 //     struct timespec start_time, end_time;
66 // } TimerSampler;
67 
68 // void TimeSampler_Start(TimerSampler *ts) {
69 //     clock_gettime(CLOCK_REALTIME, &ts->start_time);
70 // }
71 
72 // void TimeSampler_End(TimerSampler *ts) {
73 
74 //    clock_gettime(CLOCK_REALTIME, &ts->end_time);
75 
76 // }
77 
78 // long long TimeSampler_DurationNS(TimerSampler *ts) {
79 
80 //     long long diffInNanos = ((long long)1000000000 * ts->end_time.tv_sec + ts->end_time.tv_nsec) -
81 //     ((long long)1000000000 * ts->start_time.tv_sec + ts->start_time.tv_nsec);
82 //     return diffInNanos;
83 // }
84 
85 // #define TimeSampledBlock(ts, blk) { TimeSampler_Start(ts); { blk } ; TimeSampler_End(ts); }
86 
main(int argc,char ** argv)87 int main (int argc, char** argv)
88 {
89 
90     printf("filling first!\n");
91 
92 
93     TIME_SAMPLE_RUN(fill_first());
94 
95 
96 
97     for (int i  =0; i < 10; i++) {
98     TIME_SAMPLE_RUN(search("asdfg"));
99     }
100     TIME_SAMPLE_RUN(add_delete("asdfg")); // now add 1000000 entries of a variant string and remove those entries
101 
102  for (int i  =0; i < 10; i++) {
103     TIME_SAMPLE_RUN(search("asdfg")); // ended in 2 ms
104 
105  }
106     //search("asdfg"); // ended in 66 ms, i.e. 33 times slower on the same key-set
107     redisContext* c = redisConnect("127.0.0.1", PORT);
108     redisCommand(c, "FLUSHDB");
109 }