source: lab.git/misc/httpbench.pl @ 512faed

Last change on this file since 512faed was 512faed, checked in by Ken-ichi Mito <mitty@…>, 11 years ago

http bench mark client with perl's Parallel::ForkManager

  • $ ./httpbench.pl -i urls.txt -c 10 -n 2 -w 0.2
  • Property mode set to 100755
File size: 1.5 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 $i = 0; $i < $loops; $i++) {
38    for (my $child = 0; $child < $concurrency; $child++) {
39        $pm->start and next;
40            foreach my $url (@urls) {
41                get($url) or warn "fail: $url\n";
42                sleep($wait);
43            }
44        $pm->finish;
45    }
46    print STDERR " ... ", $num * $concurrency * ($i + 1), " requesting\r";
47}
48$pm->wait_all_children;
49
50warn "\n ...done.\n";
51
52
53sub usage {
54    warn "$0 -i urls.txt -c concurrency -n loops -w wait_interval\n",
55         " OR...\n",
56         "$0 url1 url2\n"
57    ;
58   
59    exit;
60}
61
62sub file2urls {
63    my $file = shift;
64   
65    open my $fh, '<', $file or die "$file: $!";
66   
67    my(@urls, $url);
68    while ($url = <$fh>) {
69        chomp $url;
70        push @urls, $url;
71    }
72   
73    return @urls;
74}
Note: See TracBrowser for help on using the repository browser.