MiniDatabase / exp_bloom.cpp
exp_bloom.cpp
Raw
// Tests for bloom filter performance.

#include "main.h"
#include <assert.h>
#include <iostream> 
#include <filesystem>
#include <limits.h>
#include <chrono>

#define BIN_SEARCH 0
#define BTREE_SEARCH 1

#define MEMTABLE_SIZE 1
#define MAX_EXPERIMENT_SIZE 1024
#define BUFFER_SIZE 10
#define BLOOMFILTER_BITS 5

double bloom_test() {
    std::cout << "Running Bloom Filter experiments. \n";

    std::filesystem::remove_all("db_t");

    int data_volume_mb = 64;
    int mem_size_mb = MEMTABLE_SIZE;
    int mem_size = (mem_size_mb * 1024 * 1024) / (2 * sizeof(int));
    int buffer_size = BUFFER_SIZE * 256; // assuming 4KB pages
    DB* db = Open("db_t", mem_size, buffer_size, buffer_size, BTREE_SEARCH, BLOOMFILTER_BITS);

    int num_kv = (data_volume_mb * 1024 * 1024) / (2 * sizeof(int));  //8,388,608

    std::cout << "Populating the database...\n";
    for (int i = 0; i < num_kv; i++){
        db->put(i,-i);
    }
    std::cout << "Database populated, starting test...\n";

    auto real_start_deep = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 100; i++){
        db->get(i);  // get from the deepest level
    }
    auto real_end_deep = std::chrono::high_resolution_clock::now();

    auto real_start_filter = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 100; i++){
        db->get(8388610 + i);  // get non-existing values
    }
    auto real_end_filter = std::chrono::high_resolution_clock::now();

    double real_time_taken_deep = (std::chrono::duration_cast<std::chrono::microseconds>(real_end_deep - real_start_deep).count())/1000000.0;
    std::cout << "Getting from the deepest level. Time taken: " << real_time_taken_deep << " seconds \n";

    double real_time_taken = (std::chrono::duration_cast<std::chrono::microseconds>(real_end_filter - real_start_filter).count())/1000000.0;
    std::cout << "Getting non-existing values, should be filtered. Time taken: " << real_time_taken << " seconds \n";

    db->Close();

    // std::cout << "All tests ended\n\n";
    std::filesystem::remove_all("db_t");
    return real_time_taken;
}

int main(){
    bloom_test();
    return 0;
}