source: lab.git/misc/httpbench.pl @ 8c96802

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

use LWP::UserAgent instead of LWP::Simple

  • LWP::Simple does not support verify_hostname option
  • Property mode set to 100755
File size: 2.7 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::UserAgent;
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
36my $ua = LWP::UserAgent->new(
37    ssl_opts => { verify_hostname => 0 },
38);
39my $transfer = 0;
40my $pm = Parallel::ForkManager->new($concurrency);
41$pm->run_on_finish(
42    sub {
43        my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $dataref) = @_;
44        if (defined $dataref) {
45            $transfer += $$dataref;
46        }
47    }
48);
49
50my ($startsec, $startmicro) = gettimeofday();
51{
52    use bytes;
53    for (my $child = 0; $child < $concurrency; $child++) {
54        if ($pm->start) {
55            warn "forks $child/$concurrency child ...\n";
56            next;
57        }
58            my $transfer = 0;
59            for (my $i = 0; $i < $loops; $i++) {
60                print STDERR "processing $i/$loops loop\r";
61                foreach my $url (@urls) {
62                    my $res = $ua->get($url);
63                    if ($res->is_success) {
64                        $transfer += length($res->content);
65                    }
66                    else {
67                        print STDERR "\nfail: $url";
68                    }
69                    sleep($wait);
70                }
71            }
72        $pm->finish(0, \$transfer);
73    }
74    $pm->wait_all_children;
75}
76my ($endsec, $endmicro) = gettimeofday();
77my $elapsed = ($endsec - $startsec) + ($endmicro - $startmicro) / 10**6;
78my $bytepersec = $transfer / $elapsed;
79
80my @units = qw( B/s KiB/s MiB/s GiB/s );
81my $unit = 0;
82while ($bytepersec > 1024) {
83    $bytepersec /= 1024;
84    $unit++;
85}
86$bytepersec = sprintf("%.4g", $bytepersec);
87
88warn "\n ...done.\n";
89warn "get $transfer bytes in $elapsed seconds ($bytepersec $units[$unit])\n";
90
91sub usage {
92    warn "$0 -i urls.txt -c concurrency -n loops -w wait_interval\n",
93         " OR...\n",
94         "$0 url1 url2\n"
95    ;
96   
97    exit;
98}
99
100sub file2urls {
101    my $file = shift;
102   
103    open my $fh, '<', $file or die "$file: $!";
104   
105    my(@urls, $url);
106    while ($url = <$fh>) {
107        chomp $url;
108        push @urls, $url;
109    }
110   
111    return @urls;
112}
Note: See TracBrowser for help on using the repository browser.