aboutsummaryrefslogtreecommitdiff
path: root/tests/bloom_perf.c
blob: 6a9abf19f0887cdb805bdbf7e177c8d0265a65dc (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
#include <stdlib.h>   /* malloc */
#include <sys/time.h> /* gettimeofday */
#include <errno.h>    /* perror */
#include <stdio.h>    /* printf */
#include "uthash.h"

#define BUFLEN 20
#if 0
#undef uthash_expand_fyi
#define uthash_expand_fyi(tbl) printf("expanding to %d buckets\n", tbl->num_buckets)
#endif

typedef struct name_rec {
    char boy_name[BUFLEN];
    UT_hash_handle hh;
} name_rec;

int main(int argc,char *argv[])
{
    name_rec *name, *names=NULL;
    char linebuf[BUFLEN];
    FILE *file;
    int i=0,j,nloops=3,loopnum=0,miss;
    struct timeval tv1,tv2;
    long elapsed_usec;
    if (argc > 1) {
        nloops = atoi(argv[1]);
    }

    if ( (file = fopen( "test14.dat", "r" )) == NULL ) {
        perror("can't open: ");
        exit(-1);
    }

    while (fgets(linebuf,BUFLEN,file) != NULL) {
        i++;
        if ( (name = (name_rec*)malloc(sizeof(name_rec))) == NULL) {
            exit(-1);
        }
        strcpy(name->boy_name, linebuf);
        HASH_ADD_STR(names,boy_name,name);
    }

again:
    if (fseek(file,0,SEEK_SET) == -1) {
        fprintf(stderr,"fseek failed: %s\n", strerror(errno));
    }
    j=0;

    if (gettimeofday(&tv1,NULL) == -1) {
        perror("gettimeofday: ");
    }
    while (fgets(linebuf,BUFLEN,file) != NULL) {
        /* if we do 10 loops, the first has a 0% miss rate,
         * the second has a 10% miss rate, etc */
        miss = ((rand()*1.0/RAND_MAX) < (loopnum*1.0/nloops)) ? 1 : 0;
        /* generate a miss if we want one */
        if (miss) {
            linebuf[0]++;
            if (linebuf[1] != '\0') {
                linebuf[1]++;
            }
        }
        HASH_FIND_STR(names,linebuf,name);
        if (name) {
            j++;
        }
    }
    if (gettimeofday(&tv2,NULL) == -1) {
        perror("gettimeofday: ");
    }
    elapsed_usec = ((tv2.tv_sec - tv1.tv_sec) * 1000000) + (tv2.tv_usec - tv1.tv_usec);
    printf("lookup on %d of %d (%.2f%%) names succeeded (%.2f usec)\n", j, i,
           j*100.0/i, (double)(elapsed_usec));
    if (++loopnum < nloops) {
        goto again;
    }
    fclose(file);

    return 0;
}