SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
Gazelle 
Plack Handler for performance freaks 
Yokohama.pm #12 
Masahiro Nagano (kazeburo) 
https://www.!ickr.com/photos/ckindel/424610604/
Me 
• 長野雅広 (Masahiro Nagano) 
• @kazeburo 
• CPAN: KAZEBURO / github: kazeburo 
• 横浜市西区在住 
• ISUCON 2013,2014 優勝
Gazelle
Gazelle #とは 
• Plack Handler / PSGI Server 
• HTTP/1.0 Web Server 
• Preforking Architecture 
• Suitable for running application servers 
behind a reverse proxy 
• Starlet compatible / hot deploy 
• Fast Fast Fast
“Hello World” 
130,000 
97,500 
req/sec nginx starman Starlet Gazelle 
65,000 
32,500 
0 
106,028 
62,069 
33,300 
127,462 
3x Faster!! 
than starman
“counter.psgi” 
110,000 
82,500 
55,000 
27,500 
0 
42,285 
106,028 
28,292 
62,069 
20,100 
33,300 
hello world counter.psgi 
req/sec 
starman Starlet Gazelle
ISUCON4 Quali!er 
45,000 
33,750 
Score 予選通過ラインstarman Starlet Gazelle 
22,500 
11,250 
39,776 42,813 44,764 37,808 
「ISUCON4 予選でアプリケーションを変更せずに予選通過ラインを突破するの術」 
に若干変更を加えたバージョン
Gazelle はなぜ速い 
• Only Support HTTP/1.0 and does not 
support KeepAlive. It make code very simple 
• Mostly written in XS 
• Ultra fast HTTP processing using 
picohttpparser 
• Use accept4(2) 
• Use writev(2) for output responses
Simple HTTP/1.0 GET 
accept4(2) 
read(2) 
parse_header 
poll(2) 
complete? 
OK 
execute app 
writev(2) 
poll(2) 
written? 
close(2) 
No 
OK No
Mostly written in XS 
accept4(2) 
read(2) 
parse_header 
poll(2) 
complete? 
OK 
execute app 
writev(2) 
poll(2) 
written? 
close(2) 
No 
OK No 
XS 
XS
Perl code using XS 
while (1) { 
if ( my ($fd, $buf, $env) = accept_psgi( 
fileno($listen_sock), $timeout, $listen_sock_is_tcp, 
$host || 0, $port || 0 
) ) { 
my $guard = guard { close_client($fd) }; 
$res = Plack::Util::run_app $app, $env; 
my $status_code = $res->[0]; 
my $headers = $res->[1]; 
my $body = $res->[2]; 
write_psgi_response($fd, $timeout, $status_code, $headers, $body); 
}
Perl code using XS 
while (1) { 
if ( my ($fd, $buf, $env) = accept_psgi( 
fileno($listen_sock), $timeout, $listen_sock_is_tcp, 
$host || 0, $port || 0 
) ) { 
my $guard = guard { close_client($fd) }; 
$res = Plack::Util::run_app $app, $env; 
my $status_code = $res->[0]; 
my $headers = $res->[1]; 
my $body = $res->[2]; 
write_psgi_response($fd, $timeout, $status_code, $headers, $body); 
}
picohttpparser 
• created by kazuho-san 
• used in H2O and HTTP::Parser::XS
必読 
http://blog.kazuhooku.com/2014/11/the-internals-h2o-or-how-to-write-fast.html
accept4(2) 
• Required Linux >= 2.6.28 
• Set FD_CLOEXEC and O_NONBLOCK in 
one system call
accept4(2) 
int 
_accept(int fileno, struct sockaddr *addr, unsigned int addrlen) { 
int fd; 
#ifdef SOCK_NONBLOCK 
fd = accept4(fileno, addr, &addrlen, SOCK_CLOEXEC|SOCK_NONBLOCK); 
#else 
fd = accept(fileno, addr, &addrlen); 
fcntl(fd, F_SETFD, FD_CLOEXEC); 
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); 
#endif 
return fd; 
}
accept4(2) 
13:51:26.755628 accept(4, {sa_family=AF_INET, sin_port=htons(42828), 
sin_addr=inet_addr("127.0.0.1")}, [16]) = 5 
13:51:27.324951 fcntl(5, F_SETFD, FD_CLOEXEC) = 0 
13:51:27.325014 fcntl(5, F_GETFL) = 0x2 (flags O_RDWR) 
13:51:27.325067 fcntl(5, F_SETFL, O_RDWR|O_NONBLOCK) = 0 
13:51:27.325117 read(5, "GET / HTTP/1.1rnUser-Agent:"..., 16384) = 155 
13:51:27.325200 setsockopt(5, SOL_TCP, TCP_NODELAY, [1], 4) = 0 
13:52:17.946622 accept4(4, {sa_family=AF_INET, sin_port=htons(42835), 
sin_addr=inet_addr("127.0.0.1")}, [16], SOCK_CLOEXEC|SOCK_NONBLOCK) = 5 
13:52:18.505428 read(5, "GET / HTTP/1.1rnUser-Agent:”..., 16384) = 155 
13:52:18.505519 setsockopt(5, SOL_TCP, TCP_NODELAY, [1], 4) = 0
writev 
• write multiple buffer to a fd in one 
system call 
• reduce memory copy or system calls
ex. memory copy 
#perl 
my $header = ["Server"=>"gazelle","Content-Type"=>"text/plain",...]; 
#xs 
char buf[512]; 
while ( i < av_len(headers) + 1 ) { 
key = SvPV_nolen(*av_fetch(headers,i++,0)); 
strcat(buf, key); 
strcat(buf, ": "); 
val = SvPV_nolen(*av_fetch(headers,i++,0)); 
strcat(buf, val); 
strcat(buf, "rn"); 
} 
write(fd, buf, sizeof(buf)-1); 
Too many 
memory copy 
cause system 
overhead
ex. write write write 
#perl 
my $header = ["Server"=>"gazelle","Content-Type"=>"text/plain"]; 
#xs 
while ( i < av_len(headers) + 1 ) { 
key = SvPV(*av_fetch(headers,i++,0),&len); 
write(fd, key, len); 
write(fd, ": ", sizeof(“: ”) - 1); 
val = SvPV(*av_fetch(headers,i++,0),&len); 
write(fd, val, len); 
write(fd, "rn", sizeof(“rn”) - 1); 
} 
Too many 
write(2) 
increase latency 
of network
writev(2) 
#perl 
my $header = ["Server"=>"gazelle","Content-Type"=>"text/plain"]; 
#xs 
struct iovec v[av_len(headers)+1)*2 + 10]; 
iovcnt = 0; 
while ( i < av_len(headers) + 1 ) { 
key = SvPV(*av_fetch(headers,i++,0),&len); 
iovcnt++; 
v[iovcnt].iov_base = key; 
v[iovcnt].iov_len = len; 
iovcnt++; 
v[iovcnt].iov_base = “: ”; 
no memory copy 
v[iovcnt].iov_len = sizeof(“: ”) - 1; 
one system call 
... 
} 
writev(fd, v, iovcnt);
writev(2) 
plackup -s Gazelle -e 'sub{[200,["Content-Type"=>"text/plain"], 
["xxx","xxx","yyy","yyy","zzzz","n"]]}' 
writev(5, [{"HTTP/1.0 200 OKrnConnection: closernServer: gazeller 
n", 53}, {"Content-Type", 12}, {": ", 2}, {"text/plain", 10}, {"rn", 
2}, {"Date: Fri, 28 Nov 2014 04:38:08 GMTrnrn", 39}, {"xxx", 3}, 
{"xxx", 3}, {"yyy", 3}, {"yyy", 3}, {"zzzz", 4}, {"n", 1}], 12) = 135
高速なサーバを書くには 
• write XS, minimize Perl code 
• reduce system calls 
• Zero Copy
高速なAppサーバって必要なの? 
• ISUCON :) 
• Social Games, AdTech, SNS 
• High optimized applications 
• few msec ~ few tens of msec 
• Several hundreds of request/sec/host 
• 1PVあたりの利益が小さいサービス
Gazelleの実績 
• livedoor Blog 
• 2500万req/day/host 
• Starletからの移行でCPU使用率 
1%~3%さがった
ぜひお使い下さい 
https://www.!ickr.com/photos/superformosa/9057428400/

