summaryrefslogtreecommitdiffstats
path: root/test/benchmark.c
blob: 2d7925a6b444eb6fd02ef4de76f1eb8a1cce20b7 (plain)
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
#include "GeoIP.h"
#include "GeoIPCity.h"
#include <stdio.h>
#if !defined(_MSC_VER)
#include <sys/time.h>
#endif

char *ipstring[4] = {
    "24.24.24.24", "80.24.24.80", "200.24.24.40", "68.24.24.46"};

int numipstrings = 4;

#if defined(_WIN32)
#define DATADIR SRCDIR "/data/"
#else
#define DATADIR "/usr/local/share/GeoIP/"
#endif

#define GEOIP_OPEN(basename, flg)                                              \
    do {                                                                       \
        i = GeoIP_open(DATADIR basename, (flg) | GEOIP_SILENCE);               \
        if (i == NULL) {                                                       \
            printf("error: %s%s does not exist\n", DATADIR, basename);         \
            return;                                                            \
            /* or a 'longjmp(geo_jmp,1)'? */                                   \
        }                                                                      \
    } while (0)

#if !defined(_WIN32)
struct timeval timer_t1;
struct timeval timer_t2;
#else  /* !defined(_WIN32) */
FILETIME timer_t1; /* 100 ns */
FILETIME timer_t2;
#endif /* !defined(_WIN32) */

#if !defined(_WIN32)
void timerstart(void) { gettimeofday(&timer_t1, NULL); }

double timerstop(void) {
    int a1 = 0;
    int a2 = 0;
    double r = 0;
    gettimeofday(&timer_t2, NULL);
    a1 = timer_t2.tv_sec - timer_t1.tv_sec;
    a2 = timer_t2.tv_usec - timer_t1.tv_usec;
    if (a1 < 0) {
        a1 = a1 - 1;
        a2 = a2 + 1000000;
    }
    r = (((double)a1) + (((double)a2) / 1000000));
    return r;
}
#else  /* !defined(_WIN32) */
void timerstart(void) { GetSystemTimeAsFileTime(&timer_t1); }

double timerstop(void) {
    __int64 delta; /* VC6 can't convert an unsigned int64 to to double */
    GetSystemTimeAsFileTime(&timer_t2);
    delta = FILETIME_TO_USEC(timer_t2) - FILETIME_TO_USEC(timer_t1);
    return ((double)delta) / 1E6;
}
#endif /* !defined(_WIN32) */

void testgeoipcountry(int flags, const char *msg, int numlookups) {
    double t = 0;
    int i4 = 0;
    int i2 = 0;
    GeoIP *i = NULL;

    GEOIP_OPEN("GeoIP.dat", flags);
    timerstart();
    for (i2 = 0; i2 < numlookups; i2++) {
        GeoIP_country_name_by_addr(i, ipstring[i4]);
        i4 = (i4 + 1) % numipstrings;
    }
    t = timerstop();
    printf("%s\n", msg);
    printf("%d lookups made in %f seconds \n", numlookups, t);
    GeoIP_delete(i);
}

void testgeoiporg(int flags, const char *msg, int numlookups) {
    GeoIP *i = NULL;
    int i4 = 0;
    int i2 = 0;
    double t = 0;

    GEOIP_OPEN("GeoIPOrg.dat", flags);
    timerstart();
    for (i2 = 0; i2 < numlookups; i2++) {
        free(GeoIP_name_by_addr(i, ipstring[i4]));
        i4 = (i4 + 1) % numipstrings;
    }
    t = timerstop();
    printf("%s\n", msg);
    printf("%d lookups made in %f seconds \n", numlookups, t);
    GeoIP_delete(i);
}

void testgeoipregion(int flags, const char *msg, int numlookups) {
    GeoIP *i = NULL;
    GeoIPRegion *i3 = NULL;
    int i4 = 0;
    int i2 = 0;
    double t = 0;

    GEOIP_OPEN("GeoIPRegion.dat", flags);
    timerstart();
    for (i2 = 0; i2 < numlookups; i2++) {
        i3 = GeoIP_region_by_addr(i, ipstring[i4]);
        GeoIPRegion_delete(i3);
        i4 = (i4 + 1) % numipstrings;
    }
    t = timerstop();
    printf("%s\n", msg);
    printf("%d lookups made in %f seconds \n", numlookups, t);
    GeoIP_delete(i);
}

void testgeoipcity(int flags, const char *msg, int numlookups) {
    GeoIP *i = NULL;
    GeoIPRecord *i3 = NULL;
    int i4 = 0;
    int i2 = 0;
    double t = 0;

    GEOIP_OPEN("GeoLiteCity.dat", flags);
    timerstart();
    for (i2 = 0; i2 < numlookups; i2++) {
        i3 = GeoIP_record_by_addr(i, ipstring[i4]);
        GeoIPRecord_delete(i3);
        i4 = (i4 + 1) % numipstrings;
    }
    t = timerstop();
    printf("%s\n", msg);
    printf("%d lookups made in %f seconds \n", numlookups, t);
    GeoIP_delete(i);
}

int main(void) {
    int time = 300 * numipstrings;
    testgeoipcountry(0, "GeoIP Country", 100 * time);
    testgeoipcountry(
        GEOIP_CHECK_CACHE, "GeoIP Country with GEOIP_CHECK_CACHE", 100 * time);
    testgeoipcountry(GEOIP_MEMORY_CACHE,
                     "GeoIP Country with GEOIP_MEMORY_CACHE",
                     1000 * time);
    testgeoipcountry(
        GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE,
        "GeoIP Country with GEOIP_MEMORY_CACHE and GEOIP_CHECK_CACHE",
        1000 * time);

    testgeoipregion(0, "GeoIP Region", 100 * time);
    testgeoipregion(
        GEOIP_CHECK_CACHE, "GeoIP Region with GEOIP_CHECK_CACHE", 100 * time);
    testgeoipregion(GEOIP_MEMORY_CACHE,
                    "GeoIP Region with GEOIP_MEMORY_CACHE",
                    1000 * time);
    testgeoipregion(
        GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE,
        "GeoIP Region with GEOIP_MEMORY_CACHE and GEOIP_CHECK_CACHE",
        1000 * time);

    testgeoiporg(0, "GeoIP Org", 50 * time);
    testgeoiporg(
        GEOIP_INDEX_CACHE, "GeoIP Org with GEOIP_INDEX_CACHE", 200 * time);
    testgeoiporg(GEOIP_INDEX_CACHE | GEOIP_CHECK_CACHE,
                 "GeoIP Org with GEOIP_INDEX_CACHE and GEOIP_CHECK_CACHE",
                 200 * time);
    testgeoiporg(
        GEOIP_MEMORY_CACHE, "GeoIP Org with GEOIP_MEMORY_CACHE", 500 * time);

    testgeoipcity(0, "GeoIP City", 50 * time);
    testgeoipcity(
        GEOIP_INDEX_CACHE, "GeoIP City with GEOIP_INDEX_CACHE", 200 * time);
    testgeoipcity(GEOIP_INDEX_CACHE | GEOIP_CHECK_CACHE,
                  "GeoIP City with GEOIP_INDEX_CACHE and GEOIP_CHECK_CACHE",
                  200 * time);
    testgeoipcity(
        GEOIP_MEMORY_CACHE, "GeoIP City with GEOIP_MEMORY_CACHE", 500 * time);
    return 0;
}