-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient_LoadTest_LWP.pl
More file actions
120 lines (91 loc) · 3.25 KB
/
Client_LoadTest_LWP.pl
File metadata and controls
120 lines (91 loc) · 3.25 KB
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
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use CGI::Util qw(escape);
use Data::Dumper;
use English qw(-no_match_vars);
use JSON::PP;
use HTTP::Request;
use LWP::UserAgent;
use Time::HiRes qw(sleep time);
# Script to load test an active AssetServer running on 127.0.0.1:PORTNUMBER
# Reports time required to run api requests
# USAGE: perl Client_LoadTest_LWP.pl PORTNUMBER [NUMBER OF ITERATIONS]
# NOTE: uses HTTP::Request and LWP::UserAgent modules which are not
# part of Perl core as of 5.16.3
# For every interation:
# creates an asset, adds a note to that asset, requests a random asset,
# requests the notes for that random asset, searches for that random asset
# by name and then by uri.
# Every 4th iteration, deletes asset that was chosen randomly
my $DEFAULT_NUM_ITER = 100;
my ($port, $num_iter) = validate_and_extract_inputs(@ARGV);
my $BASE_URI = "http://127.0.0.1:$port";
my $ua = LWP::UserAgent->new;
my %assets;
my $ctr = 0;
my $start = time;
while (++$ctr < $num_iter) {
# create an asset
my $new_asset = { name => "asset${ctr}_$PID", uri => "uri${ctr}_$PID" };
my $response_body = make_request('POST', '/assets', $new_asset);
$new_asset->{id} = $response_body->{id};
$assets{ $new_asset->{id} } = $new_asset;
# create a note for the just created asset and then for a random asset
my $new_note = { assetid => $new_asset->{id}, note => "this is a note" };
make_request('POST', "/assets/$new_asset->{id}/notes", $new_note );
# get a random asset by id, and search by each name and uri; then get its notes
my $assetid = (keys %assets)[0];
make_request('GET', "/assets/$assetid");
make_request('GET', "/assets/$assetid/notes");
make_request('GET', "/assets?asset_name=" . escape($assets{$assetid}->{name} ));
make_request('GET', "/assets?asset_uri=" . escape($assets{$assetid}->{uri} ));
# delete every 4th asset
if ($ctr % 4 == 0) {
make_request('DELETE', "/assets/$assetid");
delete $assets{$assetid};
}
}
print "Made " . ($ctr * 6.25) . " requests in " . (time - $start) . " seconds\n";
sub make_request {
my ($method, $uri, $content) = @_;
my $request = HTTP::Request->new( $method => $BASE_URI . $uri );
$request->content( encode_json($content) )
if $content;
my $response = $ua->request($request);
my $response_body = decode_json($response->decoded_content)
if $response->decoded_content;
return $response_body;
}
################
##### Helper subs
sub validate_and_extract_inputs {
my (@inputs) = @_;
my $port = $ARGV[0];
if (!$port) {
print STDERR "Missing port number\n";
print_usage_and_exit();
}
if (!is_positive_int($port)) {
print STDERR "Port number must be a positive int\n";
print_usage_and_exit();
}
my $num_iter = $ARGV[1] || $DEFAULT_NUM_ITER;
if (!is_positive_int($num_iter)) {
print STDERR "Listen queue size must be a positive int\n";
print_usage_and_exit();
}
return ($port, $num_iter);
}
sub print_usage_and_exit {
print STDERR "USAGE: perl $PROGRAM_NAME <port number> [number of iterations]\n";
exit(1);
}
sub is_positive_int {
my ($x) = @_;
return unless $x;
return unless $x =~ m/^\d+$/;
return unless $x = int($x);
return unless $x > 0;
return 1;
}