Más contenido relacionado

La actualidad más candente

"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
Ryosuke IWANAGA
 
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
Graham Dumpleton
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
Yiwei Ma
 

La actualidad más candente (20)

Elasticsearch 설치 및 기본 활용
Elasticsearch 설치 및 기본 활용Elasticsearch 설치 및 기본 활용
Elasticsearch 설치 및 기본 활용
 
Administering and Monitoring SolrCloud Clusters
Administering and Monitoring SolrCloud ClustersAdministering and Monitoring SolrCloud Clusters
Administering and Monitoring SolrCloud Clusters
 
[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화[2D1]Elasticsearch 성능 최적화
[2D1]Elasticsearch 성능 최적화
 
Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기
 
Java/Spring과 Node.js의공존
Java/Spring과 Node.js의공존Java/Spring과 Node.js의공존
Java/Spring과 Node.js의공존
 
Remove php calls and scale your site like crazy !
Remove php calls and scale your site like crazy !Remove php calls and scale your site like crazy !
Remove php calls and scale your site like crazy !
 
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
 
ニコニコ動画を検索可能にしてみよう
ニコニコ動画を検索可能にしてみようニコニコ動画を検索可能にしてみよう
ニコニコ動画を検索可能にしてみよう
 
Puppet at janrain
Puppet at janrainPuppet at janrain
Puppet at janrain
 
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 
Fluentd and Embulk Game Server 4
Fluentd and Embulk Game Server 4Fluentd and Embulk Game Server 4
Fluentd and Embulk Game Server 4
 
NoSQL атакует: JSON функции в MySQL сервере.
NoSQL атакует: JSON функции в MySQL сервере.NoSQL атакует: JSON функции в MySQL сервере.
NoSQL атакует: JSON функции в MySQL сервере.
 
Web前端性能优化 2014
Web前端性能优化 2014Web前端性能优化 2014
Web前端性能优化 2014
 
Py conkr 20150829_docker-python
Py conkr 20150829_docker-pythonPy conkr 20150829_docker-python
Py conkr 20150829_docker-python
 
아파트 정보를 이용한 ELK stack 활용 - 오근문
아파트 정보를 이용한 ELK stack 활용 - 오근문아파트 정보를 이용한 ELK stack 활용 - 오근문
아파트 정보를 이용한 ELK stack 활용 - 오근문
 
Automated Java Deployments With Rpm
Automated Java Deployments With RpmAutomated Java Deployments With Rpm
Automated Java Deployments With Rpm
 
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
 
To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)
 
