summaryrefslogtreecommitdiffstats
path: root/tests/exp/uri_hash.c
blob: 0b5d755a351ef2d171af06a646e9c18bb1745155 (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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>

#define NSERV   10
#define MAXLINE 1000

char line[MAXLINE];

int counts_gd1[NSERV][NSERV];
static unsigned long hash_gd1(char *uri)
{
    unsigned long hash = 0;
    int c;

    while ((c = *uri++))
        hash = c + (hash << 6) + (hash << 16) - hash;

    return hash;
}

int counts_gd2[NSERV][NSERV];
static unsigned long hash_gd2(char *uri)
{
    unsigned long hash = 0;
    int c;

    while ((c = *uri++)) {
	if (c == '?' || c == '\n')
	    break;
        hash = c + (hash << 6) + (hash << 16) - hash;
    }

    return hash;
}


int counts_gd3[NSERV][NSERV];
static unsigned long hash_gd3(char *uri)
{
    unsigned long hash = 0;
    int c;

    while ((c = *uri++)) {
	if (c == '?' || c == '\n')
	    break;
        hash = c - (hash << 3) + (hash << 15) - hash;
    }

    return hash;
}


int counts_gd4[NSERV][NSERV];
static unsigned long hash_gd4(char *uri)
{
    unsigned long hash = 0;
    int c;

    while ((c = *uri++)) {
	if (c == '?' || c == '\n')
	    break;
        hash = hash + (hash << 6) - (hash << 15) - c;
    }

    return hash;
}


int counts_gd5[NSERV][NSERV];
static unsigned long hash_gd5(char *uri)
{
    unsigned long hash = 0;
    int c;

    while ((c = *uri++)) {
	if (c == '?' || c == '\n')
	    break;
        hash = hash + (hash << 2) - (hash << 19) - c;
    }

    return hash;
}


int counts_gd6[NSERV][NSERV];
static unsigned long hash_gd6(char *uri)
{
    unsigned long hash = 0;
    int c;

    while ((c = *uri++)) {
	if (c == '?' || c == '\n')
	    break;
        hash = hash + (hash << 2) - (hash << 22) - c;
    }

    return hash;
}


int counts_wt1[NSERV][NSERV];
static unsigned long hash_wt1(int hsize, char *string) {
    int bits;
    unsigned long data, val;

    bits = val = data = 0;
    while (*string) {
	if (*string == '?' || *string == '\n')
	    break;
        data |= ((unsigned long)(unsigned char)*string) << bits;
        bits += 8;
        while (bits >= hsize) {
            val ^= data - (val >> hsize);
            bits -= hsize;
            data >>= hsize;
        }
        string++;
    }
    val ^= data;
    while (val > ((1 << hsize) - 1)) {
        val = (val & ((1 << hsize) - 1)) ^ (val >> hsize);
    }
    return val;
}

/*
 * efficient hash : no duplicate on the first 65536 values of 2 bytes.
 * less than 0.1% duplicates for the first 1.6 M values of 3 bytes.
 */
int counts_wt2[NSERV][NSERV];
typedef unsigned int u_int32_t;

static inline u_int32_t shl32(u_int32_t i, int count) {
	if (count == 32)
		return 0;
	return i << count;
}

static inline u_int32_t shr32(u_int32_t i, int count) {
	if (count == 32)
		return 0;
	return i >> count;
}

static unsigned int rev32(unsigned int c) {
	c = ((c & 0x0000FFFF) << 16)| ((c & 0xFFFF0000) >> 16);
	c = ((c & 0x00FF00FF) << 8) | ((c & 0xFF00FF00) >> 8);
	c = ((c & 0x0F0F0F0F) << 4) | ((c & 0xF0F0F0F0) >> 4);
	c = ((c & 0x33333333) << 2) | ((c & 0xCCCCCCCC) >> 2);
	c = ((c & 0x55555555) << 1) | ((c & 0xAAAAAAAA) >> 1);
	return c;
}

int hash_wt2(const char *src, int len) {
	unsigned int i = 0x3C964BA5; /* as many ones as zeroes */
	unsigned int j;
	unsigned int ih, il;
	int bit;

	while (len--) {
		j = (unsigned char)*src++;
		if (j == '?' || j == '\n')
		    break;
		bit = rev32(j - i);
		bit = bit - (bit >> 3) + (bit >> 16) - j;

		bit &= 0x1f;
		ih = shr32(i, bit);
		il = i & (shl32(1, bit) - 1);
		i = shl32(il, 32-bit) - ih - ~j;
	}
	return i;
}


//typedef unsigned int uint32_t;
//typedef unsigned short uint8_t;
//typedef unsigned char uint16_t;

/*
 * http://www.azillionmonkeys.com/qed/hash.html
 */
#undef get16bits
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
  || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
#define get16bits(d) (*((const uint16_t *) (d)))
#endif

#if !defined (get16bits)
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
                       +(uint32_t)(((const uint8_t *)(d))[0]) )
#endif

/*
 * This function has a hole of 11 unused bits in bytes 2 and 3 of each block of
 * 32 bits.
 */
int counts_SuperFastHash[NSERV][NSERV];

