1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#########################
use Test::More;
BEGIN {
eval "use JSON (); use Sereal ();";
if ($@) {
plan skip_all => 'No JSON/Sereal, no json/sereal storage tests';
} else {
plan tests => 5;
}
use_ok('Cache::FastMmap');
}
use Time::HiRes qw(time);
use Data::Dumper;
use strict;
#########################
my $FCStorable = Cache::FastMmap->new(serializer => 'storable', init_file => 1);
ok( defined $FCStorable );
my $FCJson = Cache::FastMmap->new(serializer => 'json', init_file => 1);
ok( defined $FCJson );
my $FCSereal = Cache::FastMmap->new(serializer => 'sereal', init_file => 1);
ok( defined $FCSereal );
eval { $FCJson->set("foo2", { key1 => '123abc', key2 => \"bar" }); };
ok( $@ =~ /cannot encode reference to scalar/ );
my $StorableTime = DoTests($FCStorable);
my $JsonTime = DoTests($FCJson);
my $SerealTime = DoTests($FCSereal);
# Lets not assume these everywhere as test breakers
# ok ($StorableTime > $SerealTime, "Sereal faster than storable");
# ok ($StorableTime > $JsonTime, "Json faster than storable");
sub DoTests {
my $FC = shift;
for (1..10000) {
$FC->set("foo$_", { key1 => 'boom', key2 => "woot$_" });
}
my $Start = time;
for (1..10000) {
$FC->set("foo$_", { key1 => '123abc', key2 => "bar$_" });
my $H = $FC->get("foo$_");
keys %$H == 2 || die;
$H->{key1} eq '123abc' || die;
$H->{key2} eq "bar$_" || die;
}
my $End = time;
return $End-$Start;
}
|