Changeset 560dfd2 in lab.git


Ignore:
Timestamp:
Mar 6, 2014 2:53:13 PM (11 years ago)
Author:
Ken-ichi Mito <mitty@…>
Branches:
master
Children:
8c96802
Parents:
0035554
Message:

measure the transfer data size and elapsed time

File:
1 edited

Legend:

Unmodified
Added
Removed
  • misc/httpbench.pl

    r0035554 r560dfd2  
    88use Parallel::ForkManager; 
    99use LWP::Simple; 
    10 use Time::HiRes qw(sleep); 
     10use Time::HiRes qw(sleep gettimeofday); 
    1111 
    1212usage() if (@ARGV == 0); 
     
    3232warn "$num urls with $concurrency clients, $loops loops\n"; 
    3333warn "Total: ", $num * $concurrency * $loops, " requests\n"; 
    34 warn "wait for $wait second between requests\n"; 
     34warn "wait for $wait second between requests\n" if ($wait); 
    3535 
    3636 
     37my $transfer = 0; 
     38my $pm = Parallel::ForkManager->new($concurrency); 
     39$pm->run_on_finish( 
     40    sub { 
     41        my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $dataref) = @_; 
     42        if (defined $dataref) { 
     43            $transfer += $$dataref; 
     44        } 
     45    } 
     46); 
    3747 
    38 my $pm = Parallel::ForkManager->new($concurrency); 
    39 for (my $child = 0; $child < $concurrency; $child++) { 
    40     if ($pm->start) { 
    41         warn "forks $child/$concurrency child ...\n"; 
    42         next; 
     48my ($startsec, $startmicro) = gettimeofday(); 
     49{ 
     50    use bytes; 
     51    for (my $child = 0; $child < $concurrency; $child++) { 
     52        if ($pm->start) { 
     53            warn "forks $child/$concurrency child ...\n"; 
     54            next; 
     55        } 
     56            my $transfer = 0; 
     57            for (my $i = 0; $i < $loops; $i++) { 
     58                print STDERR "processing $i/$loops loop\r"; 
     59                foreach my $url (@urls) { 
     60                    my $res = get($url) or print STDERR "\nfail: $url"; 
     61                    if ($res) { 
     62                        $transfer += length($res); 
     63                    } 
     64                    sleep($wait); 
     65                } 
     66            } 
     67        $pm->finish(0, \$transfer); 
    4368    } 
    44         for (my $i = 0; $i < $loops; $i++) { 
    45             print STDERR "processing $i/$loops loop\r"; 
    46             foreach my $url (@urls) { 
    47                 get($url) or warn "fail: $url\n"; 
    48                 sleep($wait); 
    49             } 
    50         } 
    51     $pm->finish; 
     69    $pm->wait_all_children; 
    5270} 
    53 $pm->wait_all_children; 
     71my ($endsec, $endmicro) = gettimeofday(); 
     72my $elapsed = ($endsec - $startsec) + ($endmicro - $startmicro) / 10**6; 
     73my $bytepersec = $transfer / $elapsed; 
     74 
     75my @units = qw( B/s KiB/s MiB/s GiB/s ); 
     76my $unit = 0; 
     77while ($bytepersec > 1024) { 
     78    $bytepersec /= 1024; 
     79    $unit++; 
     80} 
     81$bytepersec = sprintf("%.4g", $bytepersec); 
    5482 
    5583warn "\n ...done.\n"; 
    56  
     84warn "get $transfer bytes in $elapsed seconds ($bytepersec $units[$unit])\n"; 
    5785 
    5886sub usage { 
Note: See TracChangeset for help on using the changeset viewer.