uint32_t SuperFastHash (const char * data, int len) {
uint32_t hash = len, tmp;
int rem;

    if (len <= 0 || data == NULL) return 0;

    rem = len & 3;
    len >>= 2;

    /* Main loop */
    for (;len > 0; len--) {
        hash  += get16bits (data);
        tmp    = (get16bits (data+2) << 11) ^ hash;
        hash   = (hash << 16) ^ tmp;
        data  += 2*sizeof (uint16_t);
        hash  += hash >> 11;
    }

    /* Handle end cases */
    switch (rem) {
        case 3: hash += get16bits (data);
                hash ^= hash << 16;
                hash ^= data[sizeof (uint16_t)] << 18;
                hash += hash >> 11;
                break;
        case 2: hash += get16bits (data);
                hash ^= hash << 11;
                hash += hash >> 17;
                break;
        case 1: hash += *data;
                hash ^= hash << 10;
                hash += hash >> 1;
    }

    /* Force "avalanching" of final 127 bits */
    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 4;
    hash += hash >> 17;
    hash ^= hash << 25;
    hash += hash >> 6;

    return hash;
}

/*
 * This variant uses all bits from the input block, and is about 15% faster.
 */
int counts_SuperFastHash2[NSERV][NSERV];
uint32_t SuperFastHash2 (const char * data, int len) {
uint32_t hash = len, tmp;
int rem;

    if (len <= 0 || data == NULL) return 0;

    rem = len & 3;
    len >>= 2;

    /* Main loop */
    for (;len > 0; len--) {
	register uint32_t next;
	next   = get16bits(data+2);
        hash  += get16bits(data);
        tmp    = ((next << 11) | (next >> 21)) ^ hash;
        hash   = (hash << 16) ^ tmp;
        data  += 2*sizeof (uint16_t);
        hash  += hash >> 11;
    }

    /* Handle end cases */
    switch (rem) {
        case 3: hash += get16bits (data);
                hash ^= hash << 16;
                hash ^= data[sizeof (uint16_t)] << 18;
                hash += hash >> 11;
                break;
        case 2: hash += get16bits (data);
                hash ^= hash << 11;
                hash += hash >> 17;
                break;
        case 1: hash += *data;
                hash ^= hash << 10;
                hash += hash >> 1;
    }

    /* Force "avalanching" of final 127 bits */
    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 4;
    hash += hash >> 17;
    hash ^= hash << 25;
    hash += hash >> 6;

    return hash;
}

/* len 4 for ipv4 and 16 for ipv6 */
int counts_srv[NSERV][NSERV];
unsigned int haproxy_server_hash(const char *addr, int len){
  unsigned int h, l;
  l = h = 0;

  while ((l + sizeof (int)) <= len) {
    h ^= ntohl(*(unsigned int *)(&addr[l]));
    l += sizeof (int);
  }
  return h;
}/* end haproxy_server_hash() */



void count_hash_results(unsigned long hash, int counts[NSERV][NSERV]) {
    int srv, nsrv;
    
    for (nsrv = 0; nsrv < NSERV; nsrv++) {
	srv = hash % (nsrv + 1);
	counts[nsrv][srv]++;
    }
}

void dump_hash_results(char *name, int counts[NSERV][NSERV]) {
    int srv, nsrv;

    printf("%s:\n", name);
    for (nsrv = 0; nsrv < NSERV; nsrv++) {
	printf("%02d srv: ", nsrv+1);
	for (srv = 0; srv <= nsrv; srv++) {
	    //printf("%6d ", counts[nsrv][srv]);
	    //printf("%3.1f ", (100.0*counts[nsrv][srv]) / (double)counts[0][0]);
	    printf("%3.1f ", 100.0*(counts[nsrv][srv] - (double)counts[0][0]/(nsrv+1)) / (double)counts[0][0]);
	}
	printf("\n");
    }
    printf("\n");
}

int main() {
    memset(counts_gd1, 0, sizeof(counts_gd1));
    memset(counts_gd2, 0, sizeof(counts_gd2));
    memset(counts_gd3, 0, sizeof(counts_gd3));
    memset(counts_gd4, 0, sizeof(counts_gd4));
    memset(counts_gd5, 0, sizeof(counts_gd5));
    memset(counts_gd6, 0, sizeof(counts_gd6));
    memset(counts_wt1, 0, sizeof(counts_wt1));
    memset(counts_wt2, 0, sizeof(counts_wt2));
    memset(counts_srv, 0, sizeof(counts_srv));
    memset(counts_SuperFastHash, 0, sizeof(counts_SuperFastHash));
    memset(counts_SuperFastHash2, 0, sizeof(counts_SuperFastHash2));

    while (fgets(line, MAXLINE, stdin) != NULL) {
	count_hash_results(hash_gd1(line), counts_gd1);
	count_hash_results(hash_gd2(line), counts_gd2);
	count_hash_results(hash_gd3(line), counts_gd3);
	count_hash_results(hash_gd4(line), counts_gd4);
	count_hash_results(hash_gd5(line), counts_gd5);
	count_hash_results(hash_gd6(line), counts_gd6);
	count_hash_results(hash_wt1(31, line), counts_wt1);
	count_hash_results(hash_wt2(line, strlen(line)), counts_wt2);
	count_hash_results(haproxy_server_hash(line, strlen(line)), counts_srv);
	count_hash_results(SuperFastHash(line, strlen(line)), counts_SuperFastHash);
	count_hash_results(SuperFastHash2(line, strlen(line)), counts_SuperFastHash2);
    }

    dump_hash_results("hash_gd1", counts_gd1);
    dump_hash_results("hash_gd2", counts_gd2);
    dump_hash_results("hash_gd3", counts_gd3);
    dump_hash_results("hash_gd4", counts_gd4);
    dump_hash_results("hash_gd5", counts_gd5);
    dump_hash_results("hash_gd6", counts_gd6);
    dump_hash_results("hash_wt1", counts_wt1);
    dump_hash_results("hash_wt2", counts_wt2);
    dump_hash_results("haproxy_server_hash", counts_srv);
    dump_hash_results("SuperFastHash", counts_SuperFastHash);
    dump_hash_results("SuperFastHash2", counts_SuperFastHash2);

    return 0;
}