Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.76 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. package World::Schema;
  4. use base qw/DBIx::Class::Schema::Loader/;
  5. $ENV{SCHEMA_LOADER_BACKCOMPAT} = 1;
  6. my $schema = World::Schema->connect( "DBI:mysql:database=world", "world", "world1",
  7.     { PrintError => 1, RaiseError => 1, mysql_enable_utf8 => 1 } );
  8.  
  9. package main;
  10. use DBIx::Connector;
  11. use DBIx::Struct;
  12. use strict;
  13. use warnings;
  14.  
  15. use Benchmark qw|:all|;
  16.  
  17. # used this world database https://dev.mysql.com/doc/index-other.html
  18.  
  19. DBIx::Struct::connect( "DBI:mysql:database=world", "world", "world1" );
  20. my $connector =
  21.   DBIx::Connector->new( "DBI:mysql:database=world", "world", "world1",
  22.     { PrintError => 1, RaiseError => 1, mysql_enable_utf8 => 1 } );
  23.  
  24. my $tests = {
  25.     Struct => sub {
  26.         my $cities = all_rows(
  27.             [
  28.                 "city s" => -right => "country c" => -on => "s.CountryCode = c.Code" => -left =>
  29.                   "countrylanguage l" => -on => "l.CountryCode = c.Code",
  30.                 -columns => [ 's.Name city', 'c.Name country', 'Language language' ],
  31.             ],
  32.             -where => { Language => 'English' }
  33.         );
  34.     },
  35.     StructFor => sub {
  36.         my $cities = for_rows(
  37.             [
  38.                 "city s" => -right => "country c" => -on => "s.CountryCode = c.Code" => -left =>
  39.                   "countrylanguage l" => -on => "l.CountryCode = c.Code",
  40.                 -columns => [ 's.Name city', 'c.Name country', 'Language language' ],
  41.             ],
  42.             -where => { Language => 'English' },
  43.             sub { 1 }
  44.         );
  45.     },
  46.     DBI => sub {
  47.         my $dbh_cities = $connector->run(
  48.             sub {
  49.                 $_->selectall_arrayref(
  50.                     qq{select s.Name city, c.Name country, Language language from city s}
  51.                       . qq{ right join country c on(s.CountryCode = c.Code)}
  52.                       . qq{ left join countrylanguage l on(l.CountryCode = c.Code)}
  53.                       . qq{  WHERE ( Language = ? )},
  54.                     { Slice => {} },
  55.                     'English'
  56.                 );
  57.             }
  58.         );
  59.     },
  60.     DBIC => sub {
  61.         $schema->resultset('Country')->search(
  62.             { 'countrylanguages.Language' => 'English' },
  63.             {
  64.                 join   => [ 'cities', 'countrylanguages' ],
  65.                 select => [qw(cities.Name me.Name countrylanguages.Language)],
  66.                 as     => [qw(city country language)]
  67.             }
  68.         )->all;
  69.       }
  70. };
  71.  
  72. cmpthese( -5, $tests );
  73.  
  74.  
  75. __END__
  76.            Rate      DBIC    Struct StructFor       DBI
  77. DBIC      123/s        --      -66%      -67%      -80%
  78. Struct    361/s      194%        --       -3%      -40%
  79. StructFor 371/s      202%        3%        --      -38%
  80. DBI       602/s      389%       67%       62%        --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement