I am trying to learn memcached, so I installed it on my laptop and fired it up. I also created a simple SQLite db 'mem.sqlite' like so
CREATE TABLE t (id INTEGER PRIMARY KEY, str TEXT)
and filled the table with 20_000 random strings. Then I used the following simplistic script to benchmark.
use Cache::Memcached;
my $memd = new Cache::Memcached {'servers' => [ "localhost:11212" ]};
use DBI qw(:sql_types);
my $dbh = DBI->connect("dbi:SQLite:dbname=mem.sqlite");
use Benchmark qw(:all);
my $count = 40_000;
cmpthese($count, {
'query_dbh' => sub {
my $id = int(rand(20_000));
my $sth = $dbh->prepare("SELECT str FROM t WHERE id = ?");
$sth->execute($id);
my ($str) = $sth->fetchrow_array;
open F, ">", "foo.txt" or die $!;
print F "id: $id, str: $str\n";
close F;
},
'query_mem' => sub {
my $id = int(rand(20_000));
open F, ">", "foo.txt" or die $!;
my $str = $memd->get($id);
unless ($str) {
my $sth = $dbh->prepare("SELECT str FROM t WHERE id = ?");
$sth->execute($id);
($str) = $sth->fetchrow_array;
$memd->set($id, $str);
}
print F "id: $id, str: $str\n";
close F;
}
});
Rate query_mem query_dbf
query_mem 2723/s -- -29%
query_dbh 3846/s 41% --
I consistently get results such as above, while I expected the memcache to slowly fill up and speed up the queries way faster than only accessing the file based db. What am I doing wrong, or are my expectations wrong?
when small people start casting long shadows, it is time to go to bed
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.