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"
|
2015-09-14 10:48:59 +02:00
|
|
|
#include "bitfield.h"
|
2014-01-10 16:21:35 +01:00
|
|
|
|
2015-09-14 10:48:59 +02:00
|
|
|
#define BLOOM_BITS (1UL << 12)
|
|
|
|
#define BLOOM_HASHF (8)
|
2014-01-10 16:21:35 +01:00
|
|
|
#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];
|
2015-09-14 10:48:59 +02:00
|
|
|
static bloom_t bloom;
|
|
|
|
BITFIELD(bf, BLOOM_BITS);
|
|
|
|
hashfp_t hashes[BLOOM_HASHF] = {
|
|
|
|
(hashfp_t) fnv_hash, (hashfp_t) sax_hash, (hashfp_t) sdbm_hash,
|
|
|
|
(hashfp_t) djb2_hash, (hashfp_t) kr_hash, (hashfp_t) dek_hash,
|
|
|
|
(hashfp_t) rotating_hash, (hashfp_t) one_at_a_time_hash,
|
|
|
|
};
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
static void buf_fill(uint32_t *buf, int len)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < len; k++) {
|
2016-02-14 20:04:10 +01:00
|
|
|
buf[k] = random_uint32();
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2015-09-03 18:53:09 +02:00
|
|
|
xtimer_init();
|
2014-01-10 16:21:35 +01:00
|
|
|
|
2015-09-14 10:48:59 +02:00
|
|
|
bloom_init(&bloom, BLOOM_BITS, bf, hashes, BLOOM_HASHF);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
printf("Testing Bloom filter.\n\n");
|
2015-09-14 10:48:59 +02:00
|
|
|
printf("m: %" PRIu32 " k: %" PRIu32 "\n\n", (uint32_t) bloom.m,
|
|
|
|
(uint32_t) bloom.k);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
2016-02-14 20:04:10 +01:00
|
|
|
random_init(myseed);
|
2014-01-10 16:21:35 +01:00
|
|
|
|
2016-07-05 21:32:44 +02:00
|
|
|
unsigned long t1 = xtimer_now_usec();
|
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;
|
2015-09-14 10:48:59 +02:00
|
|
|
bloom_add(&bloom,
|
2014-01-10 16:21:35 +01:00
|
|
|
(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
|
|
|
|
2016-07-05 21:32:44 +02:00
|
|
|
unsigned long t2 = xtimer_now_usec();
|
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;
|
|
|
|
|
2016-07-05 21:32:44 +02:00
|
|
|
unsigned long t3 = xtimer_now_usec();
|
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;
|
|
|
|
|
2015-09-14 10:48:59 +02:00
|
|
|
if (bloom_check(&bloom,
|
2014-01-10 16:21:35 +01:00
|
|
|
(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
|
|
|
|
2016-07-05 21:32:44 +02:00
|
|
|
unsigned long t4 = xtimer_now_usec();
|
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);
|
|
|
|
|
2015-09-14 10:48:59 +02:00
|
|
|
bloom_del(&bloom);
|
2014-01-10 16:21:35 +01:00
|
|
|
printf("\nAll done!\n");
|
|
|
|
return 0;
|
|
|
|
}
|