Catalyst-Plugin-MemoryUsage

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/MemoryUsage.pm  view on Meta::CPAN





sub memory_usage_report {
    my $self = shift;

    my $title_width = max 10,
        map { length $_->[1] } @{ $self->memory_usage->state };

    my $table = Text::SimpleTable->new( 
        [$title_width, ''],
        map { [ 4, $_ ] } 
        map { ( $_, 'delta' ) }
            qw/ vsz rss  shared code data /
    );

    my @previous;

    for my $s ( @{ $self->memory_usage->state } ) {
        my ( undef, $msg, @sizes ) = @$s;

        my @data = map { $_ ? format_bytes( 1024 * $_) : '' } map { 
            ( $sizes[$_], @previous ? $sizes[$_] - $previous[$_]  : 0 )
        } 0..4;
        @previous = @sizes;

        $table->row( $msg, @data );
    }

    return $table->draw;
}

unless ( $os_not_supported ) {

    after execute => sub {
        return unless $_memory_usage_record_actions;

        my $c = shift;
        $c->memory_usage->record( "after " . join " : ", @_ );
    };

    around prepare => sub {
        my $orig = shift;
        my $self = shift;

        my $c = $self->$orig(@_);

        $c->memory_usage->record('preparing for the request') 
            if $_memory_usage_record_actions;

        return $c;
    };

    after finalize => sub {
        return unless $_memory_usage_report;

        my $c = shift;
        $c->log->debug(
            sprintf(qq{[%s] memory usage of request "%s" from "%s"\n},
                [split m{::}, __PACKAGE__]->[-1],
                $c->req->uri,
                $c->req->address,
            ),
            $c->memory_usage_report
        );
    };

}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Catalyst::Plugin::MemoryUsage - Profile memory usage of requests

=head1 VERSION

version 0.4.1

=head1 SYNOPSIS

In YourApp.pm:

    package YourApp;

    use Catalyst qw/ MemoryUsage /;

In a Controller class:

    sub foo :Path( '/foo' ) {
         # ...
         
         something_big_and_scary();
         
         $c->memory_usage->record( 'finished running iffy code' );
         
         # ...
    }

In yourapp.conf:

    <Plugin::MemoryUsage>
        report            1
        action_milestones 1
    </Plugin::MemoryUsage>

=head1 DESCRIPTION

C<Catalyst::Plugin::MemoryUsage> adds a memory usage profile to your debugging
log, which looks like this:   

 [debug] [MemoryUsage] memory usage of request "http://localhost/index" from "127.0.0.1"
 .--------------------------------------------------+------+------+------+------+------+------+------+------+------+------.
 |                                                  | vsz  | del- | rss  | del- | sha- | del- | code | del- | data | del- |



( run in 0.352 second using v1.01-cache-2.11-cpan-71847e10f99 )