Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8011d019f2
Fetching contributors…

Cannot retrieve contributors at this time

executable file 57 lines (41 sloc) 1.096 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package EventBench::ObjectMethod::CBManager;

use strict;
use warnings;
use Benchmark ':hireswallclock';
use Scalar::Util qw(weaken);

sub new {
my $self = bless({ }, $_[0]);
our $sum;

$self->{handlers}->{sum} = $self->weakcb(sub {
my ($self, %event) = @_;

$sum += $event{arg1} + $event{arg2};
});

return $self;
}

sub receive_event {
my ($self, %event) = @_;

$self->{handlers}->{$event{name}}->($self, %event);
}

#pass in code ref or method name as string
sub weakcb {
my ($self, $cb) = @_;

weaken($self);

return sub {
my ($self) = shift(@_);

die "expected weak reference to self to be valid but it was undefined" unless defined $self;

return $self->$cb(@_);
};
}

return sub {
my (@testData) = @_;
my $test = EventBench::ObjectMethod::CBManager->new;
my $bench;
our $sum;

$bench = timeit(1, sub {
our $sum = 0;

foreach(@testData) {
$test->receive_event(name => 'sum', arg1 => $_->[0], arg2 => $_->[1]);
}
});

return { bench => $bench, sum => $sum };
};
Something went wrong with that request. Please try again.