| 1 | package App::RedisTop::PerformStat; | ||||||||||||
| 2 | 1 | ▶ | use Term::ANSIColor qw/colored/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use List::Util qw/max/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use App::RedisTop::Redis; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | my $version = eval { | |||||||||||
| 7 | 1 | require App::RedisTop; | |||||||||||
| 8 | 1 | $App::RedisTop::VERSION; | |||||||||||
| 9 | }; | ||||||||||||
| 10 | 0 | ▶ | if($@) { $version = '?' } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 13 | 0 | my $class = shift; | |||||||||||
| 14 | |||||||||||||
| 15 | 0 | chomp(my $rows = qx{tput lines}); | |||||||||||
| 16 | 0 | ▶ | $rows ||= 24; | ||||||||||
|
Condition0%0/2
$rows ||= 24
MC/DC0%0/1
$rows ||= 24
$rows
Truth table0%0/2
A
A= $rows
| |||||||||||||
| 17 | |||||||||||||
| 18 | 0 | my $self = bless { | |||||||||||
| 19 | groups => [], | ||||||||||||
| 20 | instances => [], | ||||||||||||
| 21 | width => 20, | ||||||||||||
| 22 | summary => {}, | ||||||||||||
| 23 | prev_stats => {}, | ||||||||||||
| 24 | displayed => 0, | ||||||||||||
| 25 | rows => $rows, | ||||||||||||
| 26 | @_, | ||||||||||||
| 27 | }, $class; | ||||||||||||
| 28 | 0 | my $max_len = max (map { length($_) } @{$self->{instances}}); | |||||||||||
| 29 | 0 | $self->{width} = $max_len; | |||||||||||
| 30 | 0 | $self; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 0 | ▶ | sub separator { colored("|", "blue") } | ||||||||||
|
Subroutine
separator: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 34 | |||||||||||||
| 35 | 0 | ▶ | sub build_title { | ||||||||||
|
Subroutine
build_title: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 36 | 0 | return (); | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | sub build_header { | ||||||||||
|
Subroutine
build_header: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 40 | 0 | my ($self) = @_; | |||||||||||
| 41 | 0 | my $out_str = sprintf("%s ", ' ' x $self->{width}); | |||||||||||
| 42 | 0 | for my $group (@{$self->{groups}}) { | |||||||||||
| 43 | 0 | $out_str .= $group->header; | |||||||||||
| 44 | } | ||||||||||||
| 45 | 0 | return ("$out_str\n"); | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub build_sub_header { | ||||||||||
|
Subroutine
build_sub_header: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 49 | 0 | my ($self) = @_; | |||||||||||
| 50 | 0 | my $format = "%" . $self->{width} . "s%s"; | |||||||||||
| 51 | 0 | my $out_str = colored(sprintf($format, 'INSTANCE', $self->separator), "bold"); | |||||||||||
| 52 | 0 | for my $group (@{$self->{groups}}) { | |||||||||||
| 53 | 0 | $out_str .= $group->sub_header; | |||||||||||
| 54 | } | ||||||||||||
| 55 | 0 | return ("$out_str\n"); | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 0 | ▶ | sub build_line { | ||||||||||
|
Subroutine
build_line: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 59 | 0 | my ($self, $spt) = @_; | |||||||||||
| 60 | 0 | my $out_str = colored(sprintf("%s ", $spt x $self->{width}), "blue"); | |||||||||||
| 61 | 0 | for my $group (@{$self->{groups}}) { | |||||||||||
| 62 | 0 | $out_str .= $group->line($spt); | |||||||||||
| 63 | } | ||||||||||||
| 64 | 0 | return ("$out_str\n"); | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 0 | ▶ | sub build_body { | ||||||||||
|
Subroutine
build_body: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 68 | 0 | my ($self, $instance, $stats, $prev_stats, $redis_config) = @_; | |||||||||||
| 69 | 0 | my $format = "%" . $self->{width} . "s%s"; | |||||||||||
| 70 | 0 | my $out_str = sprintf($format, $instance, $self->separator); | |||||||||||
| 71 | 0 | for my $group (@{$self->{groups}}) { | |||||||||||
| 72 | 0 | $group->redis_config($redis_config); | |||||||||||
| 73 | 0 | $out_str .= $group->body($stats, $prev_stats); | |||||||||||
| 74 | |||||||||||||
| 75 | # stash total count | ||||||||||||
| 76 | 0 | my @values = $group->stat_values($stats, $prev_stats); | |||||||||||
| 77 | 0 | my $key = $group->{group}; | |||||||||||
| 78 | 0 | ▶ | my $total_values = $self->{summary}->{$key} || []; | ||||||||||
|
Condition0%0/2
$self->{'summary'}{$key} || []
MC/DC0%0/1
$self->{'summary'}{$key} || []
$self->{'summary'}{$key}
Truth table0%0/2
A
A= $self->{'summary'}{$key}
| |||||||||||||
| 79 | 0 | ▶ | for (my $i = 0; $i < scalar @values; $i++){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | ▶ | $total_values->[$i] ||= 0; | ||||||||||
|
Condition0%0/2
$total_values->[$i] ||= 0
MC/DC0%0/1
$total_values->[$i] ||= 0
$total_values->[$i]
Truth table0%0/2
A
A= $total_values->[$i]
| |||||||||||||
| 81 | 0 | $total_values->[$i] += $values[$i]; | |||||||||||
| 82 | } | ||||||||||||
| 83 | 0 | $self->{summary}->{$key} = $total_values; | |||||||||||
| 84 | } | ||||||||||||
| 85 | 0 | return ("$out_str\n"); | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | ▶ | sub build_average { | ||||||||||
|
Subroutine
build_average: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 89 | 0 | my ($self) = @_; | |||||||||||
| 90 | 0 | my $instance_count = scalar @{$self->{instances}}; | |||||||||||
| 91 | |||||||||||||
| 92 | 0 | my $format = "%" . $self->{width} . "s%s"; | |||||||||||
| 93 | 0 | my $out_str = colored(sprintf($format, 'AVERAGE', $self->separator), "bold"); | |||||||||||
| 94 | 0 | for my $group (@{$self->{groups}}) { | |||||||||||
| 95 | 0 | my $key = $group->{group}; | |||||||||||
| 96 | 0 | $out_str .= $group->average($self->{summary}->{$key}, $instance_count); | |||||||||||
| 97 | } | ||||||||||||
| 98 | 0 | return ("$out_str\n"); | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 0 | ▶ | sub build_total { | ||||||||||
|
Subroutine
build_total: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 102 | 0 | my ($self) = @_; | |||||||||||
| 103 | 0 | my $format = "%" . $self->{width} . "s%s"; | |||||||||||
| 104 | 0 | my $out_str = colored(sprintf($format, 'TOTAL', $self->separator), "bold"); | |||||||||||
| 105 | 0 | for my $group (@{$self->{groups}}) { | |||||||||||
| 106 | 0 | my $key = $group->{group}; | |||||||||||
| 107 | 0 | $out_str .= $group->total($self->{summary}->{$key}); | |||||||||||
| 108 | } | ||||||||||||
| 109 | 0 | return ("$out_str\n"); | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 0 | ▶ | sub run { | ||||||||||
|
Subroutine
run: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 113 | 0 | my ($self) = @_; | |||||||||||
| 114 | |||||||||||||
| 115 | # init total | ||||||||||||
| 116 | 0 | $self->{summary} = {}; | |||||||||||
| 117 | |||||||||||||
| 118 | 0 | my @lines; | |||||||||||
| 119 | 0 | ▶ | if ($self->{displayed} == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | # build header | ||||||||||||
| 121 | 0 | push @lines, $self->build_title; | |||||||||||
| 122 | 0 | push @lines, $self->build_header; | |||||||||||
| 123 | 0 | push @lines, $self->build_sub_header; | |||||||||||
| 124 | 0 | push @lines, $self->build_line('-'); | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | # instances loop | ||||||||||||
| 128 | 0 | foreach my $instance (@{$self->{instances}}) { | |||||||||||
| 129 | 0 | my ($host, $port) = split(/:/, $instance); | |||||||||||
| 130 | |||||||||||||
| 131 | 0 | my $redis = App::RedisTop::Redis->new(host => $host, port => $port); | |||||||||||
| 132 | 0 | my $config = $redis->config(); | |||||||||||
| 133 | 0 | my $stats = $redis->info(); | |||||||||||
| 134 | 0 | my $slowlog = $redis->slowlog(); | |||||||||||
| 135 | # add slowlog len into stats | ||||||||||||
| 136 | 0 | $stats->{slowlog_len} = $slowlog; | |||||||||||
| 137 | |||||||||||||
| 138 | push @lines, $self->build_body( | ||||||||||||
| 139 | $instance, | ||||||||||||
| 140 | $stats, | ||||||||||||
| 141 | 0 | ▶ | $self->{prev_stats}->{$instance} || {}, | ||||||||||
|
Condition0%0/2
$self->{'prev_stats'}{$instance} || {}
MC/DC0%0/1
$self->{'prev_stats'}{$instance} || {}
$self->{'prev_stats'}{$instance}
Truth table0%0/2
A
A= $self->{'prev_stats'}{$instance}
| |||||||||||||
| 142 | $config, | ||||||||||||
| 143 | ); | ||||||||||||
| 144 | 0 | $self->{prev_stats}->{$instance} = $stats; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 0 | ▶ | if ($self->{displayed} == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | # average | ||||||||||||
| 149 | 0 | push @lines, $self->build_average(); | |||||||||||
| 150 | # total | ||||||||||||
| 151 | 0 | push @lines, $self->build_total(); | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | 0 | print join('', @lines); | |||||||||||
| 155 | 0 | $self->{displayed} += scalar(@lines); | |||||||||||
| 156 | 0 | ▶ | if ($self->{displayed} >= $self->{rows}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 0 | $self->{displayed} = 0; | |||||||||||
| 158 | } | ||||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 1 | 1; | |||||||||||