Skip to content

Instantly share code, notes, and snippets.

@skaurus
Created October 12, 2018 19:37
Show Gist options
  • Save skaurus/5960a330bb68a6b88535e66af64eb448 to your computer and use it in GitHub Desktop.
Save skaurus/5960a330bb68a6b88535e66af64eb448 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/perl
use 5.026;
use warnings;
use FindBin qw/$RealBin/;
use lib $RealBin . '/../local/lib/perl5', $RealBin . '/../lib';
use Benchmark;
use LWP::Curl;
use Mojo::UserAgent;
use Time::HiRes qw/gettimeofday tv_interval/;
my $url = 'https://example.com/';
my @html;
my $t0;
@html = ();
Benchmark::cmpthese(100, {
'mojo' => sub {
my $ua = Mojo::UserAgent->new;
Mojo::IOLoop->delay(
sub {
my $delay = shift;
for (1..10) {
$ua->get( $url => $delay->begin );
}
},
sub {
my $delay = shift;
for my $r (@_) {
die unless $r->res->body =~ /Example Domain/;
push @html, $r->res->body;
}
}
)->wait;
},
'lwp_curl' => sub {
my $ua = LWP::Curl->new;
for (1..10) {
my $b = $ua->get($url);
die unless $b =~ /Example Domain/;
push @html, $b;
}
}
});
printf "mojo and lwp::curl fetched %d in benchmark\n", scalar(@html);
@html = ();
$t0 = [gettimeofday];
for (1..100) {
my $ua = Mojo::UserAgent->new;
Mojo::IOLoop->delay(
sub {
my $delay = shift;
for (1..10) {
$ua->get( $url => $delay->begin );
}
},
sub {
my $delay = shift;
for my $r (@_) {
die unless $r->res->body =~ /Example Domain/;
push @html, $r->res->body;
}
}
)->wait;
}
printf "mojo outside benchmark, fetched %d, in %.2f seconds\n", scalar(@html), tv_interval($t0);
@html = ();
$t0 = [gettimeofday];
for (1..100) {
my $ua = LWP::Curl->new;
for (1..10) {
my $body = $ua->get($url);
die unless $body =~ /Example Domain/;
push @html, $body;
}
}
printf "lwp::curl outside benchmark, fetched %d, in %.2f seconds\n", scalar(@html), tv_interval($t0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment