Hateburo: kazeburo hatenablog

SRE / 運用系小姑 / Goを書くPerl Monger

Released Kossy-0.23. Improve Performance

Kossy-0.23 gets 2x or more faster.

https://metacpan.org/release/Kossy

I made these changes.

  • Cache Kossy::Request->base. URI->canonical is slow.
  • Use Router::Boom for router
  • Use HTTP::Headers::Fast
  • Optimize Kossy::Response->finalize

Benchmark

#!/usr/bin/env perl

use strict;
use warnings;

package MyApp;

use strict;
use warnings;
use Kossy;

get "/" => sub {
    my ( $self, $c )  = @_;
    $c->response->body("ok");
};

router [qw/GET POST/] => "/bar" => sub {
    my ( $self, $c )  = @_;
    $c->response->body("ok");
};

get "/uri_for" => sub {
    my ( $self, $c )  = @_;
    for (1..4) {
        $c->request->uri_for("/uri_for")->as_string;
    }
    $c->response->body( $c->request->uri_for("/uri_for")->as_string );
};

get '/ok' => sub {
    my ( $self, $c )  = @_;
    $c->response->body("ok");
};

package main;

use HTTP::Request::Common;
use HTTP::Message::PSGI;
use Plack::Test;
use Plack::Builder;
use Benchmark qw/cmpthese timethese/;

my $env = req_to_psgi(GET "/uri_for");
my $env_ok = req_to_psgi(GET "/ok");
my $app = MyApp->psgi;

cmpthese(timethese(0,{
    'kossy_uri_for' => sub {
        $app->($env);
    },
    'kossy_ok' => sub {
        $app->($env_ok);
    }
}));
Kossy 0.20
Benchmark: running kossy_ok, kossy_uri_for for at least 3 CPU seconds...
  kossy_ok:  3 wallclock secs ( 3.00 usr +  0.01 sys =  3.01 CPU) @ 5860.47/s (n=17640)
kossy_uri_for:  3 wallclock secs ( 3.02 usr +  0.02 sys =  3.04 CPU) @ 1647.04/s (n=5007)
                Rate kossy_uri_for      kossy_ok
kossy_uri_for 1647/s            --          -72%
kossy_ok      5860/s          256%            -- 
Kossy 0.23
Benchmark: running kossy_ok, kossy_uri_for for at least 3 CPU seconds...
  kossy_ok:  4 wallclock secs ( 3.16 usr +  0.00 sys =  3.16 CPU) @ 12560.13/s (n=39690)
kossy_uri_for:  4 wallclock secs ( 3.24 usr +  0.00 sys =  3.24 CPU) @ 4211.73/s (n=13646)
                 Rate kossy_uri_for      kossy_ok
kossy_uri_for  4212/s            --          -66%
kossy_ok      12560/s          198%            --