Changeset 15437

Show
Ignore:
Timestamp:
07/08/08 11:20:49 (5 years ago)
Author:
daisuke
Message:

Add conditional benchmark

Location:
lang/perl/Queue-Q4M/trunk/misc/lib/Queue/Q4M
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Queue-Q4M/trunk/misc/lib/Queue/Q4M/Benchmark.pm

    r15435 r15437  
    4141 
    4242has '__dbh' => ( 
    43     accesor => 'dbh', 
     43    accessor => 'dbh', 
    4444    is => 'rw', 
    4545    isa => 'Maybe[DBI::db]', 
    46     around => sub { 
    47         my ($next, $self, @args) = @_; 
     46); 
    4847 
    49         my $rv = $next->($self, @args); 
    50         if (! @args) { 
    51             if (! defined $rv || ! $rv->ping) { 
    52                 $rv = DBI->connect( $self->connect_info ); 
    53                 $self->dbh($rv); 
    54             } 
     48around 'dbh' => sub { 
     49    my ($next, $self, @args) = @_; 
     50    my $rv = $next->($self, @args); 
     51    if (! @args) { 
     52        if (! defined $rv || ! $rv->ping) { 
     53            $rv = DBI->connect( $self->connect_info ); 
     54            $self->dbh($rv); 
    5555        } 
    56         return $rv; 
    57     }, 
    58 ); 
     56    } 
     57    return $rv; 
     58}; 
    5959 
    6060role_type 'Queue::Q4M::Benchmark::Plugin'; 
     
    6767            foreach my $class (@$_) { 
    6868                if ($class !~ s/^\+//) { 
    69                     $class = "Queue::Q4M::Benchmark::Plugin::$class"; 
     69                    $class = "Queue::Q4M::Benchmark::Plugin::" . ucfirst $class; 
    7070                } 
    7171                Class::MOP::load_class($class); 
     
    8585 
    8686has '__tasks' => ( 
    87     accessors => 'tasks', 
     87    accessor => 'tasks', 
    8888    is => 'rw', 
    8989    isa => 'HashRef', 
    9090    default => sub { +{} } 
     91); 
     92 
     93has 'items' => ( 
     94    is => 'rw', 
     95    isa => 'Int', 
     96    default => 10_000 
    9197); 
    9298 
     
    96102    required => 1, 
    97103    default => 1, 
     104); 
     105 
     106has 'define' => ( 
     107    is => 'rw', 
     108    isa => 'HashRef', 
     109    default => sub { +{} } 
    98110); 
    99111 
     
    119131} 
    120132 
     133sub add_task { 
     134    my ($self, %args) = @_; 
     135 
     136    $self->tasks->{$args{name}} = $args{coderef}; 
     137} 
     138 
    121139sub run_tasks { 
    122140    my $self = shift; 
    123141 
    124     Benchmark::cmpthese( 
    125         $self->iterations, 
    126         $self->tasks 
    127     ); 
     142    while (my ($name, $coderef) = each %{ $self->tasks }) { 
     143        print ">> executing $name\n"; 
     144        $coderef->(); 
     145    } 
     146} 
     147 
     148my @CHARS = ('a'..'z',0..9, 'A'..'Z'); 
     149 
     150sub random_string { 
     151    my ($self, $length) = @_; 
     152    join('', map { $CHARS[rand @CHARS] } 1..$length); 
    128153} 
    129154 
  • lang/perl/Queue-Q4M/trunk/misc/lib/Queue/Q4M/Benchmark/Plugin/Default.pm

    r15435 r15437  
    33package Queue::Q4M::Benchmark::Plugin::Default; 
    44use Moose; 
     5use Time::HiRes qw(time); 
    56 
    67with 'Queue::Q4M::Benchmark::Plugin'; 
     8 
     9has 'table' => ( 
     10    is => 'rw', 
     11    isa => 'Str', 
     12    required => 1, 
     13    default => 'q4mbench_default' 
     14); 
    715 
    816no Moose; 
     
    1220    my ($self, $c) = @_; 
    1321 
     22    my $table = $self->table; 
     23    my $dbh = $c->dbh; 
     24    $dbh->do(<<EOSQL); 
     25        CREATE TABLE IF NOT EXISTS $table ( 
     26            data TEXT NOT NULL 
     27        ) ENGINE=queue; 
     28EOSQL 
     29    $dbh->do("DELETE FROM $table"); 
     30 
     31    print "populating $table with ", $c->items, " items\n"; 
     32    my $max = $c->items; 
     33    my $i = 0; 
     34    while ($max > $i) { 
     35        $i++; 
     36        $dbh->do("INSERT INTO $table (data) VALUES (?)", undef, 
     37            $c->random_string(64)); 
     38        print " + $i\n" if $i % 100 == 0; 
     39    } 
     40 
    1441    $c->add_task( 
    1542        name => 'default', 
    1643        coderef => sub { 
    17             my $queue = Queue::Q4M->new( 
     44            my $queue = Queue::Q4M->connect( 
    1845                connect_info => [ $c->connect_info ], 
    1946            ); 
    2047 
    21             while ( $queue->next ) { 
    22                 my $h = $queue->fetchrow_hashref; 
     48            my $start = time(); 
     49            print " + Start ", scalar(localtime($start)), "\n"; 
     50            my $count = 0; 
     51            while ( $queue->next($table, 1) ) { 
     52                my $h = $queue->fetch_hashref; 
     53                $count++; 
    2354            } 
     55            my $end      = time(); 
     56            my $duration = $end - $start; 
     57            my $avg      = $duration / $count; 
     58            print " + End ", scalar(localtime($end)), "\n"; 
     59            print " + Processed $count messages in ", $duration, " secs, average $avg mess/sec\n"; 
    2460        } 
    2561    );