Permalink
Browse files

- Using a custom module for Hash persistance

  • Loading branch information...
1 parent eee9751 commit ef6d2a90927d5d285cab20da036a6814d15aa3c2 @steveschnepp steveschnepp committed Jun 28, 2011
Showing with 122 additions and 7 deletions.
  1. +67 −0 common/lib/Munin/Common/DictFile.pm
  2. +55 −7 contrib/perf/db_files.pl
@@ -0,0 +1,67 @@
+package Munin::Common::DictFile;
+require Tie::Hash;
+our @ISA = qw(Tie::StdHash);
+
+our $DEBUG_ENABLED;
+
+# The method invoked by the command tie %hash, classname.
+# Associates a new hash instance with the specified class.
+# LIST would represent additional arguments (along the lines
+# of AnyDBM_File and compatriots) needed to complete the association.
+sub TIEHASH {
+ my $classname = shift;
+
+ my ($filename) = @_;
+
+ my $self = {
+ __filename__ => $filename,
+ };
+
+ # Read the whole file if it exists
+ if (-e $filename) {
+ open(FILE, $filename) or die "Cannot open tied file '$filename' - $!";
+
+ while(my $line = <FILE>) {
+ chomp($line);
+ next unless $line =~ m/^'(.*)':\t'(.*)'$/;
+ # Found a valid line, store it
+ $self->{ _unescape($1) } = _unescape($2);
+ }
+ close(FILE);
+ }
+
+ return bless($self, $classname);
+}
+
+# Write everything down
+sub DESTROY {
+ my $self = shift;
+
+ my $tmp_filename = $self->{__filename__} . ".tmp.$$";
+
+ open(FILE, "> $tmp_filename") or die "Cannot open temp file '$tmp_filename' - $!";
+ foreach my $key (keys %$self) {
+ print FILE "'" . _escape($key) . "':\t'" . _escape($self->{$key}) . "'\n";
+ }
+ close (FILE);
+
+ rename $tmp_filename, $self->{__filename__};
+}
+
+sub DEBUG {
+ print STDOUT "[DEBUG] @_" . "\n" if $DEBUG_ENABLED;
+}
+
+sub _escape {
+ my $string = shift;
+ $string =~ s/'/''/g;
+ return $string;
+}
+
+sub _unescape {
+ my $string = shift;
+ $string =~ s/''/'/g;
+ return $string;
+}
+
+1;
View
@@ -5,11 +5,12 @@
use warnings;
use DB_File;
-use DB_File::Lock;
+use Munin::Common::DictFile;
my $nb_files = (shift || 1000);
my $nb_cmp = (shift || -10);
+print "using db_$$\n";
mkdir "db_$$";
chdir "db_$$";
@@ -41,21 +42,68 @@ sub fl_update {
rename ($tmp_filename, $last_filename);
}
+sub fh_update {
+ my ($filename, $hash) = @_;
+
+ my $last_filename = "$filename.last";
+ my $tmp_filename = "$last_filename.tmp";
+
+ open (FILE, "> $tmp_filename");
+ foreach my $key (keys %$hash) {
+ print FILE $key . "\t" . $hash->{$key} . "\n";
+ }
+ close (FILE);
+
+ rename ($tmp_filename, $last_filename);
+}
+
use Benchmark qw(:all);
use Storable;
+use SDBM_File;
+STDOUT->autoflush(1);
cmpthese($nb_cmp, {
- 'fl_update' => sub { for (my $i = 0; $i < $nb_files; $i ++) { fl_update("test$i.rrd", $i, sin($i)); } },
- 'db_update' => sub { for (my $i = 0; $i < $nb_files; $i ++) { db_update("test$i.rrd", $i, sin($i), $DB_HASH); } },
+ 'fl_update' => sub {
+ print "Starting fl_update: ";
+ for (my $i = 0; $i < $nb_files; $i ++) { fl_update("test$i.rrd", $i, rand($i)); }
+ print "done.\n";
+ },
+ 'fh_update' => sub {
+ print "Starting fh_update: ";
+ my %hash;
+ for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, rand(), \%hash); }
+ fh_update("update.txt", \%hash);
+ print "done.\n";
+ },
'db_upda_c' => sub {
+ print "Starting db_upda_c: ";
my %hash; tie %hash, 'DB_File', "update_c.db", O_CREAT|O_RDWR, 0666, $DB_HASH;
- for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, sin($i), \%hash); }
+ for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, rand(), \%hash); }
untie %hash;
+ print "done.\n";
+ },
+ 'sdbm' => sub {
+ print "Starting sdbm: ";
+ my %hash; tie %hash, 'SDBM_File', "update_sdbm.db", O_CREAT|O_RDWR, 0666;
+ for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, rand(), \%hash); }
+ untie %hash;
+ print "done.\n";
+ },
+ 'dictfile' => sub {
+ print "Starting dictfile: ";
+ my %hash; tie %hash, 'Munin::Common::DictFile', "update_munin_dict.txt", O_CREAT|O_RDWR, 0666;
+ for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, rand(), \%hash); }
+ untie %hash;
+ %hash = ();
+ print "done.\n";
},
'storable' => sub {
- my %hash;
- for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, sin($i), \%hash); }
- Storable::nstore(\%hash, "update.storable");
+ print "Starting storable: ";
+ my $hash = ( -e "update.storable" ) ? Storable::retrieve("update.storable") : {};
+ for (my $i = 0; $i < $nb_files; $i ++) { db_update_conn("test$i.rrd", $i, rand(), $hash); }
+ Storable::nstore($hash, "update.storable.$$");
+ rename("update.storable.$$", "update.storable");
+ print "done.\n";
},
});

0 comments on commit ef6d2a9

Please sign in to comment.