Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 57 lines (41 sloc) 1.096 kb
5b3c101 created benchmark suite for object delivery and consumption
Tyler Riddle authored
1 package EventBench::ObjectMethod::CBManager;
2
3 use strict;
4 use warnings;
5 use Benchmark ':hireswallclock';
6 use Scalar::Util qw(weaken);
7
8 sub new {
9 my $self = bless({ }, $_[0]);
10 our $sum;
11
12 $self->{handlers}->{sum} = $self->weakcb(sub {
13 my ($self, %event) = @_;
14
15 $sum += $event{arg1} + $event{arg2};
16 });
17
18 return $self;
19 }
20
21 sub receive_event {
22 my ($self, %event) = @_;
23
24 $self->{handlers}->{$event{name}}->($self, %event);
25 }
26
27 #pass in code ref or method name as string
28 sub weakcb {
29 my ($self, $cb) = @_;
30
31 weaken($self);
32
33 return sub {
34 my ($self) = shift(@_);
35
36 die "expected weak reference to self to be valid but it was undefined" unless defined $self;
37
38 return $self->$cb(@_);
39 };
40 }
41
42 return sub {
43 my (@testData) = @_;
44 my $test = EventBench::ObjectMethod::CBManager->new;
45 my $bench;
46 our $sum;
47
48 $bench = timeit(1, sub {
49 our $sum = 0;
50
51 foreach(@testData) {
52 $test->receive_event(name => 'sum', arg1 => $_->[0], arg2 => $_->[1]);
53 }
54 });
55
56 return { bench => $bench, sum => $sum };
57 };
Something went wrong with that request. Please try again.