Creating Reusable Puppet Profiles
Creating Reusable Puppet ProfilesCreating Reusable Puppet Profiles
Creating Reusable Puppet Profiles
 

Similar a Gazelle - Plack Handler for performance freaks #yokohamapm

Hacking Mac OSX Cocoa API from Perl
Hacking Mac OSX Cocoa API from PerlHacking Mac OSX Cocoa API from Perl
Hacking Mac OSX Cocoa API from Perl
typester
 
Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門
lestrrat
 
Nko workshop - node js crud & deploy
Nko workshop - node js crud & deployNko workshop - node js crud & deploy
Nko workshop - node js crud & deploy
Simon Su
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
yiditushe
 

Similar a Gazelle - Plack Handler for performance freaks #yokohamapm (20)

Hacking Mac OSX Cocoa API from Perl
Hacking Mac OSX Cocoa API from PerlHacking Mac OSX Cocoa API from Perl
Hacking Mac OSX Cocoa API from Perl
 
Using ngx_lua in UPYUN
Using ngx_lua in UPYUNUsing ngx_lua in UPYUN
Using ngx_lua in UPYUN
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.js
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
 
Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門Kansai.pm 10周年記念 Plack/PSGI 入門
Kansai.pm 10周年記念 Plack/PSGI 入門
 
Performance patterns
Performance patternsPerformance patterns
Performance patterns
 
Puppet @ Seat
Puppet @ SeatPuppet @ Seat
Puppet @ Seat
 
Going crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHPGoing crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHP
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Nko workshop - node js crud & deploy
Nko workshop - node js crud & deployNko workshop - node js crud & deploy
Nko workshop - node js crud & deploy
 
