App-plackbench
view release on metacpan or search on metacpan
bin/plackbench view on Meta::CPAN
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
lib/App/plackbench/Stats.pm view on Meta::CPAN
my $differences_sum = reduce {
$a + ( ( $b - $mean )**2 );
}
0, @{$self};
my $sd = sqrt( $differences_sum / $self->count() );
return $sd;
}
sub percentile {
my $self = shift;
my $percentile = shift;
my $n = int(( $percentile / 100 ) * $self->count() + 0.5) - 1;
$n = 0 if $n < 0;
return $self->[$n];
}
sub elapsed {
my $self = shift;
return sum(@$self) || 0;
}
lib/App/plackbench/Stats.pm view on Meta::CPAN
Returns the smallest number in the collection.
=head2 max
Returns the largest number in the collection.
=head2 standard_deviation
Returns the standard deviation. L<http://en.wikipedia.org/wiki/Standard_deviation>.
=head2 C<percentile($n)>
Returns the number at percentile C<$n>.
=head2 SEE ALSO
L<plackbench>
t/02_stats.t view on Meta::CPAN
use App::plackbench::Stats;
subtest 'new' => \&test_new;
subtest 'insert' => \&test_insert;
subtest 'count' => \&test_count;
subtest 'mean' => \&test_mean;
subtest 'median' => \&test_median;
subtest 'min' => \&test_min;
subtest 'max' => \&test_max;
subtest 'standard deviation' => \&test_standard_deviation;
subtest 'percentile' => \&test_percentile;
subtest 'elapsed' => \&test_elapsed;
subtest 'rate' => \&test_rate;
done_testing();
sub test_new {
my $stats = App::plackbench::Stats->new( 2, 1 );
ok( $stats->isa('App::plackbench::Stats'),
'new() should return an instance of App::plackbench::Stats' );
cmp_deeply(
t/02_stats.t view on Meta::CPAN
sub test_standard_deviation {
my $stats = App::plackbench::Stats->new(2, 4, 4, 4, 5, 5, 7, 9);
is( $stats->standard_deviation(), 2, 'standard_deviation() should return the standard_deviation' );
$stats = App::plackbench::Stats->new();
is( $stats->standard_deviation(), 0, 'standard_deviation() should return 0 for an empty list' );
return;
}
sub test_percentile {
my $stats = App::plackbench::Stats->new( 9, 8, 7, 6, 5, 4, 3, 2, 1 );
is( $stats->percentile(100),
$stats->max(), '100th percentile should return the largest number' );
is( $stats->percentile(50),
$stats->median(), '50th percentile should return median' );
is( $stats->percentile(0),
$stats->min(), '0th percentile should return the smallest number' );
$stats = App::plackbench::Stats->new();
is( $stats->percentile(50), undef, 'percentile() should return undef for an empty list' );
return;
}
sub test_elapsed {
my $stats = App::plackbench::Stats->new(2, 4, 4, 4, 5, 5, 7, 9);
is( $stats->elapsed(), 40, 'elapsed() should return the total time' );
$stats = App::plackbench::Stats->new();
is( $stats->elapsed(), 0, 'elapsed() should return 0 for an empty list' );
( run in 0.451 second using v1.01-cache-2.11-cpan-05162d3a2b1 )