Last change
on this file since 41617a0 was
d6dd05b,
checked in by Ken-ichi Mito <mitty@…>, 11 years ago
|
fix: repeat loops in child processes
- '$pm->finish and then $pm->start' loop costs more CPU time
|
-
Property mode set to
100755
|
File size:
1.6 KB
|
Rev | Line | |
---|
[512faed] | 1 | #! /usr/bin/perl -w |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use utf8; |
---|
| 6 | |
---|
| 7 | use Getopt::Long; |
---|
| 8 | use Parallel::ForkManager; |
---|
| 9 | use LWP::Simple; |
---|
| 10 | use Time::HiRes qw(sleep); |
---|
| 11 | |
---|
| 12 | GetOptions( |
---|
| 13 | 'h|help' => \ my $help, |
---|
| 14 | 'i|inputfile=s' => \ my $file, |
---|
| 15 | 'c|concurrency=i' => \ my $concurrency, |
---|
| 16 | 'n|loops=i' => \ my $loops, |
---|
| 17 | 'w|wait=f' => \ my $wait, |
---|
| 18 | ) or usage(); |
---|
| 19 | |
---|
| 20 | usage() if $help; |
---|
| 21 | |
---|
| 22 | $concurrency ||= 1; |
---|
| 23 | $loops ||= 1; |
---|
| 24 | $wait ||= 0; |
---|
| 25 | |
---|
| 26 | my @urls = file2urls($file) if ($file); |
---|
| 27 | push @urls, @ARGV; |
---|
| 28 | |
---|
| 29 | my $num = scalar @urls; |
---|
| 30 | warn "$num urls with $concurrency clients, $loops loops\n"; |
---|
| 31 | warn "Total: ", $num * $concurrency * $loops, " requests\n"; |
---|
| 32 | warn "wait for $wait second between requests\n"; |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | my $pm = Parallel::ForkManager->new($concurrency); |
---|
[d6dd05b] | 37 | for (my $child = 0; $child < $concurrency; $child++) { |
---|
| 38 | if ($pm->start) { |
---|
| 39 | warn "forks $child/$concurrency child ...\n"; |
---|
| 40 | next; |
---|
| 41 | } |
---|
| 42 | for (my $i = 0; $i < $loops; $i++) { |
---|
| 43 | print STDERR "processing $i/$loops loop\r"; |
---|
[512faed] | 44 | foreach my $url (@urls) { |
---|
| 45 | get($url) or warn "fail: $url\n"; |
---|
| 46 | sleep($wait); |
---|
| 47 | } |
---|
[d6dd05b] | 48 | } |
---|
| 49 | $pm->finish; |
---|
[512faed] | 50 | } |
---|
| 51 | $pm->wait_all_children; |
---|
| 52 | |
---|
| 53 | warn "\n ...done.\n"; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | sub usage { |
---|
| 57 | warn "$0 -i urls.txt -c concurrency -n loops -w wait_interval\n", |
---|
| 58 | " OR...\n", |
---|
| 59 | "$0 url1 url2\n" |
---|
| 60 | ; |
---|
| 61 | |
---|
| 62 | exit; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | sub file2urls { |
---|
| 66 | my $file = shift; |
---|
| 67 | |
---|
| 68 | open my $fh, '<', $file or die "$file: $!"; |
---|
| 69 | |
---|
| 70 | my(@urls, $url); |
---|
| 71 | while ($url = <$fh>) { |
---|
| 72 | chomp $url; |
---|
| 73 | push @urls, $url; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return @urls; |
---|
| 77 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.