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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
#!/opt/blocperl/bin/perl -w
#
# This is a stripped down version of cacheperl.pl from
# http://cpan.robm.fastmail.fm/cache_perf.html that just compares
# Cache::BDB, Cache::BerkeleyDB, Cache::FileCache, and
# Cache::Memcached. See the included patch to add Cache::BDB to the
# version of cacheperl.pl available from that site if you want to
# benchmark a more complete set of options. If you want to disable a
# few of these, see the @Packages array below and just comment out the
# ones you don't want run.
#
use Time::HiRes qw(gettimeofday tv_interval);
use Storable qw(freeze thaw);
use Data::Dumper;
use strict;
#----- Setup stuff
srand(1);
# Number of runs to perform
my $Runs = 1;
# Maximum number of values to generate to store in cache
my $MaxVals = 1000;
# Number of times to get/set in each run
my $NSetItems = 500;
my $NGetItems = 500;
my $NMixItems = 500;
# When getting, pick definitely stored keys this often
my $TestHitRate = 0.85;
# If mix mode, make reads this often
my $MixReadRatio = 0.85;
# Build data sets of various complexity
my (@DataComplex, @DataBin);
for my $Depth (0 .. 2) {
my @Structs = map { BuildStruct($Depth, $Depth+5) } 1 .. $MaxVals;
push @DataComplex, \@Structs;
my @Frozen = map { freeze($_) } @Structs;
push @DataBin, \@Frozen;
}
# Recursive helper call
sub BuildStruct {
my ($Depth, $NItems) = @_;
my $Struct = {};
# Alter slightly from base number of items passed
$NItems += int(rand(3))-1;
# Generate given number of items in hash struct
for (my $i = 0; $i < $NItems; $i++) {
my $Key = RandVal(1);
my $Type = int(rand(10));
if ($Type < 4) {
$Struct->{$Key} = RandVal();
} elsif ($Type < 7) {
$Struct->{$Key} = [ map { RandVal() } 1 .. int(rand($NItems)) ];
} else {
$Struct->{$Key} = $Depth ? BuildStruct($Depth-1, $NItems) : RandVal();
}
}
return $Struct;
}
# Generate random perl value (either int, float, string or undef)
sub RandVal {
my $NotUndef = shift;
my $Type = int(rand(10));
if ($Type < 3) {
return rand(100);
} elsif ($Type < 6) {
return int(rand(1000000));
} elsif ($Type < 9 || $NotUndef) {
return join '', map { chr(ord('A') + int(rand(26))) } 1 .. int(rand(20));
} else {
return undef;
}
}
sub CheckComplex {
keys %{$_[0]} == keys %{$_[1]}
|| die "Mismatch for package - " . $_[2];
}
sub CheckBin {
$_[0] eq $_[1]
|| die "Mismatch for package - " . $_[2];
}
# Packages to run through
my @Packages = (
CC5_CacheCacheBerkeleyDBStorable => [ 'complex',
{
namespace => 'testcache_cache_cache_berkeleydb',
cache_root => '/tmp'
}
],
CC5_CacheFileCacheStorable => [ 'complex',
{
cache_root => "/tmp",
namespace => "testcache_filecache",
}
],
CC6_CacheFileStorable => [ 'complex',
cache_root => "/tmp",
namespace => "testcache_file",
],
CC6_CacheBDBStorable => [ 'complex',
cache_root => "/tmp",
namespace => "testcache_cache_bdb",
],
CC3_Memcached_Local => [ 'complex', {
'servers' => [ "localhost:11211",],
'debug' => 0,
'compress_threshold' => 100000,
}
],
);
#----- Now do runs
# Repeat each package type
while (my ($Package, $PackageOpts) = splice @Packages, 0, 2) {
# eval { require $Package };#
# print $@;
# next if $@;
# Get package options
my ($DataType, @Params) = @$PackageOpts;
my ($Check, $Data);
# Set data and check routine based on data type
if ($DataType eq 'bin') {
$Check = \&CheckBin;
$Data = \@DataBin;
} else {
$Check = \&CheckComplex;
$Data = \@DataComplex;
}
my $Name = $Package->name();
print "\nPackage: $Name\nData type: $DataType\nParams: @Params\n";
printf(" %5s | %6s | %6s | %6s | %5s | %5s\n", qw(Cmplx Set/S Get/S Mix/S GHitR MHitR));
printf("-------|--------|--------|--------|-------|------\n");
# Run for each data set size
for my $DataSet (@$Data) {
# Basic data complexity metric
my $Complexity;
for (@$DataSet) {
if (ref $_) {
my @Hashes = $_;
while (my $Hash = shift @Hashes) {
$Complexity += keys %$Hash;
push @Hashes, grep { ref($_) eq 'HASH' } values %$Hash;
}
} else {
$Complexity += length($_);
}
}
$Complexity /= scalar(@$DataSet);
# Store times
my ($SetTime, $GetTime, $MixTime, $Name);
# And hit rate
my (%StoreData, $GetRead, $GetHit, $MixRead, $MixHit);
# Do runs
for (my $Run = 0; $Run < $Runs; $Run++) {
my $c = $Package->new(@Params);
# Store keys
my $t0 = [gettimeofday];
for (my $i = 0; $i < $NSetItems; $i++) {
my $k = "abc" . ($i * 103) . "defg";
my $x = $i % $MaxVals;
$c->set($k, $DataSet->[$x]);
$StoreData{$k} = $x;
}
my $t1 = [gettimeofday];
my @SetKeys = keys %StoreData;
# Get keys
for (my $i = $NGetItems-1; $i >= 0; $i--) {
my $k;
if (rand() < $TestHitRate) {
$k = $SetKeys[rand(@SetKeys)];
$GetRead++;
} else {
$k = "abcd" . ($i * 103) . "efg";
}
my $y = $c->get($k);
if (defined $y) {
$GetHit++;
} else {
my $o = $StoreData{$k};
defined $o || next;
$y = $DataSet->[$o];
}
# Reality check, not much of a check...
$Check->($y, $DataSet->[$StoreData{$k}]);
}
my $t2 = [gettimeofday];
# Now do mix
for (my $i = 0; $i < $NMixItems; $i++) {
my $k;
if (rand() < $MixReadRatio) {
if (rand() < $TestHitRate) {
$k = $SetKeys[rand(@SetKeys)];
$MixRead++;
} else {
$k = "abcd" . ($i * 103) . "efg";
}
my $y = $c->get($k);
if (defined $y) {
$MixHit++;
} else {
my $o = $StoreData{$k};
defined $o || next;
$y = $DataSet->[$o];
}
# Reality check, not much of a check...
$Check->($y, $DataSet->[$StoreData{$k}]);
} else {
$k = $SetKeys[rand(@SetKeys)];
$c->set($k, $DataSet->[$StoreData{$k}]);
}
}
my $t3 = [gettimeofday];
# Add to run times
$SetTime += tv_interval($t0, $t1);
$GetTime += tv_interval($t1, $t2);
$MixTime += tv_interval($t2, $t3);
}
my $SetRate = int ($NSetItems*$Runs / $SetTime);
my $GetRate = int ($NGetItems*$Runs / $GetTime);
my $MixRate = int ($NMixItems*$Runs / $MixTime);
my $GHitRate = $GetHit/$GetRead;
my $MHitRate = $MixHit/$MixRead;
printf(" %5d | %6d | %6d | %6d | %5.3f | %5.3f\n",
$Complexity, $SetRate, $GetRate, $MixRate, $GHitRate, $MHitRate);
}
print "\n";
}
exit(0);
package CB0_InProcHash;
sub name { return "In process hash"; }
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
my $Self = {};
bless ($Self, $Class);
return $Self;
}
sub set {
$_[0]->{$_[1]} = $_[2];
}
sub get {
return $_[0]->{$_[1]};
}
1;
package CC0_InProcHashStorable;
use Storable qw(freeze thaw);
sub name { return "Storable freeze/thaw"; }
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
my $Self = {};
bless ($Self, $Class);
return $Self;
}
sub set {
$_[0]->{$_[1]} = freeze($_[2]);
}
sub get {
return thaw($_[0]->{$_[1]});
}
1;
package CC3_Memcached_Local;
use Cache::Memcached;
use base 'Cache::Memcached';
sub name { return "Memcached Local Storable"; }
1;
package CC3_BerkeleyDB_Hash_Storable;
use Storable qw(freeze thaw);
use BerkeleyDB;
use Fcntl qw(:DEFAULT);
sub name { return "BerkeleyDB Hash Storable"; }
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
unlink glob('/tmp/bdbfile*');
my %Cache;
my $env = new BerkeleyDB::Env(
-Home => '/tmp',
-Flags => DB_INIT_CDB | DB_CREATE | DB_INIT_MPOOL,
#-Cachesize => 23152000,
)
or die "can't create BerkelyDB::Env: $!";
my $Obj = tie %Cache, 'BerkeleyDB::Hash',
-Filename => '/tmp/bdbfile',
-Flags => DB_CREATE,
-Mode => 0640,
-Env => $env
or die ("Can't tie to /tmp/bdbdfile: $!");
my $Self = {
Cache => \%Cache,
Obj => $Obj
};
bless ($Self, $Class);
return $Self;
}
sub set {
# $_[0]->{Cache}->{$_[1]} = freeze($_[2]);
$_[0]->{Obj}->db_put( $_[1], freeze($_[2]) );
}
sub get {
# return thaw($_[0]->{Cache}->{$_[1]});
my $value;
$_[0]->{Obj}->db_get( $_[1], $value );
return thaw( $value );
}
1;
package CC3_BerkeleyDB_Btree_Storable;
use Storable qw(freeze thaw);
use BerkeleyDB;
use Fcntl qw(:DEFAULT);
sub name { return "BerkeleyDB Btree Storable"; }
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
unlink glob('/tmp/bdbfile*');
my %Cache;
my $env = new BerkeleyDB::Env(
-Home => '/tmp',
-Flags => DB_INIT_CDB | DB_CREATE | DB_INIT_MPOOL,
#-Cachesize => 23152000,
)
or die "can't create BerkelyDB::Env: $!";
my $Obj = tie %Cache, 'BerkeleyDB::Btree',
-Filename => '/tmp/bdbfile',
-Flags => DB_CREATE,
-Mode => 0640,
-Env => $env
or die ("Can't tie to /tmp/bdbdfile: $!");
my $Self = {
Cache => \%Cache,
Obj => $Obj
};
bless ($Self, $Class);
return $Self;
}
sub set {
$_[0]->{Obj}->db_put( $_[1], freeze($_[2]) );
}
sub get {
my $value;
$_[0]->{Obj}->db_get( $_[1], $value );
return thaw( $value );
}
1;
package CC3_BerkeleyDB_Hash;
use BerkeleyDB;
use Fcntl qw(:DEFAULT);
sub name { return "BerkeleyDB Hash"; }
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
unlink glob('/tmp/bdbfile*');
my %Cache;
my $env = new BerkeleyDB::Env(
-Home => '/tmp',
-Flags => DB_INIT_CDB | DB_CREATE | DB_INIT_MPOOL,
#-Cachesize => 23152000,
)
or die "can't create BerkelyDB::Env: $!";
my $Obj = tie %Cache, 'BerkeleyDB::Hash',
-Filename => '/tmp/bdbfile',
-Flags => DB_CREATE,
-Mode => 0640,
-Env => $env
or die ("Can't tie to /tmp/bdbdfile: $!");
my $Self = {
Cache => \%Cache,
Obj => $Obj
};
bless ($Self, $Class);
return $Self;
}
sub set {
# $_[0]->{Obj}->db_put( $_[1], $_[2] );
$_[0]->{Cache}->{ $_[1]} = $_[2];
}
sub get {
return $_[0]->{Cache}->{$_[1]};
my $value;
$_[0]->{Obj}->db_get( $_[1], $value );
return $value;
}
package CC3_BerkeleyDB_Btree;
use BerkeleyDB;
use Fcntl qw(:DEFAULT);
sub name { return "BerkeleyDB Btree"; }
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
unlink glob('/tmp/bdbfile*');
my %Cache;
my $env = new BerkeleyDB::Env(
-Home => '/tmp',
-Flags => DB_INIT_CDB | DB_CREATE | DB_INIT_MPOOL,
#-Cachesize => 23152000,
)
or die "can't create BerkelyDB::Env: $!";
my $Obj = tie %Cache, 'BerkeleyDB::Btree',
-Filename => '/tmp/bdbfile',
-Flags => DB_CREATE,
-Mode => 0640,
-Env => $env
or die ("Can't tie to /tmp/bdbdfile: $!");
my $Self = {
Cache => \%Cache,
Obj => $Obj
};
bless ($Self, $Class);
return $Self;
}
sub set {
$_[0]->{Obj}->db_put( $_[1], $_[2] );
}
sub get {
my $value;
$_[0]->{Obj}->db_get( $_[1], $value );
return $value;
}
1;
package CC5_CacheCacheBerkeleyDBStorable;
use Cache::BerkeleyDB;
use base 'Cache::BerkeleyDB';
sub name { return "Cache::BerkeleyDB Storable"; }
1;
package CC5_CacheFileCacheStorable;
use Cache::FileCache;
use base 'Cache::FileCache';
sub name { return "Cache::FileCache Storable"; }
1;
package CC6_CacheBDBStorable;
use Cache::BDB;
use base 'Cache::BDB';
sub name { return "Cache::BDB Storable"; }
1;
package CC6_CacheFileStorable;
use Cache::File;
use base 'Cache::File';
sub name { return "Cache::File Storable"; }
sub get { shift->thaw(@_); }
sub set { shift->freeze(@_); }
1;
|