Unicornã§Sinatraã¢ããªããããã¤ãã¦ã¿ã
ãæè¿ã¯ä»äºã§Sinatraã¢ããªãæ¸ããããã¦ããã®ã§ãSinatraã¢ããªãåããããã«ã¯ã©ã®HTTPãµã¼ãã使ãã®ããã¹ããªã®ããæ°ã«ãªã£ã¦ãããï¼å
ã«çµè«ãæ¸ãã¦ããããã©ãã©ãããã¹ããã¨ããå¯ä¸ã®é¸æè¢ã¯ä»ã®ã¨ããããã¾ãããé©æé©æã§ããï¼
ãSinatraã¯Rackã®ä¸ã«æ§ç¯ããã¦ããã®ã§ãRackã«å¯¾å¿ããHTTPãµã¼ãã¼ã使ã£ã¦åããäºã«ãªãã®ã ãããã®æ°ããããã¨å¤ããã©ãã使ãã°ããã®ãè¿·ãã代表çãªãã®ãæããã ãã§ããWebRick, Mongrel, Thin, Unicorn, Passenger(Apacheã¨ãã«çµã¿è¾¼ãã§ä½¿ããã¤), FastCGI, (æ®éã®)CGIããããããã¯é¸æè¢ãããï¼ããã¤ãHTTPãµã¼ããããªãç©ãæ··ãã£ã¦ããããRackã対å¿ãã¦ãã¨ããç¹ã¯å
±éãã¦ããï¼ã
ãWebRickã¯ããããããã©ã¼ãã³ã¹ã«éç¹ãç½®ãã¦ããªãããMongrelã¯ã¡ã³ãããã¦ãªãããæ°è¦ã§ä½¿ãããã¯ãªããPassengerã¯é«è² è·æã«é¢ãã¦ããäºãèããªããFastCGIã¯æ
å ±ããªãã¦ä¸å®ãCGIã¯è«å¤ãã¨ããäºã§ãããã¾ã§ã¯ããã©ã¼ãã³ã¹ãå¿
è¦ãªå ´åã¯Thinãããã§ãªãå ´åã¯ããã©ã«ãã§å
¥ã£ã¦ãã¦ä½¿ããããWebRickã¨ãã使ãåãããã¦ããã®ã ããæè¿ã¯Unicornãæ°ã«ãªã£ã¦ããã
ãåç½®ããããã¶ãã¨é·ããªã£ãããããããããã§ãUnicornãä¸åº¦ã試ãã¦ã¿ããã¨ã«ããã
Unicornã®ç¹å¾´
ãUnicornãç´¹ä»ãã¦ããããã°è¨äºãããã¤ãèªãã§ã¿ãã®ã ããUnicornã¯ããã¾ã§ã®HTTPãµã¼ãã¨ã¯ãã£ããéãã¢ã¼ããã¯ãã£ãæ¡ç¨ãã¦ããäºãåãã£ãã
ãUnicornã«ã¯ããã«ãã¹ã¬ããã ã®epollã ã®ã1ããã»ã¹ãããã®åææ¥ç¶æ°ãä¸ããããã®å·¥å¤«ã¯ä¸åãªãã1ããã»ã¹ã§ã¯åæã«å¦çã§ãããªã¯ã¨ã¹ãã¯ãããã«1ã¤ã§ããããã®ä»£ããã«ãããã¤ãã®å·¥å¤«ãæ½ããã¦ããã
ãä¾ãã°ãRailsãSinatraã®ã¢ããªããã¼ãããå¾ã«ããã»ã¹ããã©ã¼ã¯ããäºãã§ãããããã«ãã£ã¦ãã³ã¼ãã®ãã¼ãã1åããè¡ãããã«ããã®ã§ãèµ·åãé常ã«é«éã«ãªããæè¿ã®ãµã¼ãã¼ã ã¨æ°åããã»ã¹ç¨åº¦ã¯ä½è£ãæã£ã¦åãããã¨ãã§ãããããã®å ´åãRailsã1ããã»ã¹èµ·åããã®ã«0.5ç§ã§ããã¨ãã¦ããæ°åããã»ã¹ã®åèµ·åã«ã¯åç§ä»¥ä¸ã¯ãããè¨ç®ã«ãªããèµ·åãããã»ã¹ã®ãã©ã¼ã¯ã§æ¸ããªãåèµ·åã¯1ç§ä»¥å
ã«å®äºãããå®éã«ã¯èµ·åã並åã§èµ°ãããããã ããããããã¾ã§ã®å·®ã¯ã¤ããªãã ããããã©ããã©ã¼ã¯ã§æ¸ãæ¹ãå§åçã«éãã®ã¯ç¢ºãã ã
ãã¾ããé常ã®ãªãã¼ã¹ãããã·ã§ã¯ãªã¯ã¨ã¹ãã¯ã©ã¦ã³ãããã³ãªã©ã§ç©ºãã¦ãããªããã»ã¹ã«éãã¤ããããããUnicornã§ã¯ç©ºãã¦ããããã»ã¹ãèªåã§ãªã¯ã¨ã¹ããåãã«ãããã¨ãã§ãããæ¨å¥¨ããã¦ããæ§æã§ã¯ãæåã«ãªãã¼ã¹ãããã·ã¨ãã¦nginxãç«ã¦ãnginxããUnix Domain Socketã«ãªã¯ã¨ã¹ããæ¸ãè¾¼ããæ¸ãè¾¼ã¾ãããªã¯ã¨ã¹ãã¯ç©ºãã¦ããUnicornãåãã«ãããåæã«è¤æ°ã®Unicornããã»ã¹ã空ãã¦ããå ´åãOSã®ã¹ã±ã¸ã¥ã¼ã©ã¼ãå
ã«åãããæ¹ã®ããã»ã¹ããªã¯ã¨ã¹ããåã£ã¦å¦çãããã¨ã«ãªãã
ãUnicornã®ã¢ã¼ããã¯ãã£ã¯ã大éã®ã¢ã¯ã»ã¹ãå®å®ãã¦ãã°ãããã®ã¢ã¼ããã¯ãã£ã§ãããå®å®ãã¦ãã®é¨åãé常ã«éè¦ã§ãåææ¥ç¶æ°ã1ã¤ï¼ãµã¼ãã¯è¤æ°ã®ãªã¯ã¨ã¹ããåæã«å¦çããªãï¼ã¨ããäºã¯ãä¸è¦ããã¼ãããã«æããããã並åã«ãªã¯ã¨ã¹ããå¦çããå ´åã¨æ¯ã¹ã¦ã³ã¼ããã·ã³ãã«ã«æ¸ããã·ã³ãã«ã§ããã¨ãããã¨ã¯ãã°ãåºã«ããå®å®ãã¦ããã¨ããäºã§ãããã¾ããã©ããã¦ãããã»ã¹ã殺ããªãã¨ãããªãå ´åã«ããå·»ãæ·»ããé£ããªã¯ã¨ã¹ããæå°ã§æ¸ããUnicornã¯åä¸ããã»ã¹ã§ã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãåã訳ã ããã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ã«ãããããéçºã³ã¹ãã¯ä¸è¬ã«ããã«ã¦ã§ã¢ã®éçºã³ã¹ããããä½ãå ´åãå¤ãããããã£ã¦ããã«ã¦ã§ã¢ã»ã©ä¿¡ç¨ã¯ç½®ããªãããã®ãããªããã»ã¹ãç¡éã«killããäºã¯å±éºã§ãããï¼æ¬å½ã¯å®å
¨ãªã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¹ããªãã ãã©ãï¼Unicornããªã¯ã¨ã¹ããåãã«ããã¨ããç¹ãããã¾ãã¾æ··éããããã»ã¹ã«ãªã¯ã¨ã¹ããéãã¤ããå±éºæ§ããªãããã¨ããã¡ãªãããããããªãã¼ã¹ãããã·ããã£ãããã¦ããã°ãã¢ã¯ã»ã¹ãéä¸ããã¨ãã¦ããUnicornã§æããããã ãã®ãªã¯ã¨ã¹ãã¯ãã³ã°ããã«å¦çãããã¨ãã§ããã
ãå®å®ãã¦ãªã¯ã¨ã¹ãã®åããã¼ããå°ãªããã¾ãéç¨ã楽ã§ãããã¨ããã¨ããã«ä¸»ç¼ãç½®ãããã¢ã¼ããã¯ãã£ã§ããã¨æããã
Unicornã®å¶é
ãUnicornã®ã¢ã¼ããã¯ãã£ã¯ä¸è½ã§ã¯ãªããä¾ãã°ãUnicornãåç¬ã§ä½¿ããã¨ã¯é¿ããã¹ãã§ãããåææ¥ç¶æ°ãåããã»ã¹ã«1ã¤ãã¤ãããªãã®ã§ãã¤ã³ã¿ã¼ãããã®ã©ããã®ã¯ã©ã¤ã¢ã³ãããã®ã¢ã¯ã»ã¹ãç´æ¥åãä»ããã¨ãä¾ãã°ãã©ã¸ã«ãããªã¯ã¨ã¹ããæ¥ã¦ã3ç§ãããããã»ã¹ãå æãããããããªããç©ççã«é ãã«ãµã¼ãã¼ãããã¨ãã¼ã¿ã®éåä¿¡ã«ã©ããã¦ãæéããããã®ã§ããã®éããã»ã¹ãå æããã¦ãã¾ãããããã訳ã§ãã¤ã³ã¿ã¼ãããããã®ãªã¯ã¨ã¹ããç´æ¥å¦çãããã¨ã¯ãUnicornã«ã¨ã£ã¦ã¯è´å½çãªæ§è½ã®å£åãæãå¯è½æ§ããããããã¯åä¸LANå
ããè² è·ãã¹ãããã¦ããã ãã§ã¯è¦ã¤ãã«ããåé¡ã§ãããã¯ã¾ãã¨ãã£ãããªã®ã§ãUnicornã使ãéã«ã¯ãã®äºã¯é ã®çé
ã«çãã¦ãããæ¹ãè¯ãããã®å¶éãåé¿ããããã«ãnginxãapacheãªã©ããªãã¼ã¹ãããã·ã¨ãã¦Unicornã®åã«è¨ç½®ãã¦ããªã¯ã¨ã¹ããã¯ã©ã¤ã¢ã³ãã«éä¿¡ããé¨åã¯ä¸¦åæ§ã®é«ããµã¼ãã«ä»»ããäºãæ¨å¥¨ããã¦ããã
ãã¾ããåææ¥ç¶å¯è½æ°ãå°ãªãã®ã§ãcometã®ããã«ã³ãã¯ã·ã§ã³ãå¼µãã£ã±ãªãã«ãããããªç¨éã«ãã¾ã£ããåããªããUnicornã¯ããã¾ã§ãçæéã§å®è¡ãçµãããªã¯ã¨ã¹ãã大éã«å¦çããããã®ãµã¼ãã§ããã
Rainbows
ãåææ¥ç¶æ°ãå¤ããcometãªã©ã«ã使ãããµã¼ãã¨ãã¦ãUnicornããã¼ã¹ã«è¤æ°ãªã¯ã¨ã¹ãã®åæå¦çæ©è½ãå ããRainbowsãããã並åå¦çã®å®è£ ã¯EventMachine, Rev, NeverBlockãªã©ãããããªãã®ããµãã¼ããã¦ããããã®ã©ãããé¸æããäºãã§ãããåææ¥ç¶æ°ã®å¢å ã®ä»£åã¨ãã¦ãUnicornã®ã·ã³ãã«ãã¯ãã失ããããUnicornã®æ¹ããã©ãã«ãèµ·ãã£ãå ´åã«ãããã°ããããï¼ãããã©ãã«ãèµ·ããã«ããã¯ãã§ããï¼ã®ã§ãåææ¥ç¶æ°ãå¢ããå¿ è¦ããªããã°Unicornã使ã£ãæ¹ãè¯ãã
å®éã«è©¦ãã¦ã¿ã
ãunicorn.confã¨ããååã§è¨å®ãã¡ã¤ã«ãæ¸ãããããã ãã ã¨ããããããªãããRubyã®ã¹ã¯ãªããã«ãªã£ã¦ããã
listen "/tmp/unicorntest.sock" worker_processes 4 pid "/var/lib/pids/unicorn.pid" stderr_path "/var/log/unicorn.log" stdout_path "/var/log/unicorn.log"
ãUnix Domain Socketã使ããã¨ã«ããã®ã§ãæåã«nginxã§ãªãã¼ã¹ãããã·ãç«ã¦ããnginxã®è¨å®ã¯ãããªæãã
upstream unicorntest { server unix:/tmp/unicorntest.sock; } server { listen 80; server_name localhost; (ä¸ç¥) location ~ ^.*$ { proxy_pass http://unicorntest; } }
ãconfig.ruã®ãããã£ã¬ã¯ããªã§
unicorn -c unicorn.conf
ããå®è¡ãããåãããç¹ã«ãããã¨ããã¯ãªãã£ãã
ãåããã®ã§ãç°¡åã«Sinatra用のブログアプリã§ããã©ã¼ãã³ã¹ã®å®é¨ããã¦ã¿ããç°å¢ã¯linodeã®ä¸çªå®ããã¤ã使ã£ããab -n 1000 -c 10 http://localhost/ ã§Requests per secondã352.09 [reqs/sec]ã ã£ãããã·ã³ã¹ããã¯ã¯ä»ã¨ãªã£ã¦ã¯ãããªã«é«ãæ¹ã§ã¯ãªãã¨æãããã®ã§ãããã§å®éã«350 reqs/secãåºããªãããã©ã¼ãã³ã¹çã«ã¯å
åããªã