-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadtest.pl
executable file
·131 lines (117 loc) · 2.72 KB
/
loadtest.pl
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/perl
use Switch;
use IO::Socket;
use LWP::UserAgent;
use HTTP::Cookies;
use threads;
use threads::shared;
use Data::Dumper;
use Time::HiRes qw(time);
use LWP::ConnCache;
sub randstr {
my $length_of_randomstring = shift;
# the length of
# the random string to generate
my @chars = ('a'..'z', 'A'..'Z', '0'..'9', '_');
my $random_string;
foreach(1..$length_of_randomstring) {
# rand @chars will generate a random
# number between 0 and scalar @chars
$random_string = $chars[rand @chars];
}
return $random_string;
}
sub Verify {
$resp = shift;
if ($resp->is_success) {
$counter++;
} else{
print Dumper($resp);
}
}
sub LoginSuccessful() {
my $accnumber = int(rand(100)) + 1;
Verify($ua->post(
$BASEURL . "/login",
{"teamname" => "team$accnumber", "password" => "pass$accnumber"}
));
$loggedin = true;
}
sub LoginUnsuccessful() {
$usr = randstr(10);
$psw = randstr(10);
Verify($ua->post(
$BASEURL . "/login",
{ "teamname" => $usr, "password" => $psw }
));
$loggedin = false;
}
sub Logout() {
Verify($ua->get($BASEURL . "/logout"));
$loggedin = false;
}
sub NotLoggedInActions() {
if (rand(100) < 90) {
LoginSuccessful();
} else {
LoginUnsuccessful();
}
}
sub Submit() {
Verify($ua->post($BASEURL . "/team/submit", [ id => rand(40), flag => randstr(10) ]));
}
sub LoggedInActions() {
if (rand(100) < 10) {
Logout();
}
elsif(rand(100) < 30) {
Verify($ua->get($BASEURL . "/common/status"));
}
elsif(rand(100) < 30) {
Verify($ua->get($BASEURL . "/team/status"));
}
elsif(rand(100) < 20) {
Verify($ua->get($BASEURL . "/common/challenge/" . rand(40)));
}
else {
Submit();
}
}
sub OneBusyUser() {
local $loggedin = false;
local $ua = new LWP::UserAgent();
my $cookie_jar = HTTP::Cookies->new;
$cookie_jar->clear;
$ua->cookie_jar($cookie_jar);
$ua->conn_cache(LWP::ConnCache->new());
while (1) {
if ($loggedin == false) {
NotLoggedInActions();
} else {
LoggedInActions();
}
}
}
sub Status() {
my $start = time();
my $end;
while (1) {
sleep(1);
$ctr = $counter;
$end = time();
printf("Successfully executed %i requests in %.2f seconds (avg %.2f req/s)\n", $ctr, $end - $start, $ctr / ($end - $start));
}
}
my @arr;
our $counter: shared;
our $BASEURL;
our $wait;
$counter = 0;
$BASEURL = $ARGV[1];
$arr[0] = threads->new(\&Status);
for ($i = 1; $i <= $ARGV[0]; $i++) {
$arr[$i] = threads->new(\&OneBusyUser);
}
for ($i = 0; $i <= $ARGV[0]; $i++) {
$arr[$i]->join;
}