| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Memory-Stats | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2014 by celogeek <me@celogeek.com>. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Memory::Stats; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Memory Usage Consumption of your process | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | our $VERSION = '0.04'; # VERSION | |||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Proc::ProcessTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | my $pt = Proc::ProcessTable->new; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | has '_memory_usage' => ( is => 'rw', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 10 | ▶ | sub _get_current_memory_usage { | ||||||||||||||||||||||||||
|
Subroutine
_get_current_memory_usage: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 25 | 10 | my ($info) = grep { $_->pid eq $$ } @{ $pt->table }; | |||||||||||||||||||||||||||
| 26 | 10 | my $memory_usage; | |||||||||||||||||||||||||||
| 27 | 10 | ▶ | return -1 if !defined eval { $memory_usage = $info->rss }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 10 | return $memory_usage; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 1 | $self->_memory_usage( [ [ "start", _get_current_memory_usage() ] ] ); | |||||||||||||||||||||||||||
| 34 | 1 | return; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | sub checkpoint { | ||||||||||||||||||||||||||
|
Subroutine
checkpoint: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 4 | ▶ | my $title = shift // 'checkpoint'; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // "checkpoint"
MC/DC100%1/1
shift() // "checkpoint"
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 40 | croak "Please start first !" | ||||||||||||||||||||||||||||
| 41 | 4 | ▶ | if !scalar @{ $self->_memory_usage } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not scalar @{$self->_memory_usage;} or $self->_memory_usage->[-1][0] eq "stop"
MC/DC100%2/2
not scalar @{$self->_memory_usage;} or $self->_memory_usage->[-1][0] eq "stop"
not scalar @{$self->_memory_usage;}
$self->_memory_usage->[-1][0] eq "stop"
Truth table100%3/3
not(A) or B
A= not scalar @{$self->_memory_usage;}B= $self->_memory_usage->[-1][0] eq "stop"
| |||||||||||||||||||||||||||||
| 42 | || $self->_memory_usage->[-1][0] eq 'stop'; | ||||||||||||||||||||||||||||
| 43 | 2 | push @{ $self->_memory_usage }, [ $title, _get_current_memory_usage() ]; | |||||||||||||||||||||||||||
| 44 | 2 | return; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | sub stop { | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | croak "Please start first !" | ||||||||||||||||||||||||||||
| 50 | 3 | ▶ | if !scalar @{ $self->_memory_usage } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not scalar @{$self->_memory_usage;} or $self->_memory_usage->[-1][0] eq "stop"
MC/DC100%2/2
not scalar @{$self->_memory_usage;} or $self->_memory_usage->[-1][0] eq "stop"
not scalar @{$self->_memory_usage;}
$self->_memory_usage->[-1][0] eq "stop"
Truth table100%3/3
not(A) or B
A= not scalar @{$self->_memory_usage;}B= $self->_memory_usage->[-1][0] eq "stop"
| |||||||||||||||||||||||||||||
| 51 | || $self->_memory_usage->[-1][0] eq 'stop'; | ||||||||||||||||||||||||||||
| 52 | 1 | push @{ $self->_memory_usage }, [ 'stop', _get_current_memory_usage() ]; | |||||||||||||||||||||||||||
| 53 | 1 | return; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | sub delta_usage { | ||||||||||||||||||||||||||
|
Subroutine
delta_usage: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | my $last_memory_usage = $self->_memory_usage->[-1][1] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | or return; | ||||||||||||||||||||||||||||
| 60 | 5 | return ( _get_current_memory_usage() - $last_memory_usage ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | croak "Please start and stop before !" | ||||||||||||||||||||||||||||
| 66 | 2 | ▶ | if scalar @{ $self->_memory_usage } < 2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @{$self->_memory_usage;} < 2 or $self->_memory_usage->[-1][0] ne "stop"
MC/DC50%1/2
scalar @{$self->_memory_usage;} < 2 or $self->_memory_usage->[-1][0] ne "stop"
scalar @{$self->_memory_usage;} < 2
$self->_memory_usage->[-1][0] ne "stop"
Truth table66%2/3
A or B
A= scalar @{$self->_memory_usage;} < 2B= $self->_memory_usage->[-1][0] ne "stop"
| |||||||||||||||||||||||||||||
| 67 | || $self->_memory_usage->[-1][0] ne 'stop'; | ||||||||||||||||||||||||||||
| 68 | 1 | return $self->_memory_usage->[-1][1] - $self->_memory_usage->[0][1]; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub report { | ||||||||||||||||||||||||||
|
Subroutine
report: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | 1 | print "--- Memory Usage ---\n"; | |||||||||||||||||||||||||||
| 75 | 1 | my $prev; | |||||||||||||||||||||||||||
| 76 | 1 | for my $row ( @{ $self->_memory_usage } ) { | |||||||||||||||||||||||||||
| 77 | 4 | ▶ | if ($prev) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 3 | printf( | |||||||||||||||||||||||||||
| 79 | "%s: %d - delta: %d - total: %d\n", | ||||||||||||||||||||||||||||
| 80 | @$row, | ||||||||||||||||||||||||||||
| 81 | $row->[1] - $prev, | ||||||||||||||||||||||||||||
| 82 | $row->[1] - $self->_memory_usage->[0][1] | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 1 | printf( "%s: %d\n", @$row ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 4 | $prev = $row->[1]; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 1 | print "--- Memory Usage ---\n"; | |||||||||||||||||||||||||||
| 91 | 1 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | 1; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||