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

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 114 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
END
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/perl -wT
2  
3 use strict;
4 use warnings;
5  
6 BEGIN { unshift @INC, ($ENV{'PERL5LIB'} =~ m/([^:]+)/g); }
7  
8 use MyApp::Bot;
9 use Time::HiRes 'time';
10 use Term::ReadKey;
11 use Getopt::Long;
12  
13 ReadMode "cbreak";
14 END { ReadMode "restore" }
15  
16 my ($opt_clients, $opt_rate, $opt_size, $opt_help);
17 my $no_args = (@ARGV == 0) ? 1 : 0;
18  
19 GetOptions(
20 "clients=i" => \$opt_clients, # --clients
21 "rate=i" => \$opt_rate, # --rate
22 "size=i" => \$opt_size, # --size
23 "help" => \$opt_help, # --help
24 ) or exit;
25  
26 my $Help = "
27 Usage: flood [OPTIONS]
28 Create a lot of clients who flood with messages each other.
29  
30 -c, --clients N number of client connections to use
31 -r, --rate N sustain a rate of N requests per second among all clients
32 -s, --size N size in KiB of requests, default is 0
33 -h, --help display this help and exit
34  
35 To create a 100 clients sending in total 500 messages per second to each other:
36  
37 flood -c 100 -r 500
38  
39 ";
40  
41 if ($opt_help || $no_args) {
42 print $Help;
43 exit;
44 }
45  
46 my $num_bots = $opt_clients || 50;
47 my $msg_sec = $opt_rate || 100;
48 my $msg_size = $opt_size || 0;
49  
50 my @Bots;
51 my $DEBUG = 0;
52  
53 foreach my $n (1..$num_bots) {
54  
55 my $username = sprintf("bot-%.3d", $n);
56  
57 push @Bots, MyApp::Bot->new(
58 username => $username,
59 on_message => sub {
60 my (%args) = @_;
61 return unless $DEBUG;
62 my $message = $args{'message'};
63 my $from = $args{'from'} ? "$args{from}:" : ">";
64 print "$from $message\n";
65 },
66 );
67 }
68  
69 print "$num_bots clients are sending $msg_sec requests per second.\n";
70 print "Workers are handling $msg_sec calls per second.\n";
71 print "Routers are handling " . ($msg_sec * 2) . " messages per second.\n";
72 print "(press any key to stop)\n";
73 $| = 1; # autoflush progress dots
74  
75 while (1) {
76  
77 print '.';
78  
79 my $start_on = time();
80  
81 for (1..$msg_sec) {
82  
83 my $bot_A = $Bots[rand($num_bots)];
84 my $bot_B = $Bots[rand($num_bots)];
85  
86 my $msg = $msg_size ? 'X' x ($msg_size * 1024) : 'Hello ' . $bot_B->username;
87  
88 $bot_A->talk(
89 to_user => $bot_B->username,
90 message => $msg,
91 );
92 }
93  
94 my $cv = AnyEvent->condvar;
95 AnyEvent::postpone { $cv->send };
96 $cv->recv;
97  
98 my $key = ReadKey(-1);
99 if ($key) {
100 print "\n";
101 last;
102 }
103  
104 my $took = time() - $start_on;
105 if ($took > 1) {
106 my $ovl = int(abs(($took - 1) * 100));
107 print "Cannot sustain $msg_sec msg/s ($ovl\% overload)\n";
108 next;
109 }
110  
111 my $wait = 1 - $took;
112 $cv = AnyEvent->condvar;
113 AnyEvent->now_update;
114 my $tmr = AnyEvent->timer( after => $wait, cb => $cv);
115 $cv->recv;
116 }
117  
118 1;