7 use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);
8 use Parallel::ForkManager;
10 use Time::HiRes qw(sleep gettimeofday);
12 usage() if (@ARGV == 0);
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,
28 my @urls = file2urls($file) if ($file);
31 my $num = scalar @urls;
32 warn "$num urls with $concurrency clients, $loops loops\n";
33 warn "Total: ", $num * $concurrency * $loops, " requests\n";
34 warn "wait for $wait second between requests\n" if ($wait);
36 my $ua = LWP::UserAgent->new(
37 ssl_opts => { verify_hostname => 0 },
40 my $pm = Parallel::ForkManager->new($concurrency);
43 my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $dataref) = @_;
44 if (defined $dataref) {
45 $transfer += $$dataref;
50 my ($startsec, $startmicro) = gettimeofday();
51 for (my $child = 0; $child < $concurrency; $child++) {
55 warn "forks $child/$concurrency child ...\n";
60 for (my $i = 0; $i < $loops; $i++) {
61 print STDERR "processing $i/$loops loop\r";
62 foreach my $url (@urls) {
63 my $res = $ua->get($url);
64 if ($res->is_success) {
65 $transfer += length($res->content);
68 print STDERR "\nfail: $url";
73 $pm->finish(0, \$transfer);
76 $pm->wait_all_children;
77 my ($endsec, $endmicro) = gettimeofday();
78 my $elapsed = ($endsec - $startsec) + ($endmicro - $startmicro) / 10**6;
79 my $bytepersec = $transfer / $elapsed;
81 my @units = qw( B/s KiB/s MiB/s GiB/s );
83 while ($bytepersec > 1024) {
87 $bytepersec = sprintf("%.4g", $bytepersec);
90 warn "get $transfer bytes in $elapsed seconds ($bytepersec $units[$unit])\n";
93 warn "$0 -i urls.txt -c concurrency -n loops -w wait_interval\n",
104 open my $fh, '<', $file or die "$file: $!";
107 while ($url = <$fh>) {