Gazelle & CPAN modules
for Performance
長野雅広 @kazeburo
Shibuya Perl Mongers テクニカルトーク #17 LT
Me
• 長野雅広(Masahiro Nagano)
• @kazeburo
• Mercari, Inc.
• Operations Engineer, Site Reliability
• PHP, YAML
Gazellehttps://www.flickr.com/photos/ckindel/424610604/
Gazelle
• Plack Handler / PSGI Server
• HTTP/1.1 Web Server
• Starlet compatible / Hot Deploy
• Fast
Benchmark
0
100000
200000
300000
400000
starman Starlet Gazelle
96577
60562
43588
334752
92027
48888
hello world counter
https://gist.github.com/kazeburo/71603b4bc0994e2222b4
なぜGazelleは速いのか
• Simple Architecture
• Prefork
• no keepalive support
• Mostly written in XS
• Ultra fast HTTP processing using
picohttpparser
• Use accept4(2) and writev(2)
Gazelleの使いどころ
• ISUCON!!!1
• ソーシャルゲーム、広告、大規模サイト
• 高度に最適化されたWebアプリケーショ
ン
• 普通に使えます
実績
• livedoorBlog
• 日本最大級のBlogサービス
• Starletからの移行でCPU使用率が
1~3%下がった
WWW::Form::UrlEncoded
WWW::Form::UrlEncoded::XS
parser and builder for application/x-www-form-urlencoded
s_id=1&type=foo&message1=foo+bar+baz+hoge
+hoge+hoge+hoge+hogehogemessage2=
%E6%97%A5%E6%9C%AC%E8%AA%9E
%E3%81%A7%E3%81%99%E3%82%88%E3%83%BC
(s_id => 1, type => 'foo', message1 =>
'foo bar baz hoge hoge hoge hoge
hogehoge', message2 => '日本語ですよー')
parse build
Rate wwwform_pp wwwform_xs urlencode_xs
wwwform_pp 21764/s -- -90% -91%
wwwform_xs 227555/s 946% -- -1%
urlencode_xs 229375/s 954% 1% --
parseのベンチマークで9.5倍
* URL::Encode(::XS)は若干互換性がない
Rate use_uri concat_escape build_xs
use_uri 23640/s -- -32% -97%
concat_escape 34796/s 47% -- -96%
build_xs 843158/s 3467% 2323% --
buildのベンチマークで35倍
W::F::UE::XS使いどころ
• Query String付きのリクエストが非常に多い
ところ
• Plack::Requestと互換性あるので置き換え可
能
• Query String付きのURLを大量に生成する場合
• URL::Builderがおすすめ
Cookie::Baker
Cookie::Baker::XS
my $cookie = bake_cookie('foo', {
value => 'val',
path => "test",
domain => '.example.com',
expires => '+24h'
});
$headers->push_header(‘Set-Cookie’,$cookie);
my $cookie_hashref = crush_cookie($headers->header(‘Cookie’));
build
parse
Benchmark: running pp, xs for at least 1 CPU seconds...
crush_pp: 1 wallclock secs @ 16592.59/s (n=17920)
crush_xs: 1 wallclock secs @ 182043.81/s (n=191146)
Rate pp xs
crush_pp 16593/s -- -91%
crush_xs 182044/s 997% --
Benchmark: running pp, xs for at least 1 CPU seconds...
crush_pp: 2 wallclock secs @ 201749.06/s (n=213854)
crush_xs: 0 wallclock secs @ 1042617.50/s (n=1251141)
Rate pp xs
crush_pp 201749/s -- -81%
crush_xs 1042618/s 417% --
## length($cookie) == 675
## length($cookie) == 17
C::B使いどころ
• Cookie::BakerはすでにPlack@1.0035-
TRIALで使われている
• Cookieのparseだけを行うシンプルな
モジュールがなかった
• Cookie::Baker::XSを入れる事
で”parse”が高速化
C::B::XS使いどころ
• Cookieがすごくたくさん付いて来るサ
イト
• Google Analytics, Ninja, Ad, Ad
tracking, Session and more
使ってくださいまし

Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT