-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest.c
More file actions
162 lines (130 loc) · 4.17 KB
/
test.c
File metadata and controls
162 lines (130 loc) · 4.17 KB
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
/*
* Copyright (c) 2012, Jyri J. Virkki
* All rights reserved.
*
* This file is under BSD license. See LICENSE file.
*/
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "bloom.h"
#ifdef __linux
#include <sys/time.h>
#include <time.h>
#endif
/** ***************************************************************************
* A few simple tests to check if it works at all.
*
*/
static void basic()
{
(void)printf("----- basic -----\n");
struct bloom bloom;
assert(bloom_init(&bloom, 0, 1.0) == 1);
assert(bloom_init(&bloom, 10, 0) == 1);
assert(bloom.ready == 0);
assert(bloom_add(&bloom, "hello world", 11) == -1);
assert(bloom_check(&bloom, "hello world", 11) == -1);
bloom_free(&bloom);
assert(bloom_init(&bloom, 102, 0.1) == 0);
assert(bloom.ready == 1);
bloom_print(&bloom);
assert(bloom_check(&bloom, "hello world", 11) == 0);
assert(bloom_add(&bloom, "hello world", 11) == 0);
assert(bloom_check(&bloom, "hello world", 11) == 1);
assert(bloom_add(&bloom, "hello world", 11) > 0);
assert(bloom_add(&bloom, "hello", 5) == 0);
assert(bloom_add(&bloom, "hello", 5) > 0);
assert(bloom_check(&bloom, "hello", 5) == 1);
bloom_free(&bloom);
}
/** ***************************************************************************
* Create a bloom filter with given parameters and add 'count' random elements
* into it to see if collission rates are within expectations.
*
*/
static void add_random(int entries, double error, int count)
{
(void)printf("----- add_random(%d, %f, %d) -----\n", entries, error, count);
struct bloom bloom;
assert(bloom_init(&bloom, entries, error) == 0);
bloom_print(&bloom);
char block[32];
int collisions = 0;
int fd = open("/dev/urandom", O_RDONLY);
int n;
for (n = 0; n < count; n++) {
assert(read(fd, block, 32) == 32);
if (bloom_add(&bloom, (void *)block, 32)) { collisions++; }
}
(void)close(fd);
bloom_free(&bloom);
(void)printf("added %d, collisions %d\n", count, collisions);
}
/** ***************************************************************************
* Simple loop to compare performance.
*
*/
static void perf_loop(int entries, int count)
{
(void)printf("----- perf_loop -----\n");
struct bloom bloom;
assert(bloom_init(&bloom, entries, 0.001) == 0);
bloom_print(&bloom);
int i;
int collisions = 0;
struct timeval tp;
(void)gettimeofday(&tp, NULL);
long before = (tp.tv_sec * 1000L) + (tp.tv_usec / 1000L);
for (i = 0; i < count; i++) {
if (bloom_add(&bloom, (void *)&i, sizeof(int))) { collisions++; }
}
(void)gettimeofday(&tp, NULL);
long after = (tp.tv_sec * 1000L) + (tp.tv_usec / 1000L);
(void)printf("Added %d elements of size %d, took %d ms (collisions=%d)\n",
count, (int)sizeof(int), (int)(after - before), collisions);
(void)printf("%d,%d,%ld\n", entries, bloom.bytes, after - before);
bloom_free(&bloom);
}
/** ***************************************************************************
* main...
*
* To test performance only, run with options: -p ENTRIES COUNT
* Where 'ENTRIES' is the expected number of entries used to initialize the
* bloom filter and 'COUNT' is the actual number of entries inserted.
*
* To test collisions, run with options: -c ENTRIES ERROR COUNT
* Where 'ENTRIES' is the expected number of entries used to initialize the
* bloom filter and 'ERROR' is the acceptable probability of collision
* used to initialize the bloom filter. 'COUNT' is the actual number of
* entries inserted.
*
* With no options, it runs various tests.
*
*/
int main(int argc, char **argv)
{
(void)printf("testing libbloom...\n");
if (argc == 4 && !strncmp(argv[1], "-p", 2)) {
perf_loop(atoi(argv[2]), atoi(argv[3]));
exit(0);
}
if (argc == 5 && !strncmp(argv[1], "-c", 2)) {
add_random(atoi(argv[2]), atof(argv[3]), atoi(argv[4]));
exit(0);
}
basic();
add_random(100, 0.001, 300);
int i;
for (i = 0; i < 10; i++) {
add_random(1000000, 0.001, 1000000);
}
perf_loop(10000000, 10000000);
printf("\nBrought to you by libbloom-%s\n", bloom_version());
}