Groovy on the Shell
Groovy on the ShellGroovy on the Shell
Groovy on the Shell
 
Usp
UspUsp
Usp
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 
Nodejs - A-quick-tour-v3
Nodejs - A-quick-tour-v3Nodejs - A-quick-tour-v3
Nodejs - A-quick-tour-v3
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 

Más de Masahiro Nagano

ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
Masahiro Nagano
 
Isucon makers casual talks
Isucon makers casual talksIsucon makers casual talks
Isucon makers casual talks
Masahiro Nagano
 
blogサービスの全文検索の話 - #groonga を囲む夕べ
blogサービスの全文検索の話 - #groonga を囲む夕べblogサービスの全文検索の話 - #groonga を囲む夕べ
blogサービスの全文検索の話 - #groonga を囲む夕べ
Masahiro Nagano
 
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Masahiro Nagano
 
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LTWeb Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Masahiro Nagano
 
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
Masahiro Nagano
 
Webアプリケーションの パフォーマンス向上のコツ 実践編
 Webアプリケーションの パフォーマンス向上のコツ 実践編 Webアプリケーションの パフォーマンス向上のコツ 実践編
Webアプリケーションの パフォーマンス向上のコツ 実践編
Masahiro Nagano
 
Webアプリケーションの パフォーマンス向上のコツ 概要編
 Webアプリケーションの パフォーマンス向上のコツ 概要編 Webアプリケーションの パフォーマンス向上のコツ 概要編
Webアプリケーションの パフォーマンス向上のコツ 概要編
Masahiro Nagano
 
Webアプリケーションとメモリ
WebアプリケーションとメモリWebアプリケーションとメモリ
Webアプリケーションとメモリ
Masahiro Nagano
 
最近作ったN個のCPANモジュール Yokohama.pm #10
最近作ったN個のCPANモジュール Yokohama.pm #10最近作ったN個のCPANモジュール Yokohama.pm #10
最近作ったN個のCPANモジュール Yokohama.pm #10
Masahiro Nagano
 
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
Masahiro Nagano
 

Más de Masahiro Nagano (20)

Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/MinAdvanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
 
Stream processing in Mercari - Devsumi 2015 autumn LT
Stream processing in Mercari - Devsumi 2015 autumn LTStream processing in Mercari - Devsumi 2015 autumn LT
Stream processing in Mercari - Devsumi 2015 autumn LT
 
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月
 
ISUCONの勝ち方 YAPC::Asia Tokyo 2015
ISUCONの勝ち方 YAPC::Asia Tokyo 2015ISUCONの勝ち方 YAPC::Asia Tokyo 2015
ISUCONの勝ち方 YAPC::Asia Tokyo 2015
 
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LTNorikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT
 
メルカリでのNorikraの活用、 Mackerelを添えて
メルカリでのNorikraの活用、 Mackerelを添えてメルカリでのNorikraの活用、 Mackerelを添えて
メルカリでのNorikraの活用、 Mackerelを添えて
 
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LTGazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT
 
Mackerel & Norikra mackerel meetup #4 LT
Mackerel & Norikra mackerel meetup #4 LTMackerel & Norikra mackerel meetup #4 LT
Mackerel & Norikra mackerel meetup #4 LT
 
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
 
Isucon makers casual talks
Isucon makers casual talksIsucon makers casual talks
Isucon makers casual talks
 
blogサービスの全文検索の話 - #groonga を囲む夕べ
blogサービスの全文検索の話 - #groonga を囲む夕べblogサービスの全文検索の話 - #groonga を囲む夕べ
blogサービスの全文検索の話 - #groonga を囲む夕べ
 
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
 
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LTWeb Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
 
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
 
Webアプリケーションの パフォーマンス向上のコツ 実践編
 Webアプリケーションの パフォーマンス向上のコツ 実践編 Webアプリケーションの パフォーマンス向上のコツ 実践編
Webアプリケーションの パフォーマンス向上のコツ 実践編
 
Webアプリケーションの パフォーマンス向上のコツ 概要編
 Webアプリケーションの パフォーマンス向上のコツ 概要編 Webアプリケーションの パフォーマンス向上のコツ 概要編
