Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created October 9, 2013 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazeburo/6896740 to your computer and use it in GitHub Desktop.
Save kazeburo/6896740 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Plack::Response;
use Plack::Util;
use Cookie::Baker;
use Benchmark qw/timethese cmpthese/;
cmpthese(timethese(-1, {
'baker' => sub {
baker_set_cookie(
'foo',
'bar',
[200,["Content-Type"=>"text/plain"],["Hello World"]],
path => '/',
httponly => 1,
)
},
'original' => sub {
original_set_cookie(
'foo',
'bar',
[200,["Content-Type"=>"text/plain"],["Hello World"]],
path => '/',
httponly => 1,
)
},
}));
sub baker_set_cookie {
my($session_key, $id, $res, %options) = @_;
my $cookie = bake_cookie(
$session_key, {
value => $id,
%options,
}
);
Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
}
sub original_set_cookie {
my($session_key, $id, $res, %options) = @_;
# TODO: Do not use Plack::Response
my $response = Plack::Response->new(@$res);
$response->cookies->{ $session_key } = +{
value => $id,
%options,
};
my $final_r = $response->finalize;
$res->[1] = $final_r->[1]; # headers
}
__END__
Benchmark: running baker, original for at least 1 CPU seconds...
baker: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 95466.98/s (n=101195)
original: 1 wallclock secs ( 1.14 usr + 0.00 sys = 1.14 CPU) @ 13472.81/s (n=15359)
Rate original baker
original 13473/s -- -86%
baker 95467/s 609% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment