File: bench.pl

package info (click to toggle)
libtie-cache-perl 0.17-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 92 kB
  • ctags: 30
  • sloc: perl: 550; makefile: 47
file content (64 lines) | stat: -rwxr-xr-x 1,178 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
#!/usr/bin/perl

use Tie::Cache;
use Tie::Cache::LRU;
use Benchmark;
use strict;

my $cache_size = 5000;
my $write_count = $cache_size * 2;
my $read_count = $write_count * 4;
my $delete_count = $write_count;

tie my %cache, 'Tie::Cache', $cache_size;
tie my %cache_lru, 'Tie::Cache::LRU', $cache_size;

my @cols;
push(@cols, \%cache, \%cache_lru);

printf " %15s", "Cache Size $cache_size";
for(@cols) {
    my $module = ref(tied(%$_));
    printf " %16s %3.2f", $module, eval "\$$module"."::VERSION";
}
print "\n";

&report("$write_count Writes", sub {
	    my $cache = shift;
	    for(1..$write_count) {
		$cache->{$_} = $_;
	    }
	},
	@cols,
	);

&report("$read_count Reads", sub {
	    my $cache = shift;
	    for(1..$read_count) {
		my $value = $cache->{$_};
	    }
	},
	@cols,
	);

&report("$delete_count Deletes", sub {
	    my $cache = shift;
	    for(1..$delete_count) {
		my $value = $cache->{$_};
	    }
	},
	@cols,
	);

sub report {
    my($desc, $sub, @caches) = @_;

    printf(" %-15s", $desc);
    for my $cache (@caches) {
	my $timed = timestr(timeit(1, sub { &$sub($cache) }));
	$timed =~ /([\d\.]+\s+cpu)/i;
	printf("%18s sec", $1);
    }
    print "\n";
}