App-plackbench

 view release on metacpan or  search on metacpan

lib/App/plackbench.pm  view on Meta::CPAN

    my @requests;
    if ( $self->post_data() ) {
        @requests = map {
            my $req = HTTP::Request->new( POST => $self->uri() );
            $req->content($_);
            $req;
        } @{ $self->post_data() };
    }
    else {
        @requests = ( HTTP::Request->new( GET => $self->uri() ) );
    }

    $self->_fixup_requests(\@requests);

    return \@requests;
}

sub _fixup_requests {
    my $self = shift;
    my $requests = shift;

    my $fixups = $self->fixup();
    $fixups = [ grep { reftype($_) && reftype($_) eq 'CODE' } @{$fixups} ];

    for my $request (@{$requests}) {
        $_->($request) for @{$fixups};
    }

    return;
}

sub add_fixup_from_file {
    my $self = shift;
    my $file = shift;

    my $sub = do $file;

    if (!$sub) {
        die($@ || $!);
    }

    if (!reftype($sub) || !reftype($sub) eq 'CODE') {
        die("$file: does not return a subroutine reference");
    }

    my $existing = $self->fixup();
    if (!$existing || !reftype($existing) || reftype($existing) ne 'ARRAY') {
        $self->fixup([]);
    }

    push @{$self->fixup()}, $sub;

    return;
}

sub _execute_request {
    my $self = shift;
    my $request = shift;
    my $response = $self->tester->request($request);
    if ( $response->is_error() ) {
        die "Request failed: " . $response->decoded_content;
    }

    return;
}

1;

__END__

=head1 NAME

App::plackbench - programmatic interface to plackbench

B<See L<plackbench> for the command line tool.>

=head1 SYNOPSIS

    my $bench = App::plackbench->new(
        psgi_path => $psgi_path,
        count     => 5,
        uri       => '/some/path',
    );
    my $stats = $bench->run();

    printf("Averaged %8.3f seconds over %d requests\n", $stats->mean(), $stats->count());

=head1 DESCRIPTION

Class for executing requests on a L<Plack> application and recording stats.

=head1 ATTRIBUTES

=head2 app

Defaults to a L<Plack> app loaded from L<psgi_path>, using L<Plack::Util/load_psgi>.

=head2 tester

Defaults to a L<Plack::Test> instance initialized with the app from L</app>.

=head2 count

Number of times to execute the request. Defaults to 1.

=head2 warm

If true, an initial request will be made which won't be included in the stats.
Defaults to false.

=head2 fixup

An arrayref of subroutine references to do any preprocessing of the request.
Each subroutine reference will be called in order (though you shouldn't rely on
that) and passed a reference to the L<HTTP::Request> object.

Each sub will be called once for every unique request. Under a normal GET
request, there will only be one unique request. However if L</post_data> is
being used there will be one unique request for request body.

The return value from the subs is ignored.



( run in 0.534 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )