Data-TopK-Shared
view release on metacpan or search on metacpan
t/02-decayed.t view on Meta::CPAN
cmp_ok $t->estimate("new"), '>', $t->estimate("old"),
'recent key (fewer adds) outranks an old key (more adds)';
is +($t->top(1))[0]{key}, "new", 'top-1 is the recent key';
}
# ---- timestamps are monotonic: an out-of-order (past) timestamp does not rewind ----
{
my $t = Data::TopK::Shared->new_decayed(undef, 4, 16, 10);
$t->add("a", 1000);
my $e1 = $t->estimate("a");
$t->add("a", 5); # far in the past -> now stays at 1000
my $e2 = $t->estimate("a");
cmp_ok $e2, '>', $e1, 'a past-timestamp add still increases the (current) estimate';
cmp_ok $e2, '<', 2.001, 'and does not blow up (now did not rewind)';
}
# ---- rescale keeps weights bounded over a long horizon ----
{
my $H = 5;
my $t = Data::TopK::Shared->new_decayed(undef, 4, 16, $H);
$t->add("z", $_ * 10) for 1 .. 3000; # times to 30000, many landmark rescales
my $e = $t->estimate("z");
ok $e == $e && $e < 1e12, 'estimate stays finite and bounded after many rescales';
# steady state for a fixed-interval repeated key: 1/(1 - exp(-alpha*dt))
my $expect = 1 / (1 - exp(-($LN2/$H) * 10));
cmp_ok abs($e - $expect), '<', 1e-6 * $expect, 'converges to the analytic steady state';
}
# ---- no-timestamp mode: time advances one tick per add ----
{
my $t = Data::TopK::Shared->new_decayed(undef, 4, 16, 4);
$t->add("x") for 1 .. 20; # older
$t->add("y") for 1 .. 5; # newer
cmp_ok $t->estimate("y"), '>', 0, 'newer key has a positive estimate';
cmp_ok $t->estimate("x"), '>', 0, 'older key still tracked';
# y's 5 recent adds vs x's 20 decayed-away adds
ok defined +($t->top(1))[0]{key}, 'top works in no-timestamp decayed mode';
}
# ---- persistence: a reopened decayed tree keeps its mode + decayed values ----
{
my $path = "/tmp/topk-decayed-$$-" . int(rand 1e6) . ".shm";
unlink $path;
{
my $w = Data::TopK::Shared->new_decayed($path, 8, 16, 50);
$w->add("p", $_) for 1 .. 40;
$w->sync;
}
{
my $r = Data::TopK::Shared->new_decayed($path, 1, 1, 1); # args ignored on reopen
ok $r->is_decayed, 'reopened tree is still decayed';
cmp_ok abs($r->half_life - 50), '<', 1e-9, 'reopen: half_life persisted';
cmp_ok $r->estimate("p"), '>', 0, 'reopen: decayed value persisted';
}
unlink $path;
}
# ---- regression: a non-finite timestamp must not brick the summary ----
{
my $t = Data::TopK::Shared->new_decayed(undef, 8, 16, 10);
my $inf = "Inf" + 0; my $nan = "NaN" + 0; # NOT 9e999 (finite on -Duselongdouble)
$t->add("a", 5);
$t->add("a", $inf); # Inf timestamp -> ignored (per-add tick), not a poison
$t->add("a", $nan); # NaN timestamp -> ignored
my $e = $t->estimate("a");
ok $e == $e && $e < 1e9, 'Inf/NaN timestamp does not NaN-brick the estimate';
$t->add("b", 10);
ok $t->estimate("b") == $t->estimate("b"), 'summary still usable after a non-finite timestamp';
}
# ---- regression: a huge time gap fully decays old weight (no g=Inf stale weights) ----
{
my $t = Data::TopK::Shared->new_decayed(undef, 8, 16, 10); # half-life 10
$t->add("old", $_) for 100, 101, 102;
$t->add("new", 200000); # gap far past the exp-overflow point -> old must decay to ~0
cmp_ok $t->estimate("old"), '<', 1e-6, 'old weight fully decays across a huge gap';
cmp_ok $t->estimate("new"), '>', $t->estimate("old"), 'new dominates after the gap';
is +($t->top(1))[0]{key}, "new", 'top-1 is the recent key after a huge gap';
my $u = Data::TopK::Shared->new_decayed(undef, 4, 16, 1e-4); # alpha huge: each tick overflows g
$u->add("z") for 1 .. 10;
my $ez = $u->estimate("z");
ok $ez == $ez && abs($ez - 1) < 0.5, 'tiny half-life: estimate stays ~1 (no stale accumulation)';
}
{
my $t = Data::TopK::Shared->new_decayed_memfd('clear-decay', 4, 16, 10);
my $peer = Data::TopK::Shared->new_from_fd($t->memfd);
$t->add('old', 1000);
$t->clear;
is $peer->seen, 0, 'decayed clear resets observations for every handle';
is $peer->estimate('old'), 0, 'decayed clear forgets old weights';
$peer->add('a', 10);
$t->add('b', 20);
cmp_ok abs($peer->estimate('a') - 0.5), '<', 1e-9,
'clear allows a fresh timestamp stream to decay normally';
is $t->estimate('b'), 1, 'new stream starts with unit weight';
$t->clear;
$t->add_many(['a', 'b']);
cmp_ok abs($t->estimate('a') - 2 ** (-1 / 10)), '<', 1e-9,
'implicit ticks also work after decayed clear';
}
done_testing;
( run in 1.336 second using v1.01-cache-2.11-cpan-60884a8df76 )