Skip to content

Instantly share code, notes, and snippets.

@mackee
Last active December 17, 2015 20:38
Show Gist options
  • Save mackee/5668673 to your computer and use it in GitHub Desktop.
Save mackee/5668673 to your computer and use it in GitHub Desktop.
Nornal VS Ramdisk datadir on Test::mysqld
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Benchmark qw/cmpthese/;
use Test::mysqld;
use DBI;
use Sys::Ramdisk;
use feature qw/say/;
my $mysqld =
Test::mysqld->new(
my_cnf => {
'skip-networking' => '',
},
);
my $dbh = DBI->connect($mysqld->dsn(dbname => 'test'));
my $ramdisk = Sys::Ramdisk->new(size => '100m');
$ramdisk->mount();
my $mysqld_inmemory =
Test::mysqld->new(
my_cnf => {
'skip-networking' => '',
datadir => $ramdisk->dir(),
}
);
my $dbh_inmemory = DBI->connect($mysqld_inmemory->dsn(dbname => 'test'));
my $sql = <<"SQL";
CREATE TABLE IF NOT EXISTS `sample_table` (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
SQL
$dbh->do($sql);
$dbh_inmemory->do($sql);
my $sth = $dbh->prepare('INSERT INTO sample_table (`name`) VALUES (?);');
my $sth_inmemory = $dbh->prepare('INSERT INTO sample_table (`name`) VALUES (?);');
cmpthese(100000, {
'basic' => sub {
my $rand_name = '';
$rand_name .= ['A'..'Z', 'a'..'z']->[int(rand(42))] for (1..16);
$sth->execute($rand_name);
my $id = $dbh->last_insert_id('test', 'test', 'sample_table', 'id');
$dbh->selectrow_array('SELECT id, name FROM sample_table WHERE id = ?', undef, $id);
},
'inmemory' => sub {
my $rand_name = '';
$rand_name .= ['A'..'Z', 'a'..'z']->[int(rand(42))] for (1..16);
$sth_inmemory->execute($rand_name);
my $id = $dbh_inmemory->last_insert_id('test', 'test', 'sample_table', 'id');
$dbh_inmemory->selectrow_array('SELECT id, name FROM sample_table WHERE id = ?', undef, $id);
},
});
END {
$ramdisk->unmount();
}
__DATA__
$ perl benchmark.pl [perl:5.16.3][~/develop/Test-mysqld-inmemory]
Rate inmemory basic
inmemory 3418/s -- -0%
basic 3431/s 0% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment