ãšããã©ããã®ãªããŽãŒã
ãšããã©ããã®ãªããžã§ãŒã¯ãèŠããŠããŸããïŒ ãŸãã«ãWho need itïŒããšããæå笊ãã WHYïŒ ããšãã質åã®åœ¢ã§èããããGO 1ã®æåã®å®å®ããŒãžã§ã³ã®ãªãªãŒã¹ã¯Habréã§è¡ãããŸããã
Habrã«ãšã£ãŠçãã圢åŒã§æžãããäžé£ã®èšäºã§ããã®è³ªåã«çããããšæããŸã-æ¢åã®Webãããžã§ã¯ãã®æ®µéçãªå ±åéçºã®åœ¢ã§-掻çºãªè°è«ãšæ©èœã®è¿œå ã ãããŠããããHigh Performanceãã«ãµã€ã¯ã«ãå°å ¥ããããšãäºéã«æ£åœåããããã«ãããã ã¹ã¿ãŒãã ãã§ãªããhabri et orbiã®éèŠãªèªç¶è² è·ã«èããèœåãæ確ã«ç€ºããããžã§ã¯ããäœæããã¿ã¹ã¯ãèšå®ããŸãã
é£åé ã®ä»£ããã«ïŒGoã§ã®æãåçŽãªåçWebã¢ããªã±ãŒã·ã§ã³ã®å®è£ ã¯ãåæ§ã®Pythonå®è£ ããã5ã20åéãå®è¡ãããŸãã ãŸããNginxãè¿ãéçããŒã¿ã®é床ã«æ¯ã¹ãŠ2åããå£ã£ãŠããŸããã
ãã®ãããžã§ã¯ãã®ãã¬ãŒã ã¯ãŒã¯ã§ã¯ãGoèšèªèªäœã«å ããŠãHTML5ãCSS3ãRedisãMongoDBãªã©ã®ä»ã®ïŒæ¯èŒçæ°ããïŒWebéçºãã¯ãããžãŒã«éæ¥çã«è§ŠããŸãã ãŸããå®å šæ§ã®åéã§ããã€ãã®ããªãã¯ãæœåºããé·æèšæ¶ã®é ã ããããããç¯çŽããããšãè©Šã¿ãŸãã ææ ¢ããŠãå¿èãšã³ãŒããŒãè²·ãã ãããŠãã ãã-ãããããã®æçŽãã®äžã§ããããããã¯çŽ¹ä»ã ãã§ã:)
ãŸããHabræ¿æš©ã®è¿ éãªå¯Ÿå¿ãšGoèšèªå°çšã®ããã®äœæã«æè¬ããŸãã ãæ°ã«å ¥ãã®äž»é¡ã®èªèã¯åžžã«æ¥œããã®ã§ããã®ãããªããã®åºçŸãšèŽè¡ã®é¢å¿ã®é«ãŸãããHabréã§ã®2幎ã¶ãã®ããã©ãããåãäžãããããšãç§ã«ä¿ããŸããã
ããã«ãã²ããçãããèšèãããã€ãã®èã§çžãããèãã¹ãããããé·ãæ»ãã èšèªã§ã®ç¿Œã®ãããã¬ãŒãºã®èšãæããã¹ããããããããæãªããã®ãã¬ãŒã³ããŒã·ã§ã³ã¹ã¿ã€ã«ãäºåã«è¬çœªããŸã æ³çéå»ããåãç¶ãã ç¿æ £ã®ããã«ã
ãã©ã€ããšåèŠ
ãåç¥ã®ããã«ã圌ãã¯å³ã«ã€ããŠè°è«ããŸããã ãããã£ãŠãGoèšèªã®åªããæ§æãã·ã³ãã«ããããã³åªé ãã«ã€ããŠæåã®å£°ãèå°è£ã«æ®ããªããã°ãªããŸããã å°ãªããšãã³ã¡ã³ãã®äžã§ã次ã®éããŠå°ã£ãæŠããèŠãŠã¿ãããšæããŸãã ããã«åºã¥ããŠãç§ã¯åçŽã§ç解å¯èœãªæž¬å®ã®å¯Ÿè±¡ãšãªãäºé ã«ã€ããŠã®ã¿è©±ãããšãææ¡ããŸãã ããããæ°åãæ¯èŒããŠããä»ã®èšèªãšæ¯èŒããå¿ èŠããããŸããä»ã®èšèªããå æé¢ä¿ãšããŠèªèãããŸãã ç§ã¯ãã®ç¹çŽ°ãªç¶æ³ããæãåºãæ¹æ³ãé·ãéèããŠããŸãããããã®æ±ºå®ã¯å®å šã«ãœãã¢ããã¯ã§ããããã«æãããŸãã ä»ã®ãæ°ã«å ¥ãã®èšèªã§ããPythonãšæ¯èŒããŸãã ãã®ãããªã¢ãããŒãã¯ãåèŠã®éé£ããç§ãæãããã«æããŸãã çœé»ãšå€ªåã§æžããŸããããPythonã倧奜ãã§ã ã
èšèªãã®ãã®ããã®å²åŠãããã³èœåã説æããææžããã®å¹³å¡ãªæ®µèœãç¹°ãè¿ããããããŸããã ç§ã¯ããªããæ¢ã«èå³ãæã£ãŠãããšåæã«ãèªåã§ããã¥ã¡ã³ããèªãããšãã§ãããšããäºå®ããé²ãã§ãããŸãã
éå£ã«ãäž»ãïŒ éå£ã«ïŒ
åã®æ®µèœã§ãã³ããããæ¯èŒããå§ããŸãããã 次ã®2ã€ã®ããªããã£ããªåçã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã
- WebãµãŒããŒãèµ·åãã
- ã«ãŒãã£ã³ã°ãšãã³ãã©ãŒããããŸã
- ã³ã³ããã¹ãããå€æ°ãååŸããŸã
- ãã³ãã©ã§ããã眮ãæããŠãããŒãœãã©ã€ãºãããæšæ¶HelloãïŒ sãçºè¡ããŸãã
è€éãªããšã¯äœããããŸããããããã¯éçãªã³ã³ãã³ãã§ã¯ãããŸããããå€å°ã®unningã¯ãããŸãã ã¡ã€ã³ãã¬ãŒãããŸã ãã£ã¹ã¯æäœã®ãŸãŸã§ãããããŒã¿ããŒã¹ãæäœããŠãããšããäºå®ã«ã¯ãå€ãã®ç°è«ãäºæ³ãããŸãã ãããŠãç§ã¯ããã«ã€ããŠè°è«ããŸããã ããããçºè¡šãããèšäºã®å 容ãæãåºããŸãããã i / oããã³dbã®æé©åã«ã€ããŠèª¬æããŸãããããã§ã¯ãçŽå¿ççã®æ°å€ãèŠãŠãããŸãã
1. Webapp2.pyã®ã¢ããªã±ãŒã·ã§ã³ã³ãŒã-貌ãä»ãã«åºã¥ãéåžžã«ã·ã³ãã«ã§é«éãªãããã¬ãŒã ïŒ
webapp2ãã€ã³ããŒããã ã¯ã©ã¹HelloïŒwebapp2.RequestHandlerïŒïŒ def getïŒselfãnameïŒïŒ self.response.writeïŒ 'HelloãïŒ sïŒ'ïŒ nameïŒ app = webapp2.WSGIApplicationïŒ[ ïŒ '/ïŒ\ w +ïŒ'ãããã«ã¡ã¯ïŒã ]ãdebug = FalseïŒ def mainïŒïŒïŒ ããŒã¹ãã€ã³ããŒãhttpserverãã httpserver.serveïŒã¢ããªããã¹ã= '127.0.0.1'ãããŒã= '8080'ïŒ __name__ == '__main__'ã®å ŽåïŒ ã¡ã€ã³ïŒïŒ
èµ·åã³ãŒãïŒpython habr.py
2. Goã³ãŒãïŒ
ããã±ãŒãžã¡ã€ã³ ã€ã³ããŒãïŒ ãfmtã ãnet / httpã ïŒ funcãã³ãã©ãŒïŒw http.ResponseWriterãr * http.RequestïŒ{ fmt.FprintfïŒwã "HelloãïŒ sïŒ"ãr.URL.Path [1ïŒ]ïŒ } func mainïŒïŒ{ http.HandleFuncïŒ "/"ããã³ãã©ãŒïŒ http.ListenAndServeïŒ "ïŒ8080"ããªãïŒ }
ã¹ã¿ãŒãã¢ããã³ãŒãïŒhabr.goãå®è¡ããŸã
è€éãªããšã¯ãããŸãããïŒ å°ãªããšããã®ãããªåçŽãªã¿ã¹ã¯ã«ã€ããŠã¯ãGoã³ãŒããããã«ã·ã³ãã«ã§ç解ãããããã«æ³šæããŠãã ããã äž¡æ¹ã®ãªã¹ãã¯åãã¢ã«ãŽãªãºã ã瀺ããŠããŸãã ããŒãããªãã¹ã³ãããã¹ãŠã®GET /ïŒ*ïŒããã³ãã©ãŒã«éããŸãããã³ãã©ãŒã¯ãïŒ*ïŒãHelloã®åŸã«è¡šç€ºåã«å€æããŸãã ç§ã¯ããã®ãããªã¹ããŒã ã§åãããã¹ãŠã®äººã«ã³ãŒããæ確ã§ãªããã°ãªããªããšä¿¡ããŠããŸãã
3.æ¯èŒã®ããã«ãnginxãè¿œå ããŸããããã«ããããHelloãhabrããšããããã¹ããå«ãéçããŒãžã衚瀺ãããŸãã 泚æïŒ Nginxã¯ãnginx-lightããã±ãŒãžã«å«ãŸãããããã«äœ¿çšå¯èœãªèšå®ã§èµ·åãããŸãã ãªãŒããŒã¯ããã¯çšã®åšåº«ã¯å·šå€§ãªãŸãŸã§ãããããã«ã€ããŠã¯åŸã§èª¬æããŸãã
4.次ã®æ§æã®ãã¹ããã³ãïŒèšèªã¯ãµãŒããŒãšã¯åŒã³ãŸããïŒïŒ
Lenovo B460E / RAMïŒ2G / CPUïŒCeleron Dual-Core T3500 @ 2.10Ghz
OSïŒLubuntu 12.04 x86ãã«ãŒãã«ïŒ3.2.0-20-generic
5. apache2-utilsã®abãŠãŒãã£ãªãã£ã䜿çšããŠãæãåºæ¬çãªæ¹æ³ã§æž¬å®ãè¡ããŸãã å¿ èŠã«å¿ããŠãSiegeãŸãã¯å¥ã®äœ¿ãæ £ããããŒã«ã䜿çšããŠèªåã§ç¢ºèªã§ããŸãã
6.ã¬ãŒã¹çµæ
Py @ ab -c10 -n1000 httpïŒ// localhostïŒ8080 / habrïŒã-nãã®å€ã倧ãããªããšãPythonããŒãžã§ã³ã§ã¯å¯Ÿå¿ã§ããªããªããŸãïŒ
ãã¹ãã«ããã£ãæéïŒ0.987ç§
1ç§ãããã®ãªã¯ãšã¹ãïŒ1012.67 [ïŒ/ç§]ïŒå¹³åïŒ
Go @ ab -c10 -n1000 httpïŒ// localhostïŒ8080 / habr
ãã¹ãã«ããã£ãæéïŒ0.197ç§
1ç§ãããã®ãªã¯ãšã¹ãæ°ïŒ5080.24 [ïŒ/ç§]ïŒå¹³åïŒ
Nginx @ ab -c10 -n1000 httpïŒ// localhost /
ãã¹ãã«ããã£ãæéïŒ0.101ç§
1ç§ãããã®ãªã¯ãšã¹ãïŒ9895.50 [ïŒ/ç§]ïŒå¹³åïŒ
ã¿ã¹ã¯ãè€éã«ããŸãããã
Py @ ab -c100 -n1000 httpïŒ// localhostïŒ8080 / habr
ãã¹ãã«ããã£ãæéïŒ1.045ç§
1ç§ãããã®ãªã¯ãšã¹ãïŒ956.57 [ïŒ/ sec]ïŒå¹³åïŒ
Go @ ab -c100 -n1000ïŒhttpïŒ// localhostïŒ8080 / habr
ãã¹ãã«ããã£ãæéïŒ0.199ç§
1ç§ãããã®ãªã¯ãšã¹ãïŒ5021.14 [ïŒ/ç§]ïŒå¹³åïŒ
Nginx @ ab -c100 -n1000 httpïŒ// localhost /
ãã¹ãã«ããã£ãæéïŒ0.110ç§
1ç§ãããã®ãªã¯ãšã¹ãïŒ9054.77 [ïŒ/ç§]ïŒå¹³åïŒ
ã-cã> 1000ã®å ŽåãPythonããŒãžã§ã³ã¯ãã¹ãã«åæ ŒãããNginxã¯ãã¥ãŠãªã䜿ãç¶ããŸãããGoã¯ãäºæããã1ç§ããã50ãã4000ã®ãªã¯ãšã¹ãã瀺ããå®å šã«äºæž¬äžå¯èœãªçµæãââãããããŸãã ä»æ¥ãUbuntu 12.04ãšããŒãžã§ã³Go 1ã§æåã«ééããããããã®çŸè±¡ã¯èª¬æã§ããŸããã以åã®ããŒãžã§ã³ïŒr60.3ïŒã§ã¯ãGoã¯å®å®ããŠããŸããã ãŸãã¯ãã©ãããããã§ããããã±ãŒãžãã³ãã«ã¯æããã«HighLoadãµãŒããŒçšã§ã¯ãããŸãã:)
誰ãããã¹ãæ¹æ³è«ãããã©ã«ãèšå®ããŸãã¯ä»ã®èšèªã§ã®å®è£ ã®æ¬ åŠã«æºè¶³ããªãã®ã§ã¯ãªãããšæããŸãã ããªãã®ææ¡ãèããŠããã¹ããç¹°ãè¿ããŸãã Goã®ååšïŒéçºïŒã®2幎éã§ãããŸããŸãªãã¹ããå®è¡ããããŸããŸãªèšå®ã§å®è¡ããŸããã ãããŠããã¹ãŠã®ãã¹ãã§ãGoãç§ã®äœæ¥ç¯å²ã«æé©ãªããŒã«ã§ãããšç¢ºä¿¡ããŸããã ãããããã¹ãŠå®æœããããããããšã¯æ©ç¥ãããªè·æ¥ã§ãããªããªãã圌ãã¯åã³äžæºãæããããã§ãã
èšèãžã®ä¿¡ä»°ãæ±ããŠãç§ã¯èšæ¶ããã®çµæããç°¡åãªæç²ãããŸãã
Javaã¯10ã30ïŒ éããªãããšããããŸãããã¡ã¢ãªæ¶è²»ãèãã倧ããããã§ãïŒVMèªäœãšããã»ã¹ã®äž¡æ¹ã«ããïŒã ç¹°ãè¿ããŸãããäžèšã®äŸïŒãã¬ãŒã ã¯ãŒã¯ã䜿çšãããæšæºã©ã€ãã©ãªã®ã¿ïŒã¯ãæå°éã®å®è£ ã³ãŒããæ確ã«ç€ºããŠããŸãã ãããŠãã³ãŒããå°ãªãã»ã©ããšã©ãŒãå°ãªããªããŸãã
Net / Mono CïŒå®è£ ã«ã€ããŠãåãããšãèšããŸãã
C / C ++ã䜿çšãããšãæå°éã®ãªãœãŒã¹èŠä»¶ã§çŽ æŽãããé«éãªWebã¢ããªã±ãŒã·ã§ã³ãäœæã§ããŸãã ããããéçºé床ã¯å€§ããäœäžããŸãã
PythonãšRubyã®å®è£ ã¯é床ãäœäžããã»ãŒåãè² è·ã«ãªããŸãããPHPã¯å£æ» çãªææãåããŸãã
次ã®éšåã®çºè¡šïŒ
- ãµã³ãã«HelloãHabrïŒããã®ã³ãŒãã®è§£æ
- è€æ°ã®ãã³ãã©ãŒã«ããè€é
- Nginxã§ããã€ãã®GoãµãŒããŒãã³ã³ãã€ã«ããŠè©°ã蟌ã¿ãŸãã
ãæž èŽããããšãããããŸããã å°ãªããšã誰ãã玹ä»ããã¹ã¿ãŒããèªåã«ãšã£ãŠèå³æ·±ããã®ãèŠã€ããããšãé¡ã£ãŠããŸãã
ã§ããã°ææçãªè°è«ã楜ãã¿ã«ããŠããŸãã