source: lab.git/misc/httpbench.pl @ d6dd05b

Last change on this file since d6dd05b 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
Line 
1#! /usr/bin/perl -w
2
3use strict;
4use warnings;
5use utf8;
6
7use Getopt::Long;
8use Parallel::ForkManager;
9use LWP::Simple;
10use Time::HiRes qw(sleep);
11
12GetOptions(
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
20usage() if $help;
21
22$concurrency ||= 1;
23$loops ||= 1;
24$wait ||= 0;
25
26my @urls = file2urls($file) if ($file);
27push @urls, @ARGV;
28
29my $num = scalar @urls;
30warn "$num urls with $concurrency clients, $loops loops\n";
31warn "Total: ", $num * $concurrency * $loops, " requests\n";
32warn "wait for $wait second between requests\n";
33
34
35
36my $pm = Parallel::ForkManager->new($concurrency);
37for (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";
44            foreach my $url (@urls) {
45                get($url) or warn "fail: $url\n";
46                sleep($wait);
47            }
48        }
49    $pm->finish;
50}
51$pm->wait_all_children;
52
53warn "\n ...done.\n";
54
55
56sub 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
65sub 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.