use std::io::{self, Write};
use subms::{SubMsPerfHarness, bench_keyed_op, summarize, summary_to_json};
use subms_hyperloglog::HyperLogLog;
const ENTRIES: usize = 50_000;
const SEED: u64 = 0;
const PRECISION: u32 = 14;
fn main() -> io::Result<()> {
let mut h = SubMsPerfHarness::new("hyperloglog-features", "rust");
h.input("entries", &ENTRIES.to_string());
h.input("seed", &SEED.to_string());
let mut hll = HyperLogLog::new(PRECISION);
bench_keyed_op(&mut h, "base_add", ENTRIES, SEED, |key| hll.add(key));
{
let stage = h.stage("base_estimate", ENTRIES);
for _ in 0..ENTRIES {
stage.time(|| {
let _ = hll.estimate();
});
}
}
#[cfg(feature = "sparse")]
{
use subms_hyperloglog::SparseHyperLogLog;
let mut sparse = SparseHyperLogLog::new(PRECISION);
bench_keyed_op(&mut h, "sparse_add", ENTRIES, SEED, |key| sparse.add(key));
let stage = h.stage("sparse_estimate", ENTRIES);
for _ in 0..ENTRIES {
stage.time(|| {
let _ = sparse.estimate();
});
}
}
#[cfg(feature = "union-intersect")]
{
use subms_hyperloglog::{estimate_intersect, estimate_union};
let mut a = HyperLogLog::new(PRECISION);
let mut b = HyperLogLog::new(PRECISION);
for i in 0..ENTRIES {
a.add(&format!("a-{i}"));
b.add(&format!("b-{}", i / 2));
}
{
let stage = h.stage("union", ENTRIES);
for _ in 0..ENTRIES {
stage.time(|| {
let _ = estimate_union(&a, &b);
});
}
}
{
let stage = h.stage("intersect", ENTRIES);
for _ in 0..ENTRIES {
stage.time(|| {
let _ = estimate_intersect(&a, &b);
});
}
}
}
let summary = summarize(&h);
let mut stdout = io::stdout();
summary_to_json(&summary, &mut stdout)?;
writeln!(stdout)?;
Ok(())
}