1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdint.h>
4 #include <sys/time.h>
5 #include <time.h>
6 #include <math.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 
11 #include "../libpatricia/patricia.h"
12 
13 /*
14   How to compile:
15     gcc ../libpatricia/patricia.c -c -opatricia.o
16     gcc patricia_performance_tests.c patricia.o -o patricia_performance_test
17 */
18 
main()19 int main() {
20     patricia_tree_t* lookup_tree;
21     lookup_tree = New_Patricia(32);
22 
23     make_and_lookup(lookup_tree, "46.36.216.0/21");
24     make_and_lookup(lookup_tree, "159.253.16.0/21");
25     make_and_lookup(lookup_tree, "5.45.112.0/21");
26     make_and_lookup(lookup_tree, "5.45.120.0/21");
27     make_and_lookup(lookup_tree, "5.101.112.0/21");
28     make_and_lookup(lookup_tree, "5.101.120.0/21");
29     make_and_lookup(lookup_tree, "185.4.72.0/22");
30     make_and_lookup(lookup_tree, "181.114.240.0/20");
31     make_and_lookup(lookup_tree, "193.42.142.0/24");
32 
33     prefix_t prefix_for_check_adreess;
34     prefix_for_check_adreess.family = AF_INET;
35     prefix_for_check_adreess.bitlen = 32;
36     patricia_node_t* found_patrica_node = NULL;
37     // prefix_for_check_adreess.add.sin.s_addr = 123123123;
38 
39     // std::map <unsigned int, bool> lpm_cache;
40     // Without cache: 16.7 million of operations
41 
42     int i_iter = 100;
43     // Million operations
44     int j_iter = 1000000;
45 
46     // printf("Preallocate table\n");
47     // Iterate over all our IP addresses
48     // for (int j = 0; j < j_iter; j++) {
49     //    for (int i = 0; i < i_iter; i++) {
50     //        lpm_cache[i*j] = true;
51     //    }
52     //}
53 
54     printf("Start tests\n");
55     struct timespec start_time;
56     clock_gettime(CLOCK_REALTIME, &start_time);
57 
58     int i, j;
59     for (j = 0; j < j_iter; j++) {
60         for (i = 0; i < i_iter; i++) {
61             // Random Pseudo IP
62             prefix_for_check_adreess.add.sin.s_addr = i*j;
63             patricia_node_t* found_patrica_node =
64             patricia_search_best2(lookup_tree, &prefix_for_check_adreess, 1);
65 
66             unsigned long destination_subnet = 0;
67 
68             if (found_patrica_node != NULL) {
69                 // printf("Found\n");
70             }
71         }
72     }
73 
74     struct timespec finish_time;
75     clock_gettime(CLOCK_REALTIME, &finish_time);
76 
77     unsigned long used_seconds = finish_time.tv_sec - start_time.tv_sec;
78     unsigned long total_ops = i_iter * j_iter;
79     float megaops_per_second = (float)total_ops / (float)used_seconds / 1000000;
80 
81     printf("Total time is %d seconds total ops: %d\nMillion of ops per second: %.1f\n",
82            used_seconds, total_ops, megaops_per_second);
83 
84     Destroy_Patricia(lookup_tree, (void_fn_t)0);
85 }
86