Cache-FastMmap

 view release on metacpan or  search on metacpan

t/6.t  view on Meta::CPAN


#########################

use Test::More;

my $GetMem;

BEGIN {
  eval "use GTop ();";
  if (!$@) {
    my $GTop = GTop->new;
    $GetMem = sub { return $GTop->proc_mem($$)->size };
  } elsif (-f "/proc/$$/status") {
    $GetMem = sub { open(my $Sh, "/proc/$$/status"); my ($S) = map { /(\d+) kB/ && $1*1024 } grep { /^VmSize:/ } <$Sh>; close($Sh); return $S; }
  }
  if ($GetMem) {
    plan tests => 10;
  } else {
    plan skip_all => 'No GTop or /proc/, no memory leak tests';
  }
  use_ok('Cache::FastMmap');
}

use strict;

#########################

# Memory-leak detection. Repeats new()/destroy() and a tight churn loop
# of get/set/remove with read/write/delete callbacks, sampling RSS via
# GTop or /proc/$$/status; process size must not grow across runs.
# Skipped if neither is available.

our ($DidRead, $DidWrite, $DidDelete, $HitCount);

our $FC;
$FC = Cache::FastMmap->new(init_file => 0, serializer => '');
$FC = undef;

TestLeak(\&NewLeak, "new - 1");
TestLeak(\&NewLeak, "new - 2");
TestLeak(\&NewLeak2, "new2 - 1");
TestLeak(\&NewLeak2, "new2 - 2");

$FC = Cache::FastMmap->new(
  init_file => 1,
  serializer => '',
  num_pages => 17,
  page_size => 65536,
  read_cb => sub { $DidRead++; return undef; },
  write_cb => sub { $DidWrite++; },
  delete_cb => sub { $DidDelete++; },
  write_action => 'write_back'
);

ok( defined $FC );

# Prefill cache to make sure all pages mapped
for (1 .. 10000) {
  $FC->set(RandStr(20), RandStr(20));
}
$FC->get('foo');

our $Key = "blah" x 100;
our $Val = "\x{263A}" . RandStr(1000);

our $IterCount = 100;

our $StartKey = 1;
SetLeak();
$StartKey = 1;
GetLeak();

our $IterCount = 20000;

$StartKey = 1;
TestLeak(\&SetLeak, "set");

$StartKey = 1;
TestLeak(\&GetLeak, "get");

$FC->clear();

$StartKey = 1;
TestLeak(\&SetLeak, "set2");

our (@a, @b, @c);
@a = $FC->get_keys(0);
@b = $FC->get_keys(1);
@c = $FC->get_keys(2);



( run in 0.599 second using v1.01-cache-2.11-cpan-140bd7fdf52 )