File: 3.t

package info (click to toggle)
libcache-fastmmap-perl 1.58-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 356 kB
  • sloc: ansic: 1,399; perl: 628; makefile: 7
file content (85 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download | duplicates (4)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

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

use Test::More tests => 8;
BEGIN { use_ok('Cache::FastMmap') };
use strict;

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

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

my $FC = Cache::FastMmap->new(init_file => 1, serializer => '');
ok( defined $FC );

my (@Keys, $HitRate);
$HitRate = RepeatMixTest($FC, 1000, 0.0, \@Keys);
ok( $HitRate == 0.0, "hit rate 1");

$HitRate = RepeatMixTest($FC, 1000, 0.5, \@Keys);
ok( $HitRate == 1.0, "hit rate 2");

$HitRate = RepeatMixTest($FC, 1000, 0.8, \@Keys);
ok( $HitRate == 1.0, "hit rate 3");

$FC = undef;
@Keys = ();

# Should be repeatable
srand(123456);

$FC = Cache::FastMmap->new(
  init_file => 1,
  page_size => 8192,
  serializer => ''
);
ok( defined $FC );

$HitRate = RepeatMixTest($FC, 1000, 0.0, \@Keys);
ok( $HitRate == 0.0, "hit rate 1");
$HitRate = RepeatMixTest($FC, 10000, 0.5, \@Keys);
ok( $HitRate > 0.8 && $HitRate < 0.95, "hit rate 4 - $HitRate");


sub RepeatMixTest {
  my ($FC, $NItems, $Ratio, $WroteKeys) = @_;

  my ($Read, $ReadHit);

  # Lots of random tests
  for (1 .. $NItems) {

    # Read/write ratio
    if (rand() < $Ratio) {

      # Pick a key from known written ones
      my $K = $WroteKeys->[ rand(@$WroteKeys) ];
      my $V = $FC->get($K);
      $Read++;

      # Skip if not found in cache
      next if !defined $V;
      $ReadHit++;

      # Offset of 10 past first chars of value are key
      substr($V, 10, length($K)) eq $K
        || die "Cache/key not equal: $K, $V";

    } else {

      my $K = RandStr(16);
      my $V = RandStr(10) . $K . RandStr(int(rand(200)));
      push @$WroteKeys, $K;
      $FC->set($K, $V);

    }
  }

  return $Read ? ($ReadHit/$Read) : 0.0;
}

sub RandStr {
  return join '', map { chr(ord('a') + rand(26)) } (1 .. $_[0]);
}