Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
micro benchmark about urlmap
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Request::Common;
use HTTP::Message::PSGI;
use Plack::Test;
use Plack::Builder;
use Benchmark qw/cmpthese timethese/;
use File::Spec;
use Log::Minimal;
$Log::Minimal::AUTODUMP = 1;
my $hashmap_app = builder {
enable AxsLog => (
response_time => 1,
logger => sub {}
);
enable ErrorDocument => (
map {
($_ => File::Spec->catfile("htdocs", "errors", "$_.html"))
} (403, 404, 500)
);
enable 'ServerStatus::Lite',
path => '/___server-status',
allow => [ qw(127.0.0.1 10.0.0.0/8) ],
scoreboard => File::Spec->tmpdir();
my %urlmap;
for my $n (1..30) {
$urlmap{sprintf('/%02d',$n)} = sub{ [ 200, [], [ "Hello "] ] };
}
sub {
my $env = shift;
if ( exists $urlmap{$env->{PATH_INFO}} ) {
return $urlmap{$env->{PATH_INFO}}->($env);
}
[ 404, [], [ "Hello "] ];
};
};
my $urlmap_app = builder {
enable AxsLog => (
response_time => 1,
logger => sub {}
);
enable ErrorDocument => (
map {
($_ => File::Spec->catfile("htdocs", "errors", "$_.html"))
} (403, 404, 500)
);
enable 'ServerStatus::Lite',
path => '/___server-status',
allow => [ qw(127.0.0.1 10.0.0.0/8) ],
scoreboard => File::Spec->tmpdir();
my $last;
for my $n (1..30) {
$last = mount sprintf('/%02d',$n) => sub{ [ 200, [], [ "Hello "] ] };
}
$last;
};
my $env = req_to_psgi(GET "/15");
warnf "%s", $hashmap_app->($env);
warnf "%s", $urlmap_app->($env);
cmpthese(timethese(0,{
'hashmap_app' => sub {
$hashmap_app->($env);
},
'urlmap_app' => sub {
$urlmap_app->($env);
},
}));
__END__
2013-03-05T10:21:42 [WARN] [200,[],['Hello ']] at pmbench.pl line 67
2013-03-05T10:21:42 [WARN] [200,[],['Hello ']] at pmbench.pl line 68
Benchmark: running hashmap_app, urlmap_app for at least 3 CPU seconds...
hashmap_app: 3 wallclock secs ( 2.88 usr + 0.29 sys = 3.17 CPU) @ 5890.54/s (n=18673)
urlmap_app: 3 wallclock secs ( 2.91 usr + 0.16 sys = 3.07 CPU) @ 2965.47/s (n=9104)
Rate urlmap_app hashmap_app
urlmap_app 2965/s -- -50%
hashmap_app 5891/s 99% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment