source: lab.git/misc/httpbench.pl @ 560dfd2

Last change on this file since 560dfd2 was 560dfd2, checked in by Ken-ichi Mito <mitty@…>, 10 years ago

measure the transfer data size and elapsed time

  • Property mode set to 100755
File size: 2.5 KB
Line 
1#! /usr/bin/perl -w
2
3use strict;
4use warnings;
5use utf8;
6
7use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);
8use Parallel::ForkManager;
9use LWP::Simple;
10use Time::HiRes qw(sleep gettimeofday);
11
12usage() if (@ARGV == 0);
13
14GetOptions(
15    'h|help'            => \ my $help,
16    'i|inputfile=s'     => \ my $file,
17    'c|concurrency=i'   => \ my $concurrency,
18    'n|loops=i'         => \ my $loops,
19    'w|wait=f'          => \ my $wait,
20) or usage();
21
22usage() if $help;
23
24$concurrency ||= 1;
25$loops ||= 1;
26$wait ||= 0;
27
28my @urls = file2urls($file) if ($file);
29push @urls, @ARGV;
30
31my $num = scalar @urls;
32warn "$num urls with $concurrency clients, $loops loops\n";
33warn "Total: ", $num * $concurrency * $loops, " requests\n";
34warn "wait for $wait second between requests\n" if ($wait);
35
36
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);
47
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);
68    }
69    $pm->wait_all_children;
70}
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);
82
83warn "\n ...done.\n";
84warn "get $transfer bytes in $elapsed seconds ($bytepersec $units[$unit])\n";
85
86sub usage {
87    warn "$0 -i urls.txt -c concurrency -n loops -w wait_interval\n",
88         " OR...\n",
89         "$0 url1 url2\n"
90    ;
91   
92    exit;
93}
94
95sub file2urls {
96    my $file = shift;
97   
98    open my $fh, '<', $file or die "$file: $!";
99   
100    my(@urls, $url);
101    while ($url = <$fh>) {
102        chomp $url;
103        push @urls, $url;
104    }
105   
106    return @urls;
107}
Note: See TracBrowser for help on using the repository browser.