blib/lib/Linux/GetPidstat/Collector/Parser.pm

filter: Statementstmt 100% 49 / 49 Branchbran 92.8% 13 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 92.9% 79 / 85 CC 11 SCAR 15.6
CC
11
Cov
96%
_get_metric_mean
9.1
parse
3.0
BEGIN
1.0
CRAP 4.7
« blib/lib/Linux/GetPidstat.pm ↑ Summary blib/lib/Linux/GetPidstat/Reader.pm »
1 package Linux::GetPidstat::Collector::Parser;
2 23 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use List::Util qw(sum reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 23 our @EXPORT = qw(parse_pidstat_output);
10  
11 sub parse ($) {
12 20 ▶ my $lines = shift;
Subroutine parse: called CC=3 SCAR=11.0
Pod undocumented
13  
14 20 my $ret;
15  
16 20 my $mapping = _get_metric_rule_mapping();
17 20 my $pid_rule = delete $mapping->{pid};
18 20 ▶ while (my ($name, $metric_rule) = each %$mapping) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$mapping) 140 15 155
19 140 my $metric = _get_metric_mean($metric_rule, $pid_rule, $lines);
20 140 ▶ unless (defined $metric) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $metric) 5 135 140
21 5 warn (sprintf "Empty metric: name=%s, lines=%s\n",
22 $name, join ',', @$lines);
23 5 return;
24 }
25  
26 # ex. cpu => 21.0
27 135 $ret->{$name} = $metric;
28 }
29  
30 15 return $ret;
31 }
32  
33 sub _get_metric_rule_mapping() {
34 745 ▶ my $convert_from_kilobytes = sub { my $raw = shift; return $raw * 1000 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _get_metric_rule_mapping: called CC=1 SCAR=0.0
35  
36 return {
37 20 pid => {
38 column_num => 2,
39 },
40 cpu => {
41 column_num => 6,
42 },
43 memory_percent => {
44 column_num => 12,
45 },
46 memory_rss => {
47 column_num => 11,
48 convert_func => $convert_from_kilobytes,
49 },
50 stk_size => {
51 column_num => 13,
52 convert_func => $convert_from_kilobytes,
53 },
54 stk_ref => {
55 column_num => 14,
56 convert_func => $convert_from_kilobytes,
57 },
58 disk_read_per_sec => {
59 column_num => 15,
60 convert_func => $convert_from_kilobytes,
61 },
62 disk_write_per_sec => {
63 column_num => 16,
64 convert_func => $convert_from_kilobytes,
65 },
66 cswch_per_sec => {
67 column_num => 18,
68 },
69 nvcswch_per_sec => {
70 column_num => 19,
71 },
72 };
73 }
74  
75 sub _get_metric_mean($$) {
76 140 ▶ my ($metric_rule, $pid_rule, $lines) = @_;
Subroutine _get_metric_mean: called CC=9 SCAR=22.0
77  
78 140 my %metrics;
79 140 for (@$lines) {
80 2517 my @fields = split " ";
81  
82 2517 my $pid = $fields[$pid_rule->{column_num}];
83 2517 ▶ next unless defined $pid && $pid =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid =~ /^\d+$/ 1176 1341 2517
Condition100%3/3
defined $pid and $pid =~ /^\d+$/
!ll&&!rl&&r
5186581341
MC/DC100%2/2
defined $pid and $pid =~ /^\d+$/
defined $pid $pid =~ /^\d+$/
Truth table100%3/3
A and B
A= defined $pidB= $pid =~ /^\d+$/
ABresult
0X0
100
111
84  
85 1341 my $metric = $fields[$metric_rule->{column_num}];
86 1341 ▶ next unless defined $metric && $metric =~ /^[-+]?[0-9.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $metric and $metric =~ /^[-+]?[0-9.]+$/ 0 1341 1341
Condition33%1/3
defined $metric and $metric =~ /^[-+]?[0-9.]+$/
!ll&&!rl&&r
001341
MC/DC0%0/2
defined $metric and $metric =~ /^[-+]?[0-9.]+$/
defined $metric $metric =~ /^[-+]?[0-9.]+$/
Truth table33%1/3
A and B
A= defined $metricB= $metric =~ /^[-+]?[0-9.]+$/
ABresult
0X0
100
111
87 1341 ▶ if (my $cf = $metric_rule->{convert_func}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cf = $metric_rule->{"convert_func"}) 745 596 1341
88 745 $metric = $cf->($metric);
89 }
90  
91 1341 ▶ $metrics{$pid} = [] unless exists $metrics{$pid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $metrics{$pid} 342 999 1341
92 1341 push @{ $metrics{$pid} }, $metric;
93 }
94  
95 140 ▶ return unless %metrics;
Branch: 1 branch
BranchTrueFalseTotal
unless %metrics 5 135 140
96 135 ▶ return reduce { $a + _mean(@$b) } (0, values %metrics);
Subroutine __ANON__: called CC=1 SCAR=0.0
97 }
98  
99 sub _mean(@) {
100 342 ▶ return sprintf '%.2f', sum(@_)/@_;
Subroutine _mean: called CC=1 SCAR=0.0
101 }
102  
103 23 *parse_pidstat_output = \&parse;
104  
105 23 1;
106 __END__
« blib/lib/Linux/GetPidstat.pm ↑ Summary blib/lib/Linux/GetPidstat/Reader.pm »