Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created June 8, 2012 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazeburo/2894707 to your computer and use it in GitHub Desktop.
Save kazeburo/2894707 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use File::Temp qw/tempfile/;
use Parallel::Benchmark;
for my $n ( 1..4 ) {
(undef ,my $db) = tempfile(OPEN => 0);
my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",{
RaiseError => 1,
AutoCommit => 1,
sqlite_use_immediate_transaction => 1,
});
$dbh->do('create table if not exists foo (id)');
$dbh->do('insert into foo values(?)',{}, 1);
my $bm = Parallel::Benchmark->new(
setup => sub {
my ($self, $id) = @_;
$self->stash->{dbh} = DBI->connect("dbi:SQLite:dbname=$db","","",{
RaiseError => 1,
AutoCommit => 1,
sqlite_use_immediate_transaction => 1,
});
$self->stash->{dbh}->do('PRAGMA journal_mode = WAL') if $n > 1;
$self->stash->{dbh}->do('PRAGMA synchronous = NORMAL') if $n == 3;
$self->stash->{dbh}->do('PRAGMA synchronous = OFF') if $n > 3;
},
benchmark => sub {
my ($self, $id) = @_;
my $dbh = $self->stash->{dbh};
$dbh->begin_work;
my $arrayref = $dbh->selectrow_arrayref('select * from foo limit 1;');
$dbh->do('insert into foo values(?)',{}, $arrayref->[0]++);
$dbh->commit;
return 1;
},
teardown => sub {
my ($self, $id) = @_;
delete $self->stash->{dbh};
},
concurrency => 4,
time => 8,
);
my $result = $bm->run();
}
__END__
#default
2012-06-08T18:29:58 [INFO] starting benchmark: concurrency: 4, time: 8
2012-06-08T18:30:08 [INFO] done benchmark: score 5560, elapsed 8.082 sec = 687.942 / sec
# journal_mode = WAL
2012-06-08T18:30:08 [INFO] starting benchmark: concurrency: 4, time: 8
2012-06-08T18:30:18 [INFO] done benchmark: score 16624, elapsed 8.032 sec = 2069.632 / sec
# journal_mode = WAL + synchronous = NORMAL
2012-06-08T18:30:18 [INFO] starting benchmark: concurrency: 4, time: 8
2012-06-08T18:30:28 [INFO] done benchmark: score 31283, elapsed 8.000 sec = 3910.348 / sec
# journal_mode = WAL + synchronous = OFF
2012-06-08T18:30:28 [INFO] starting benchmark: concurrency: 4, time: 8
2012-06-08T18:30:38 [INFO] done benchmark: score 33307, elapsed 8.003 sec = 4161.728 / sec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment