Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active August 29, 2015 14:10
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 dex4er/757a8ec653d01cb5f192 to your computer and use it in GitHub Desktop.
Save dex4er/757a8ec653d01cb5f192 to your computer and use it in GitHub Desktop.
Mojo::Asset::Null
#!/usr/bin/env perl
# Create 1GB file with this command:
# dd if=/dev/zero of=/var/www/html/bigfile.bin bs=1 count=1 seek=$((1024*1024*1024-1)) conv=sparse
use v5.14;
$ENV{MOJO_MAX_MEMORY_SIZE} = 2 * 1024 * 1024 * 1024;
use Mojo::UserAgent;
use Mojo::Asset::Null;
use Time::HiRes qw(time);
use List::Util qw(sum);
my $verbose = $ARGV[0] =~ /^--?v(?:erbose)?/ ? shift @ARGV : '';
my $url = $ARGV[0] || 'http://localhost/bigfile.bin';
my $n = $ARGV[1] || 1;
my $ua = Mojo::UserAgent->new;
my @speeds;
for (1 .. $n) {
my $t0 = time;
my $size = 0;
my $tx = $ua->build_tx(GET => $url);
$tx->res->content->asset(Mojo::Asset::Null->new);
$tx->res->max_message_size(0);
$tx->res->on(progress => sub {
my ($res) = @_;
$size = $res->content->progress;
print "$size\r" if $verbose;
});
$ua->start($tx, sub {
$size = $tx->res->content->body_size;
my $t1 = time;
my $delta = $t1 - $t0;
push @speeds, int $size * 8 / 1024 / $delta / 1000;
print "$size\n" if $verbose;
});
}
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
my $speed = sum @speeds;
while ($speed =~ s/(?<=\d)(\d{3})(,|$)/,$1/) { }
say $speed, ' Mb/s';
package Mojo::Asset::Null;
use Mojo::Base 'Mojo::Asset';
has size => 0;
sub add_chunk {
my ($self, $chunk) = @_;
$self->{size} += length($chunk);
return $self;
}
sub slurp {
return '';
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment