blib/lib/auto/share/dist/Beekeeper/flood/flood.pl untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 192 CC 11 SCAR 43.0
CC
11
Cov
0%
time_this
132.0
BEGIN
2.0
END
2.0
CRAP 73.5
1 #!/usr/bin/perl -wT
2  
3 use strict;
4 use warnings;
5  
6 BEGIN { unshift @INC, ($ENV{'PERL5LIB'} =~ m/([^:]+)/g); }
7  
8  
9 use MyApp::Service::Flood;
10 use Time::HiRes 'time';
11 use Term::ReadKey;
12 use Getopt::Long;
13  
14 ReadMode "cbreak";
15 END { ReadMode "restore" }
16  
17 my ($opt_count, $opt_number, $opt_rate, $opt_type, $opt_size, $opt_bench, $opt_help);
18 my $no_args = (@ARGV == 0) ? 1 : 0;
19  
20 GetOptions(
21 "type=s" => \$opt_type, # --type
22 "count=i" => \$opt_count, # --count
23 "n=i" => \$opt_number, # --n
24 "rate=i" => \$opt_rate, # --rate
25 "size=i" => \$opt_size, # --size
26 "benchmark" => \$opt_bench, # --benchmark
27 "help" => \$opt_help, # --help
28 ) or exit;
29  
30 my $Help = "
31 Usage: flood [OPTIONS]
32 Flood with requests a test worker pool.
33  
34 -t, --type str type of requests to be made (N, S, A or F)
35 -c, --count N how many requests to be made
36 -r, --rate N sustain a rate of N requests per second
37 -s, --size N size in KiB of requests, default is 0
38 -n, --n N alias for --count
39 -b, --benchmark run a set of predefined benchmarks
40 -h, --help display this help and exit
41  
42 Create a burst of 5000 notifications:
43  
44 flood --type N --count 5000
45  
46 Create a constant load of 100 requests per second:
47  
48 flood --type S --rate 100
49  
50 Run a predefined set of benchmarks:
51  
52 flood --benchmark
53  
54 ";
55  
56 if ($opt_help || $no_args) {
57 print $Help;
58 exit;
59 }
60  
61 my $client = Beekeeper::Client->instance;
62  
63 if ($opt_bench) {
64 # Predefined benchmarks
65 print "\n";
66 run_benchmarks();
67 }
68 else {
69 # Flood / benchmark
70 time_this(
71 type => $opt_type,
72 count => $opt_count || $opt_number,
73 rate => $opt_rate,
74 size => $opt_size,
75 );
76 }
77  
78  
79 sub time_this {
80  
81 my %args = (
82 count => undef,
83 rate => undef,
84 size => undef,
85 type => undef,
86 @_
87 );
88  
89 my $size = $args{'size'} || 0;
90 my $payload = { data => 'X' x ($size * 1024) };
91  
92 my $type = $args{'type'} || 'N';
93 my $code;
94  
95 if ($type =~ m/^N(otification)?/i) {
96 $type = 'notifications';
97 $code = sub {
98 # Measure send time
99 MyApp::Service::Flood->notify( $payload );
100 };
101 }
102 elsif ($type =~ m/^S(ync)?/i) {
103 $type = 'sync calls';
104 $code = sub {
105 # Measure response time
106 MyApp::Service::Flood->echo( $payload );
107 };
108 }
109 elsif ($type =~ m/^A(sync)?/i) {
110 $type = 'async calls';
111 $code = sub {
112 # Measure response time
113 MyApp::Service::Flood->async_echo( $payload, sub {} );
114 };
115 }
116 elsif ($type =~ m/^F(ire)?/i) {
117 $type = 'fire & forget';
118 $code = sub {
119 # Measure send time only
120 MyApp::Service::Flood->fire_echo( $payload );
121 };
122 }
123 else {
124 die "type must be one of (N)otification, (S)ync, (A)sync or (F)ire and forget\n";
125 }
126  
127 my $count = $args{'count'};
128 my $rate = $args{'rate'};
129 my $batch = $rate || $count || 1000;
130 my $total = 0;
131  
132 local $| = 1;
133  
134 BENCH: {
135  
136 printf( "%s %-15s of %3s Kb ", $batch, $type, $size );
137  
138 my $start = time();
139  
140 for (1..$batch) {
141 &$code();
142 }
143  
144 $total += $batch;
145  
146 if ($type eq 'async calls') {
147  
148 # Run the event loop in order to receive responses
149 my $cv = AnyEvent->condvar;
150 AnyEvent::postpone { $cv->send };
151 $cv->recv;
152 }
153  
154 my $took = time() - $start;
155 my $clk = sprintf("%.3f", $took);
156 my $cps = sprintf("%.0f", $batch / $took);
157 my $avg = sprintf("%.2f", $took / $batch * 1000);
158  
159 if ($rate) {
160  
161 printf( "in %6s sec %6s /sec %6s ms each", $clk, $cps, $avg );
162  
163 if ($took <= 1) {
164  
165 my $sleep = 1 - $took;
166 my $cv = AnyEvent->condvar;
167 AnyEvent->now_update;
168 my $tmr = AnyEvent->timer( after => $sleep, cb => $cv);
169 $cv->recv;
170 }
171 else {
172 my $ovl = int(abs(($took - 1) * 100));
173 print " $ovl\% overload";
174 }
175  
176 print "\n";
177  
178 last if $count && $total >= $count;
179  
180 my $key = ReadKey(-1);
181 last if $key;
182  
183 redo BENCH;
184 }
185 else {
186  
187 printf( "in %6s sec %6s /sec %6s ms each\n", $clk, $cps, $avg );
188 }
189 }
190  
191 if ($type eq 'async calls' && !$rate) {
192 # Ensure that all responses are received before running the next benchmark
193 $client->wait_async_calls;
194 }
195 }
196  
197 sub run_benchmarks {
198  
199 my $count = $opt_count || $opt_number || 1000;
200  
201 my @sizes = ( 0, 1, 5, 10 );
202  
203 # Notifications
204 foreach (@sizes) {
205 time_this( type => 'N', count => $count, size => $_ );
206 sleep 1;
207 }
208  
209 print "\n";
210  
211 # Sync calls
212 foreach (@sizes) {
213 time_this( type => 'S', count => $count, size => $_ );
214 sleep 1;
215 }
216  
217 print "\n";
218  
219 # Async calls
220 foreach (@sizes) {
221 time_this( type => 'A', count => $count, size => $_ );
222 sleep 1;
223 }
224  
225 print "\n";
226  
227 # Fire calls
228 foreach (@sizes) {
229 time_this( type => 'F', count => $count, size => $_ );
230 sleep 1;
231 }
232  
233 print "\n";
234 }