Webアプリケーションの パフォーマンス向上のコツ 概要編
 
Webアプリケーションとメモリ
WebアプリケーションとメモリWebアプリケーションとメモリ
Webアプリケーションとメモリ
 
最近作ったN個のCPANモジュール Yokohama.pm #10
最近作ったN個のCPANモジュール Yokohama.pm #10最近作ったN個のCPANモジュール Yokohama.pm #10
最近作ったN個のCPANモジュール Yokohama.pm #10
 
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
 
MHA for MySQL の話
MHA for MySQL の話MHA for MySQL の話
MHA for MySQL の話
 

Último

VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Enjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort Service
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 

Gazelle - Plack Handler for performance freaks #yokohamapm

  • 1. Gazelle Plack Handler for performance freaks Yokohama.pm #12 Masahiro Nagano (kazeburo) https://www.!ickr.com/photos/ckindel/424610604/
  • 2. Me • 長野雅広 (Masahiro Nagano) • @kazeburo • CPAN: KAZEBURO / github: kazeburo • 横浜市西区在住 • ISUCON 2013,2014 優勝
  • 4. Gazelle #とは • Plack Handler / PSGI Server • HTTP/1.0 Web Server • Preforking Architecture • Suitable for running application servers behind a reverse proxy • Starlet compatible / hot deploy • Fast Fast Fast
  • 5. “Hello World” 130,000 97,500 req/sec nginx starman Starlet Gazelle 65,000 32,500 0 106,028 62,069 33,300 127,462 3x Faster!! than starman
  • 6. “counter.psgi” 110,000 82,500 55,000 27,500 0 42,285 106,028 28,292 62,069 20,100 33,300 hello world counter.psgi req/sec starman Starlet Gazelle
  • 7. ISUCON4 Quali!er 45,000 33,750 Score 予選通過ラインstarman Starlet Gazelle 22,500 11,250 39,776 42,813 44,764 37,808 「ISUCON4 予選でアプリケーションを変更せずに予選通過ラインを突破するの術」 に若干変更を加えたバージョン
  • 8. Gazelle はなぜ速い • Only Support HTTP/1.0 and does not support KeepAlive. It make code very simple • Mostly written in XS • Ultra fast HTTP processing using picohttpparser • Use accept4(2) • Use writev(2) for output responses
  • 9. Simple HTTP/1.0 GET accept4(2) read(2) parse_header poll(2) complete? OK execute app writev(2) poll(2) written? close(2) No OK No
  • 10. Mostly written in XS accept4(2) read(2) parse_header poll(2) complete? OK execute app writev(2) poll(2) written? close(2) No OK No XS XS
  • 11. Perl code using XS while (1) { if ( my ($fd, $buf, $env) = accept_psgi( fileno($listen_sock), $timeout, $listen_sock_is_tcp, $host || 0, $port || 0 ) ) { my $guard = guard { close_client($fd) }; $res = Plack::Util::run_app $app, $env; my $status_code = $res->[0]; my $headers = $res->[1]; my $body = $res->[2]; write_psgi_response($fd, $timeout, $status_code, $headers, $body); }
  • 12. Perl code using XS while (1) { if ( my ($fd, $buf, $env) = accept_psgi( fileno($listen_sock), $timeout, $listen_sock_is_tcp, $host || 0, $port || 0 ) ) { my $guard = guard { close_client($fd) }; $res = Plack::Util::run_app $app, $env; my $status_code = $res->[0]; my $headers = $res->[1]; my $body = $res->[2]; write_psgi_response($fd, $timeout, $status_code, $headers, $body); }
  • 13. picohttpparser • created by kazuho-san • used in H2O and HTTP::Parser::XS
  • 15. accept4(2) • Required Linux >= 2.6.28 • Set FD_CLOEXEC and O_NONBLOCK in one system call
  • 16. accept4(2) int _accept(int fileno, struct sockaddr *addr, unsigned int addrlen) { int fd; #ifdef SOCK_NONBLOCK fd = accept4(fileno, addr, &addrlen, SOCK_CLOEXEC|SOCK_NONBLOCK); #else fd = accept(fileno, addr, &addrlen); fcntl(fd, F_SETFD, FD_CLOEXEC); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); #endif return fd; }
  • 17. accept4(2) 13:51:26.755628 accept(4, {sa_family=AF_INET, sin_port=htons(42828), sin_addr=inet_addr("127.0.0.1")}, [16]) = 5 13:51:27.324951 fcntl(5, F_SETFD, FD_CLOEXEC) = 0 13:51:27.325014 fcntl(5, F_GETFL) = 0x2 (flags O_RDWR) 13:51:27.325067 fcntl(5, F_SETFL, O_RDWR|O_NONBLOCK) = 0 13:51:27.325117 read(5, "GET / HTTP/1.1rnUser-Agent:"..., 16384) = 155 13:51:27.325200 setsockopt(5, SOL_TCP, TCP_NODELAY, [1], 4) = 0 13:52:17.946622 accept4(4, {sa_family=AF_INET, sin_port=htons(42835), sin_addr=inet_addr("127.0.0.1")}, [16], SOCK_CLOEXEC|SOCK_NONBLOCK) = 5 13:52:18.505428 read(5, "GET / HTTP/1.1rnUser-Agent:”..., 16384) = 155 13:52:18.505519 setsockopt(5, SOL_TCP, TCP_NODELAY, [1], 4) = 0
  • 18. writev • write multiple buffer to a fd in one system call • reduce memory copy or system calls
  • 19. ex. memory copy #perl my $header = ["Server"=>"gazelle","Content-Type"=>"text/plain",...]; #xs char buf[512]; while ( i < av_len(headers) + 1 ) { key = SvPV_nolen(*av_fetch(headers,i++,0)); strcat(buf, key); strcat(buf, ": "); val = SvPV_nolen(*av_fetch(headers,i++,0)); strcat(buf, val); strcat(buf, "rn"); } write(fd, buf, sizeof(buf)-1); Too many memory copy cause system overhead
  • 20. ex. write write write #perl my $header = ["Server"=>"gazelle","Content-Type"=>"text/plain"]; #xs while ( i < av_len(headers) + 1 ) { key = SvPV(*av_fetch(headers,i++,0),&len); write(fd, key, len); write(fd, ": ", sizeof(“: ”) - 1); val = SvPV(*av_fetch(headers,i++,0),&len); write(fd, val, len); write(fd, "rn", sizeof(“rn”) - 1); } Too many write(2) increase latency of network
  • 21. writev(2) #perl my $header = ["Server"=>"gazelle","Content-Type"=>"text/plain"]; #xs struct iovec v[av_len(headers)+1)*2 + 10]; iovcnt = 0; while ( i < av_len(headers) + 1 ) { key = SvPV(*av_fetch(headers,i++,0),&len); iovcnt++; v[iovcnt].iov_base = key; v[iovcnt].iov_len = len; iovcnt++; v[iovcnt].iov_base = “: ”; no memory copy v[iovcnt].iov_len = sizeof(“: ”) - 1; one system call ... } writev(fd, v, iovcnt);
  • 22. writev(2) plackup -s Gazelle -e 'sub{[200,["Content-Type"=>"text/plain"], ["xxx","xxx","yyy","yyy","zzzz","n"]]}' writev(5, [{"HTTP/1.0 200 OKrnConnection: closernServer: gazeller n", 53}, {"Content-Type", 12}, {": ", 2}, {"text/plain", 10}, {"rn", 2}, {"Date: Fri, 28 Nov 2014 04:38:08 GMTrnrn", 39}, {"xxx", 3}, {"xxx", 3}, {"yyy", 3}, {"yyy", 3}, {"zzzz", 4}, {"n", 1}], 12) = 135
  • 23. 高速なサーバを書くには • write XS, minimize Perl code • reduce system calls • Zero Copy
  • 24. 高速なAppサーバって必要なの? • ISUCON :) • Social Games, AdTech, SNS • High optimized applications • few msec ~ few tens of msec • Several hundreds of request/sec/host • 1PVあたりの利益が小さいサービス
  • 25. Gazelleの実績 • livedoor Blog • 2500万req/day/host • Starletからの移行でCPU使用率 1%~3%さがった