File Coverage

blib/lib/Promises/Deferred/Mojo.pm
Criterion Covered Total %
statement 16 20 80.0
branch n/a
condition n/a
subroutine 6 8 75.0
pod n/a
total 22 28 78.5


line stmt bran cond sub pod time code
1             package Promises::Deferred::Mojo;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: An implementation of Promises in Perl
4             $Promises::Deferred::Mojo::VERSION = '1.05';
5 2     2   1251 use strict;
  2         4  
  2         72  
6 2     2   10 use warnings;
  2         4  
  2         123  
7              
8 2     2   9 use Mojo::IOLoop;
  2         4  
  2         25  
9              
10 2     2   66 use parent 'Promises::Deferred';
  2         4  
  2         20  
11              
12              
13             # Before the next_tick-based approach used below, there was a
14             # Mojo::IOLoop->timer()-based approach for _notify_backend.
15             # The current code is more performant:
16              
17             # Original code (using the Mojo::Reactor::EV backend):
18             # Backend: Promises::Deferred::Mojo
19             # Benchmark: running one, two for at least 10 CPU seconds...
20             # one: 46 wallclock secs @ 758.45/s (n=8032)
21             # two: 44 wallclock secs @ 309.08/s (n=3097)
22              
23              
24             # New approach:
25             # Backend: Promises::Deferred::Mojo
26             # Benchmark: running one, two for at least 10 CPU seconds...
27             # one: 29 wallclock secs @ 1714.56/s (n=17197)
28             # two: 24 wallclock secs @ 1184.80/s (n=12156)
29              
30              
31              
32             sub _notify_backend {
33 8     8   21 my ( $self, $callbacks, $result ) = @_;
34             Mojo::IOLoop->next_tick(sub {
35 8     8   1587 foreach my $cb (@$callbacks) {
36 7         21 $cb->(@$result);
37             }
38 8         63 });
39             }
40              
41             sub _timeout {
42 0     0     my ( $self, $timeout, $callback ) = @_;
43              
44 0           my $id = Mojo::IOLoop->timer( $timeout => $callback );
45            
46 0     0     return sub { Mojo::IOLoop->remove($id) };
  0            
47             }
48              
49             1;
50              
51             __END__