Skip to content

Instantly share code, notes, and snippets.

@mrsndmn
Last active December 17, 2019 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsndmn/4c0d6fbe425aa7904ae739c85c72b315 to your computer and use it in GitHub Desktop.
Save mrsndmn/4c0d6fbe425aa7904ae739c85c72b315 to your computer and use it in GitHub Desktop.
Perl AnyEvent::HTTP and LWP::Protocol::Coro::http benchmark
=head1 install deps
cpm install AnyEvent::HTTP LWP::Protocol::Coro::http
=head1 bench result
Rate aehttp_socks aehttp my_lwp state_lwp
aehttp_socks 183/s -- -1% -6% -17%
aehttp 185/s 1% -- -5% -16%
my_lwp 195/s 6% 5% -- -12%
state_lwp 221/s 20% 19% 13% --
=cut
use Benchmark qw(:all);
use strict;
use warnings;
use AnyEvent::HTTP;
use AnyEvent::HTTP::Socks;
use feature 'state';
use LWP::Protocol::Coro::http;
use LWP::UserAgent;
use Coro qw( async );
sub test_with {
my $cb = shift;
my @urls = ('https://vk.com') x 10;
my @coros;
for my $url (@urls) {
push @coros, async {
$cb->($url);
};
}
$_->join for @coros;
}
cmpthese( -5, {
state_lwp => sub {
test_with(sub {
my $url = shift;
state $ua = LWP::UserAgent->new();
$ua->protocols_allowed([qw( http https )]); # The only protocols made safe.
$ua->get($url);
})
},
my_lwp => sub {
test_with(sub {
my $url = shift;
my $ua = LWP::UserAgent->new();
$ua->protocols_allowed([qw( http https )]); # The only protocols made safe.
$ua->get($url);
})
},
aehttp => sub {
test_with(sub {
my $url = shift;
my $headers_avail = AnyEvent->condvar();
my $data_channel = Coro::Channel->new(1);
my $method = 'GET';
# AnyEvent::HTTP::Socks::http_request(
AnyEvent::HTTP::http_request(
$method => $url,
headers => {},
# %opts,
recurse => 0,
on_header => sub {
#my ($headers) = @_;
# _set_response_headers($response, $_[0]);
$headers_avail->send();
return 1;
},
on_body => sub {
#my ($chunk, $headers) = @_;
$data_channel->put(\$_[0]);
return 1;
},
sub {
$headers_avail->send();
$data_channel->put(\''); # '
},
);
$headers_avail->recv();
$data_channel->get();
}),
},
aehttp_socks => sub {
test_with(sub {
my $url = shift;
my $headers_avail = AnyEvent->condvar();
my $data_channel = Coro::Channel->new(1);
my $method = 'GET';
# AnyEvent::HTTP::Socks::http_request(
AnyEvent::HTTP::http_request(
$method => $url,
headers => {},
# %opts,
recurse => 0,
on_header => sub {
#my ($headers) = @_;
# _set_response_headers($response, $_[0]);
$headers_avail->send();
return 1;
},
on_body => sub {
#my ($chunk, $headers) = @_;
$data_channel->put(\$_[0]);
return 1;
},
sub {
$headers_avail->send();
$data_channel->put(\''); # '
},
);
$headers_avail->recv();
$data_channel->get();
})
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment