2æ¥åã«mrubyã®ç»åå¤æmrbgemä¸ããã¨ã¤ã¶ããã¦ããã®å¾2æ¥ä»¥å
ã«gdã¨Magick++ã®mrbgemãéçºããããã®ã¹ãã¼ãæãmrubyã https://t.co/RzTZJsX9vP https://t.co/4YkJ2PwISQ
— MATSUMOTO, Ryosuke (@matsumotory) 2015, 4æ 25
ã¨ããããã«ãmrubyã§ç»åå¤æãã¦ã¿ãããªã¼ã¨åãã¨ãã£ã¨ããã¾ã«ç»åå¤æmrbgemãéçºããã¦ãã¾ãmrubyã§ããããã®ã¾ã¾ã§ã¯åããå½äºè ã¨ãã¦ãªãã ãç³ã訳ãªãã®ã§ãngx_mrubyã使ã£ã¦ãµã¯ã£ã¨ç»åå¤æãµã¼ããã§ããªãã試ãã¦ã¿ã¾ããã
çµè«ã¨ãã¦ã¯ãç°¡åãªãã®ã¯ãµã¯ã£ã¨ã§ãã¦ãã¾ãã¾ããã以éã§ã¯ä½ãæ¹ãç°¡åã«ç´¹ä»ãã¾ãã
ããããã®ãæ°åã§ã§ãã¦ãã¾ãã®ãmod_mrubyãngx_mrubyãããã«ã¯trusterdã®è¯ãã§ãããã
ngx_mruby + mruby-mrmagickã使ã
mruby-mrmagickã使ã£ãã®ã¯ãããã¨è¦ãæãREADMEãæ¸ããã¦ãã¦æã£åãæ©ã試ãããã ã£ãããã§ããä»ã§ã¯mruby-gdãªã©ããã£ã¦ãä»å¾è²ã ã¨å¢ãã¦ããã§ãããã
以ä¸è¨å®ã®æé ã§ãã
1. build_config.rb
ã«ä»¥ä¸ã®ãªã³ã¯è¨å®ã追è¨
conf.gem :github => 'kjunichi/mruby-mrmagick'
ãã®è¨å®ã追è¨ããä¸ã§ngx_mrubyããã«ããã¾ãã
2. nginx.conf
ã«Rubyã¹ã¯ãªããããã¯è¨å®ã追å
location /image {
mruby_content_handler build/nginx/html/resize.rb cache;
}
ä¸è¨ã®ããã«ã/image
ã«ã¢ã¯ã»ã¹ããã£ããç»åå¤æããã¹ã¯ãªãããããã¯ããããã«nginxã«è¨å®ãã¾ãã
3. build/nginx/html/resize.rb
å®è£
r = Nginx::Request.new if File.extname(r.filename) == ".jpeg" type = r.var.arg_type if r.var.is_args == "?" if type == "half" new_file = r.filename + "_" + type + ".jpeg" img = Mrmagick::ImageList.new(r.filename) unless File.exist? new_file new_img = img.scale(0.5) new_img.write(new_file) end r.uri = r.uri + "_" + type + ".jpeg" end end Nginx.return Nginx::DECLINED
å®éã®ã¹ã¯ãªããã¯ä¸è¨ã®ããã«ãã¾ãããåç´ã«ãURLã¯ã¨ãªãã©ã¡ã¼ã¿ã解æãã¦ãtype=half
ã ã£ãã縦横ããããååã«ãªãµã¤ãºããã¨ããå¦çã§ãã
ã¾ãããã§ã«ååã«ããç»åãçæããã¦ããããå度ãªãµã¤ãºããã«ãã§ã«ãããªãµã¤ãºæ¸ã¿ã®ç»åãè¿ãã¾ãã
ããã§ã
curl -O http://127.0.0.1/image/matsumotory.jpeg
ã¨ã
curl -O http://127.0.0.1/image/matsumotory.jpeg?type=half
ã¨ãããã¨ãç¡äºãªãµã¤ãºãããç»åãçæãããä¸ã§ãã¬ã¹ãã³ã¹ã§ã縦横ååã«ãªã£ãç»åã表示ããã¾ããã
åèãã³ããã¼ã¯
ã¨ãããã§ä¾ã®ãã¨ãã¨ãããããã³ããã¼ã¯ãã¨ã£ã¦ã¿ã¾ããã
ãªãµã¤ãºæ©è½ç¡ãã§ãªãªã¸ãã«ç»åã¢ã¯ã»ã¹
$ ab -c 5 -n 10000 http://127.0.0.1:58080/image/matsumotory.jpeg Document Path: /image/matsumotory.jpeg Document Length: 15350 bytes Complete requests: 10000 Failed requests: 0 Total transferred: 155878192 bytes HTML transferred: 153507955 bytes Requests per second: 8339.72 [#/sec] (mean)
ãªãµã¤ãºæ©è½ããã§ãªãªã¸ãã«ç»åã¢ã¯ã»ã¹
$ ab -c 5 -n 10000 http://127.0.0.1:58080/image/matsumotory.jpeg Document Path: /image/matsumotory.jpeg Document Length: 15350 bytes Complete requests: 10000 Failed requests: 0 Total transferred: 155870000 bytes HTML transferred: 153500000 bytes Requests per second: 8052.35 [#/sec] (mean)
ãªãµã¤ãºæ©è½ãå ¥ããã¨ãªãªã¸ãã«ç»åã¢ã¯ã»ã¹ã§ãç´3%ã®æ§è½å£åãã¾ãããRubyã®ããã¯ã¨ãã®ä¸ã®æ¡ä»¶åé¢å¦çã®ããã ããã¨æãã¾ããã¨ã¯ããæ§è½å£åã¯è»½å¾®ã§ãã
ãªãµã¤ãºæ©è½ããã§ãªãµã¤ãºç»åã¢ã¯ã»ã¹
$ ab -c 5 -n 10000 http://127.0.0.1:58080/image/matsumotory.jpeg?type=half Document Path: /image/matsumotory.jpeg?type=half Document Length: 5117 bytes Complete requests: 10000 Failed requests: 0 Total transferred: 53530000 bytes HTML transferred: 51170000 bytes Requests per second: 7327.31 [#/sec] (mean)
æåã®ä¸åã¯ç»åããªãµã¤ãºããã¦æ°ããªãã¡ã¤ã«ã¨ãã¦ä¿åããããã®å¾ã¯ä¿åããããã¡ã¤ã«ãè¿ãã®ã§ãå ¨ä½ã¨ãã¦Rubyã§ãã®è¾ºãã®å¤å®ãè¡ã£ã¦ãã¦ããããªã«ã¬ãã³ã¨ã¯é ããªã£ã¦ããªãããã§ãã
å¾ã¯ãnginxä¸ã§ç»åå¤æãããã¨ã«ããCPUãã¦ã³ãã®å¦çãã©ãããããã¾ãããã¡ã¤ã«å¤æçã«ããå¦çã®ãããã¯ãã©ããããã¨ãã£ã課é¡ã¯ããã¾ãããä»åã®ç®çã§ãããµã¯ãã¨ä½ã£ã¦ã¿ãã¨ããç®çã¯æããã¾ããã
ã¾ã¨ã
ã¨ãããã¨ã§ãngx_mrubyã¨ç»åå¤æmrbgemã使ã£ã¦ç°¡åãªç»åå¤æãµã¼ããå®è£ ãã¦ã¿ã¾ãããããããå¦çãRubyã§ç°¡åã«ãµã¯ãã¨æ¸ãã¦ãã¾ãã®ãngx_mrubyãmod_mrubyã®æ¥½ããã ã¨æãã¾ããã¾ããTrusterdã§ãåæ§ã®å¦çãã§ããã®ã§ãHTTP/2ãªç»åå¤æãµã¼ããç°¡åã«ã§ãããã§ãã
ä»å¾ãããã§ã¯ãããã£ãæè¡ã調æ»ããªããããã¡ãªãããã¡ãªãããæ¤è¨¼ãã¤ã¤ç©æ¥µçã«åãçµãã§ããäºå®ã§ãããããã£ãåãçµã¿ãè¡ãäºã§ãRubyãmrubyã®å ¨ç¤¾çãªæè¡ãåºä¸ãããããmrbgemãä½ããã¹ãã«ãã¤ãããã¨ã§Webãµã¼ãï¼HTTP/2å«ãï¼ãèªç±ã«æ¡å¼µã§ããããã«ãªãã¨è¯ãã§ããã
ã¾ãä»å¾ãæè¬Middleware as Codeçãªã話ãç¶ãã¦ããäºå®ã§ãã®ã§ãã©ãããããããé¡ããã¾ãã