App-plackbench

 view release on metacpan or  search on metacpan

bin/plackbench  view on Meta::CPAN


exit 0;

sub _parse_argv {
    my $argv = shift;

    my %opts;

    Getopt::Long::Configure('bundling');
    Getopt::Long::GetOptionsFromArray(
        $argv,
        'n=i'    => \$opts{count},
        'warm'   => \$opts{warm},
        'post=s' => \$opts{post_data},
        'e=s'    => \$opts{fixup},
        'f=s'    => \$opts{fixup_files},
        'p=i'    => \$opts{precision},
    );

    ( $opts{psgi_path}, $opts{uri} ) = @{$argv};

    for (keys %opts) {
        delete $opts{$_} unless defined $opts{$_};
    }

    return \%opts;
}

sub _post_data {
    my $file = shift;

    my @bodies;
    if ( $file eq '-' ) {
        say 'Enter POST data. <Ctrl-D> when finished.';
        @bodies = <STDIN>;
    }
    else {
        open( my $fh, $file );
        @bodies = <$fh>;
        close($fh);
    }

    return [ grep $_, map { chomp; $_ } @bodies ];
}

sub _report {
    my $stats = shift;
    my $opts = shift;

    $opts->{precision} //= 3;
    my $time = "%8.$opts->{precision}f";

    printf "Rate (requests per second): %.2f\n\n", $stats->rate;

    print "Request times (seconds):\n";
    printf( "%8s %8s %8s %8s %8s\n", 'min', 'mean', 'sd', 'median', 'max' );
    printf( "$time $time $time $time $time\n\n",
        $stats->min(), $stats->mean(), $stats->standard_deviation(), $stats->median(), $stats->max() );

    print "Percentage of requests within a certain time (seconds):\n";
    for my $percent ( 50, 66, 75, 80, 90, 95, 98, 99, 100 ) {
        my $value = $stats->percentile( $percent );
        printf( "%4d%% $time\n", $percent, $value );
    }
}

=pod

=head1 NAME

plackbench - Benchmarking/Debugging tool for Plack web requests

=head1 SYNOPSIS

    # Make a request 5 times, and print some stats
    $ plackbench -n 5 /path/to/app.psgi '/search?q=stuff'

    # Debug the same request
    $ PERL5OPT=-d plackbench -n 5 /path/to/app.psgi '/search?q=stuff'

    # Profile the same request
    $ PERL5OPT=-d:NYTProf plackbench -n 5 /path/to/app.psgi '/search?q=stuff'
    $ nytprofhtml -m

=head1 DESCRIPTION

This script benchmarks a web request. It hits the Plack app directly without
going through a web server.

This is somewhat useful on it's own for getting an idea of the time spent in
Perl-land for a web request. But it's mostly a harness for a debugger or
profiler.

=head1 USAGE

  plackbench /path/to/app.psgi URI

The first positional argument is the path to a .psgi file. The second is the
URL to request.

The URI is relative to the application root.

=head1 OPTIONS

=over 4

=item -n

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

=item --warm

Make an initial request that won't be included in the stats.

=item --post=<file>

Make a POST request instead of a GET. Pass the path to a file with the raw
URL-encoded POST data. If the file contains multiple lines, each will be used a
separate POST request.

If the file is a '-', the POST body will be read from STDIN.

=item -e <code>



( run in 1.265 second using v1.01-cache-2.11-cpan-39bf76dae61 )