Changeset 15437
- Timestamp:
- 07/08/08 11:20:49 (5 years ago)
- Location:
- lang/perl/Queue-Q4M/trunk/misc/lib/Queue/Q4M
- Files:
-
- 1 added
- 2 modified
-
Benchmark.pm (modified) (5 diffs)
-
Benchmark/Plugin/Conditional.pm (added)
-
Benchmark/Plugin/Default.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Queue-Q4M/trunk/misc/lib/Queue/Q4M/Benchmark.pm
r15435 r15437 41 41 42 42 has '__dbh' => ( 43 acces or => 'dbh',43 accessor => 'dbh', 44 44 is => 'rw', 45 45 isa => 'Maybe[DBI::db]', 46 around => sub { 47 my ($next, $self, @args) = @_; 46 ); 48 47 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 } 48 around '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); 55 55 } 56 return $rv;57 },58 );56 } 57 return $rv; 58 }; 59 59 60 60 role_type 'Queue::Q4M::Benchmark::Plugin'; … … 67 67 foreach my $class (@$_) { 68 68 if ($class !~ s/^\+//) { 69 $class = "Queue::Q4M::Benchmark::Plugin:: $class";69 $class = "Queue::Q4M::Benchmark::Plugin::" . ucfirst $class; 70 70 } 71 71 Class::MOP::load_class($class); … … 85 85 86 86 has '__tasks' => ( 87 accessor s=> 'tasks',87 accessor => 'tasks', 88 88 is => 'rw', 89 89 isa => 'HashRef', 90 90 default => sub { +{} } 91 ); 92 93 has 'items' => ( 94 is => 'rw', 95 isa => 'Int', 96 default => 10_000 91 97 ); 92 98 … … 96 102 required => 1, 97 103 default => 1, 104 ); 105 106 has 'define' => ( 107 is => 'rw', 108 isa => 'HashRef', 109 default => sub { +{} } 98 110 ); 99 111 … … 119 131 } 120 132 133 sub add_task { 134 my ($self, %args) = @_; 135 136 $self->tasks->{$args{name}} = $args{coderef}; 137 } 138 121 139 sub run_tasks { 122 140 my $self = shift; 123 141 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 148 my @CHARS = ('a'..'z',0..9, 'A'..'Z'); 149 150 sub random_string { 151 my ($self, $length) = @_; 152 join('', map { $CHARS[rand @CHARS] } 1..$length); 128 153 } 129 154 -
lang/perl/Queue-Q4M/trunk/misc/lib/Queue/Q4M/Benchmark/Plugin/Default.pm
r15435 r15437 3 3 package Queue::Q4M::Benchmark::Plugin::Default; 4 4 use Moose; 5 use Time::HiRes qw(time); 5 6 6 7 with 'Queue::Q4M::Benchmark::Plugin'; 8 9 has 'table' => ( 10 is => 'rw', 11 isa => 'Str', 12 required => 1, 13 default => 'q4mbench_default' 14 ); 7 15 8 16 no Moose; … … 12 20 my ($self, $c) = @_; 13 21 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; 28 EOSQL 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 14 41 $c->add_task( 15 42 name => 'default', 16 43 coderef => sub { 17 my $queue = Queue::Q4M-> new(44 my $queue = Queue::Q4M->connect( 18 45 connect_info => [ $c->connect_info ], 19 46 ); 20 47 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++; 23 54 } 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"; 24 60 } 25 61 );
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)