-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrebuchet
executable file
·95 lines (83 loc) · 2.26 KB
/
trebuchet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
# ------------------------------------------------------------
#
# trebuchet - launch a set of URLs against a web server in parallel.
# Run for a specific number of seconds, then stop.
#
# Options:
# --host h
# --macro key=value
# --seconds s
# --threads t
# --urlfile file
# --verbose
#
# Copyright 2012 Bill Karwin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------
use strict;
use warnings;
use HTTP::Async;
use HTTP::Request;
use File::Slurp 'read_file';
use Getopt::Long;
my $host;
my %macros = ();
my $seconds = 60;
my $threads = 5;
my $urlfile = 'urls.txt';
my $verbose = 0;
my $r = GetOptions(
'host=s' => \$host,
'macro=s%' => \%macros,
'seconds=i' => \$seconds,
'threads=i' => \$threads,
'urlfile=s' => \$urlfile,
'verbose!' => \$verbose,
);
$macros{HOST} = $host if ($host);
my @lines = read_file($urlfile);
my @urls;
for my $line (@lines) {
if ($line =~ /^\s*#/) { next; }
$line =~ s/\$\((\w+)\)/$macros{$1}/eg;
if ($line =~ /^(\w+)=(.*)/) {
$macros{$1} = $2;
next;
}
push(@urls, $line);
}
my $stoptime = time + $seconds;
my $async = HTTP::Async->new;
my $num_requests = 0;
for (1..$threads) {
my $url = $urls[int(rand($#urls))];
print "$url" if $verbose;
my $req = HTTP::Request->new( GET => $url );
$async->add($req);
$num_requests++;
}
while (my $resp = $async->wait_for_next_response) {
if (time < $stoptime) {
my $url = $urls[int(rand($#urls))];
print "$url" if $verbose;
my $req = HTTP::Request->new( GET => $url );
$async->add($req);
$num_requests++;
}
}
print "$num_requests requests = ", ($num_requests/$seconds), "/sec.\n" if $verbose;
print "Done."