Skip to content

Instantly share code, notes, and snippets.

@knutov
Forked from jef-sure/dbtest.pl
Created July 3, 2019 01:11
Show Gist options
  • Save knutov/3b2df7900e6da61ad8aa1538b4c0e75d to your computer and use it in GitHub Desktop.
Save knutov/3b2df7900e6da61ad8aa1538b4c0e75d to your computer and use it in GitHub Desktop.

Revisions

  1. Anton Petrusevich created this gist Oct 18, 2017.
    81 changes: 81 additions & 0 deletions dbtest.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    #!/usr/bin/perl
    package World::Schema;
    use base qw/DBIx::Class::Schema::Loader/;
    $ENV{SCHEMA_LOADER_BACKCOMPAT} = 1;
    my $schema = World::Schema->connect("DBI:mysql:database=world", "world", "world1",
    {PrintError => 1, RaiseError => 1, mysql_enable_utf8 => 1});

    package main;
    use DBIx::Connector;
    use DBIx::Struct;
    use strict;
    use warnings;

    use Benchmark qw|:all|;

    # used this world database https://dev.mysql.com/doc/index-other.html

    DBIx::Struct::connect("DBI:mysql:database=world", "world", "world1");
    my $connector = DBIx::Connector->new("DBI:mysql:database=world", "world", "world1",
    {PrintError => 1, RaiseError => 1, mysql_enable_utf8 => 1});

    my $tests = {
    Struct => sub {
    my $cities = all_rows(
    [ "city s" => -right => "country c" => -left => "countrylanguage l",
    -columns => ['s.Name city', 'c.Name country', 'Language language'],
    ],
    -where => {Language => 'English'}
    );
    },
    StructFor => sub {
    my $cities = for_rows(
    [ "city s" => -right => "country c" => -left => "countrylanguage l",
    -columns => ['s.Name city', 'c.Name country', 'Language language'],
    ],
    -where => {Language => 'English'},
    sub {1}
    );
    },
    DBI => sub {
    my $dbh_cities = $connector->run(
    sub {
    $_->selectall_arrayref(
    qq{select s.Name city, c.Name country, Language language from city s}
    . qq{ right join country c on(s.CountryCode = c.Code)}
    . qq{ left join countrylanguage l on(l.CountryCode = c.Code)}
    . qq{ WHERE ( Language = ? )},
    {Slice => {}},
    'English'
    );
    }
    );
    },
    DBIC => sub {
    $schema->resultset('Country')->search(
    {'countrylanguages.Language' => 'English'},
    { join => ['cities', 'countrylanguages'],
    select => [qw(cities.Name me.Name countrylanguages.Language)],
    as => [qw(city country language)]
    }
    )->all;
    }
    };

    cmpthese(-5, $tests);

    __END__
    Rate DBIC Struct StructFor DBI
    DBIC 123/s -- -66% -67% -80%
    Struct 361/s 194% -- -3% -40%
    StructFor 371/s 202% 3% -- -38%
    DBI 602/s 389% 67% 62% --
    another computer:
    Rate DBIC StructFor Struct DBI
    DBIC 119/s -- -60% -64% -77%
    StructFor 295/s 148% -- -9% -44%
    Struct 325/s 174% 10% -- -38%
    DBI 526/s 343% 78% 62% --