Skip to content

Instantly share code, notes, and snippets.

@dougwilson dougwilson/ Secret
Created Mar 11, 2012

What would you like to do?
#!/usr/bin/env perl
use v5.12;
use warnings;
use Mojo::UserAgent;
use Mojo::IOLoop;
use Time::HiRes qw(time);
my $delay = Mojo::IOLoop->delay(sub { warn "done"; });
my @urls = (
my $ua = Mojo::UserAgent->new(max_redirects => 3);
my $start = time;
foreach my $u (@urls) {
my $now = time;
$ua->get($u => sub {
my($ua, $tx) = @_;
my $u = sprintf "%-25s", $u;
my $et = sprintf "%5.3f", time - $now;
my $len = sprintf "%8s", length($tx->res->body);
? "$u has length $len and loaded in $et ms"
: sprintf "Error for $u: (%s) %s", $tx->error
warn "End of loop\n";
say for $delay->wait;
my $tet = sprintf "%5.3f", time - $start;
say "Total elapsed time: $tet ms";

This comment has been minimized.

Copy link

afresh1 commented Mar 19, 2012

The reason some of these get 0 bytes is because they are redirecting. If you allow redirects somewhere around line 21, you can get results for those more similarly than the other examples. $ua->max_redirects(3);


This comment has been minimized.

Copy link
Owner Author

dougwilson commented Mar 19, 2012

I've enabled redirects in the example now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.