2014-01-10 16:21:35 +01:00
|
|
|
/*
|
2014-02-03 23:03:13 +01:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2014-01-10 16:21:35 +01:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-01-10 16:21:35 +01:00
|
|
|
*/
|
|
|
|
|
2014-02-03 23:03:13 +01:00
|
|
|
/**
|
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Bloom filter test application
|
|
|
|
*
|
|
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2015-09-03 18:53:09 +02:00
|
|
|
#include "xtimer.h"
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
#include "hashes.h"
|
|
|
|
#include "bloom.h"
|
|
|
|
#include "random.h"
|
|
|
|
|
|
|
|
#define lenB 512
|
|
|
|
#define lenA (10 * 1000)
|
|
|
|
|
|
|
|
#define MAGIC_A 0xafafafaf
|
|
|
|
#define MAGIC_B 0x0c0c0c0c
|
|
|
|
|
|
|
|
#define myseed 0x83d385c0 /* random number */
|
|
|
|
|
|
|
|
#define BUF_SIZE 50
|
|
|
|
static uint32_t buf[BUF_SIZE];
|
|
|
|
|
|
|
|
static void buf_fill(uint32_t *buf, int len)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < len; k++) {
|
|
|
|
buf[k] = genrand_uint32();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2015-09-03 18:53:09 +02:00
|
|
|
xtimer_init();
|
2014-01-10 16:21:35 +01:00
|
|
|
|
2014-09-17 19:54:40 +02:00
|
|
|
bloom_t *bloom = bloom_new(1 << 12, 8, fnv_hash, sax_hash, sdbm_hash,
|
2014-01-25 11:29:35 +01:00
|
|
|
djb2_hash, kr_hash, dek_hash, rotating_hash, one_at_a_time_hash);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
printf("Testing Bloom filter.\n\n");
|
|
|
|
printf("m: %" PRIu32 " k: %" PRIu32 "\n\n", (uint32_t) bloom->m,
|
2014-01-25 11:29:35 +01:00
|
|
|
(uint32_t) bloom->k);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
genrand_init(myseed);
|
|
|
|
|
2015-09-03 18:53:09 +02:00
|
|
|
unsigned long t1 = xtimer_now();
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
for (int i = 0; i < lenB; i++) {
|
|
|
|
buf_fill(buf, BUF_SIZE);
|
|
|
|
buf[0] = MAGIC_B;
|
|
|
|
bloom_add(bloom,
|
|
|
|
(uint8_t *) buf,
|
2014-01-25 11:29:35 +01:00
|
|
|
BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t));
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2015-09-03 18:53:09 +02:00
|
|
|
unsigned long t2 = xtimer_now();
|
2014-01-10 16:21:35 +01:00
|
|
|
printf("adding %d elements took %" PRIu32 "ms\n", lenB,
|
2015-09-03 18:53:09 +02:00
|
|
|
(uint32_t) (t2 - t1) / 1000);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
int in = 0;
|
|
|
|
int not_in = 0;
|
|
|
|
|
2015-09-03 18:53:09 +02:00
|
|
|
unsigned long t3 = xtimer_now();
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
for (int i = 0; i < lenA; i++) {
|
|
|
|
buf_fill(buf, BUF_SIZE);
|
|
|
|
buf[0] = MAGIC_A;
|
|
|
|
|
|
|
|
if (bloom_check(bloom,
|
|
|
|
(uint8_t *) buf,
|
|
|
|
BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t))) {
|
|
|
|
in++;
|
2014-01-25 11:29:35 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-01-10 16:21:35 +01:00
|
|
|
not_in++;
|
|
|
|
}
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2015-09-03 18:53:09 +02:00
|
|
|
unsigned long t4 = xtimer_now();
|
2014-01-10 16:21:35 +01:00
|
|
|
printf("checking %d elements took %" PRIu32 "ms\n", lenA,
|
2015-09-03 18:53:09 +02:00
|
|
|
(uint32_t) (t4 - t3) / 1000);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("%d elements probably in the filter.\n", in);
|
|
|
|
printf("%d elements not in the filter.\n", not_in);
|
|
|
|
double false_positive_rate = (double) in / (double) lenA;
|
|
|
|
printf("%f false positive rate.\n", false_positive_rate);
|
|
|
|
|
|
|
|
bloom_del(bloom);
|
|
|
|
printf("\nAll done!\n");
|
|
|
|
return 0;
|
|
|
|
}
|