ãã®è¨äºã¯ãmruby advent calendar 2015ã®16æ¥ç®ã®è¨äºã§ãã
ç»åãstaticã³ã³ãã³ãé ä¿¡ç³»ã¯HTTP/2ãæå©ãªç¶æ³ãå¹¾ã¤ãããã®ã§ãã¤ãã§ã«HTTP/2ãåãç»åå¤æãµã¼ãã®ãããã¿ã¤ããmrubyã§ä½ã£ã¦ã¿ã¾ããããã¼ã¹ã¯ãã¡ããtrusterdã§ãããªãã¦ãã£ãã£ã¦mrubyã®HTTP/2ãµã¼ãã§ããããï¼ï¼
æè¿ã¾ãéçºãåéãã¦ããã¾ãã¦ãæã¯h2oãnghttp2ã®ãã³ããã¼ã¯ã«ä¸ç·ã«æ¯è¼å¯¾è±¡ã¨ãã¦å ¥ãã¦ããã£ãããã¦ããã®ã§ããããã°ããé¢ãã¦ãããã¡ã«çããå ã¸å ã¸ã¨è¡ã£ã¦ãã¾ãããã®ã§ãã¾ã追ãã¤ããããã«ã»ãã»ã¨åå¼·ããªããå®è£ ãã ãã¦ããã¾ãã
ãã®ä»ãtrusterdã«ã¤ãã¦ã¯ãã®è¾ºã¨ãã
ãã®è¾ºãè¦ã¦ããã ãã¨è¯ããã¨æãã¾ãã
trusterdã®ãã«ã
ã¾ãã¯æ©é以ä¸ã®ããã«ãã«ããã¦ããã¾ãããã
build_config.rb
cloneããtrusterd/
ã«ããbuild_config.rb
ã«ãä»åã¯ç»åå¤æããããã®mgemããªã³ã¯ããã¹ãä¸è¨ã®mgemã追è¨ãã¾ãã
conf.gem :github => 'kjunichi/mruby-mrmagick'
- build
å¾ã¯ãã«ãããã ãã§ããç°å¢ã¯ubuntu14_04ã®ã³ã¢4ã¤ã¡ã¢ãª8GBã®VMWareã使ãã¾ããã
make make install
build/conf/trusterd.conf
installããã¨ãããã©ã«ãã§ã¯ã«ã¬ã³ããã£ã¬ã¯ããªã®build/
以ä¸ã«å¿
è¦ãªãã¡ã¤ã«ãã¤ã³ã¹ãã¼ã«ãããã®ã§ããã®ä¸ã«ããtrusterdã®è¨å®ãã¡ã¤ã«ã以ä¸ã®ããã«ãã¾ããã
ãã®è¨å®ã«ãã£ã¦ããã£ã¨ããéã«HTTP/2 + TLS + ç»åå¤æã®åãããã¾ããç°¡åã§ããã
ã¢ã¯ã»ã¹å¯¾è±¡ã®PNGãã¡ã¤ã«ã«å¯¾ãã¦ãimage.png?type=half
ãªã©ã¨ã¯ã¨ãªãä¸ãã¦ã¢ã¯ã»ã¹ããã¨ãç»åãååã«ãã¦ã¬ã¹ãã³ã¹ãããããªåãããã¾ããä»åã¯ãããã¿ã¤ãã¨ãããã¨ã§ãç°¡åã«ããã®ã¿ã®åããããã ãã«ã¨ã©ãã¾ããã
SERVER_NAME = "Trusterd" SERVER_VERSION = "0.0.1" SERVER_DESCRIPTION = "#{SERVER_NAME}/#{SERVER_VERSION}" root_dir = "/home/matsumotory/DEV/trusterd/build" s = HTTP2::Server.new({ :port => 8080, :document_root => "#{root_dir}/htdocs", :server_name => SERVER_DESCRIPTION, :run_user => "matsumotory", :worker => "auto", :tls => true, :key => "#{root_dir}/ssl/server.key", :crt => "#{root_dir}/ssl/server.crt", :callback => true, }) s.set_map_to_storage_cb do r = s.r if File.extname(r.filename) == ".png" unless r.args.empty? # ã¯ã¨ãªãã©ã¡ã¼ã¿ã¯ä¸ã¤ã§?type=halfã§ãããã¨åæã§ã¨ããããå®è£ type = r.args[1..-1].split("=")[1] if type == "half" new_file = r.filename + "_" + type + ".png" unless File.exist? new_file img = Mrmagick::ImageList.new(r.filename) new_img = img.scale(0.5) new_img.write(new_file) end r.filename = r.filename + "_" + type + ".png" end end end end s.run
ãã©ã¦ã¶ããHTTP/2ã¢ã¯ã»ã¹ã§ããããã«TLSé¢é£ãã¡ã¤ã«ã以ä¸ã®ããã«è©¦ãã«ä½ã£ã¦ããã¾ãã
cd build/ssl sudo sh -c 'yes "" | openssl req -new -days 365 -x509 -nodes -keyout server.key -out server.crt'
ãã©ã¦ã¶ã¢ã¯ã»ã¹
ã§ã¯æ©éã¡ããã¨å¤æãããã確èªãã¦ã¿ã¾ãããã
é常ã®ãµã¤ãºã®ã¢ã¯ã»ã¹ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ããã«ãç»åå¤æãã¹ãã¯ã¨ãªã¼ãã©ã¡ã¼ã¿ãä¸ãã¦ãä»åã¯ååã®ãµã¤ãºã«ãªãããã«?type=half
ã¨ãªãããã«ãã¦ã¢ã¯ã»ã¹ãã¨ã以ä¸ã®ããã«å¤æãããçµæã帰ã£ã¦ãã¾ããã
ã§ãã¾ããã
ãã³ããã¼ã¯
ãã£ãããªã®ã§ãã¤ãã§ã«ãã³ããã¼ã¯ãã¨ã£ã¦ã¿ã¾ããããHTTP/2 + TLSãªã®ã§h2loadã使ãã¾ããåºæ¬ãã©ã¡ã¼ã¿ã¯ä»¥ä¸ã¨ãã¾ãã
h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/
- å¤æåã®ç»åã¸ã®ãã³ããã¼ã¯
$ h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/logo.png starting benchmark... spawning thread #0: 25 total client(s). 250000 total requests spawning thread #1: 25 total client(s). 250000 total requests spawning thread #2: 25 total client(s). 250000 total requests spawning thread #3: 25 total client(s). 250000 total requests TLS Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Application protocol: h2 progress: 10% done progress: 20% done progress: 30% done progress: 40% done progress: 50% done progress: 60% done progress: 70% done progress: 80% done progress: 90% done progress: 100% done finished in 12.45s, 80349.06 req/s, 655.39MB/s requests: 1000000 total, 1000000 started, 1000000 done, 1000000 succeeded, 0 failed, 0 errored, 0 timeout status codes: 1000000 2xx, 0 3xx, 0 4xx, 0 5xx traffic: 8553033172 bytes total, 10030172 bytes headers (space savings 91.85%), 8525000000 bytes data min max mean sd +/- sd time for request: 3.45ms 330.42ms 111.99ms 46.70ms 62.28% time for connect: 14.57ms 147.67ms 76.94ms 22.21ms 75.00% time to 1st byte: 25.56ms 202.79ms 135.98ms 41.96ms 76.00% req/s (client) : 803.61 1766.93 935.77 291.06 89.00%
- å¤æãããç»åã¸ã®ãã³ããã¼ã¯
h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/logo.png?type=half starting benchmark... spawning thread #0: 25 total client(s). 250000 total requests spawning thread #1: 25 total client(s). 250000 total requests spawning thread #2: 25 total client(s). 250000 total requests spawning thread #3: 25 total client(s). 250000 total requests TLS Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Application protocol: h2 progress: 10% done progress: 20% done progress: 30% done progress: 40% done progress: 50% done progress: 60% done progress: 70% done progress: 80% done progress: 90% done progress: 100% done finished in 14.15s, 70663.46 req/s, 385.74MB/s requests: 1000000 total, 1000000 started, 1000000 done, 1000000 succeeded, 0 failed, 0 errored, 0 timeout status codes: 1000000 2xx, 0 3xx, 0 4xx, 0 5xx traffic: 5724037565 bytes total, 10034565 bytes headers (space savings 91.84%), 5696000000 bytes data min max mean sd +/- sd time for request: 8.55ms 332.16ms 129.32ms 42.88ms 65.24% time for connect: 38.10ms 214.01ms 79.91ms 33.53ms 94.00% time to 1st byte: 59.78ms 270.10ms 143.91ms 45.15ms 73.00% req/s (client) : 706.85 1201.34 793.84 171.11 85.00%
VMã®ç°å¢ã§ãããããã§ãã°ããããè¯ãããã§ããã
logo.png | logo.png?type=half | |
---|---|---|
req/s | 80349.06 | 70663.46 |
åèï¼index.htmlã®ãããªå°ããªãã¡ã¤ã«ã®ãã³ããã¼ã¯
trusterd + TLS + HTTP/2ã¯ãå°ããªãã¡ã¤ã«ã®å ´åã¯ã©ããããæ§è½ãã§ãã®ããã¤ãã§ã«ç¢ºèªãã¦ããã¾ãã
$ h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/index.html starting benchmark... spawning thread #0: 25 total client(s). 250000 total requests spawning thread #1: 25 total client(s). 250000 total requests spawning thread #2: 25 total client(s). 250000 total requests spawning thread #3: 25 total client(s). 250000 total requests TLS Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Application protocol: h2 progress: 10% done progress: 20% done progress: 30% done progress: 40% done progress: 50% done progress: 60% done progress: 70% done progress: 80% done progress: 90% done progress: 100% done finished in 3.40s, 294449.34 req/s, 13.76MB/s requests: 1000000 total, 1000000 started, 1000000 done, 1000000 succeeded, 0 failed, 0 errored, 0 timeout status codes: 1000000 2xx, 0 3xx, 0 4xx, 0 5xx traffic: 49014749 bytes total, 9011749 bytes headers (space savings 92.55%), 22000000 bytes data min max mean sd +/- sd time for request: 487us 125.01ms 28.70ms 14.40ms 70.64% time for connect: 34.69ms 109.32ms 77.21ms 19.47ms 55.00% time to 1st byte: 57.57ms 158.23ms 103.08ms 28.21ms 60.00% req/s (client) : 2947.22 5091.48 3476.10 646.64 84.00%
å°ãããã¡ã¤ã«ã¯ãTLSãã¤fdã®ãã£ãã·ã¥ç¡ã㧠294449.34 req/s
ãããã§ã¾ãããããã¾ãã¾ãã¯ããã
ã¤ãã§ã«nginxã®TLS+HTTP/2ããã³ããã¼ã¯
ã¾ããä¸è¨ã®ãã³ããã¼ã¯çµæã ãã§ã¯åºæºã¨ãªãå¤ãã©ã®ç¨åº¦ããããã«ããã®ã§ãä»åã¯ããã«nginxãHTTP/2ã«å¯¾å¿ãã¦ããäºãè¸ã¾ãã¦ãv1.9.6ã§ä»¥ä¸ã®ãããªè¨å®ãããä¸ã§ãåèç¨åº¦ã«index.htmlã¨logo.pngã¸ã®ãã³ããã¼ã¯ãè¨æ¸¬ãã¾ããã
è¨å®ã¯ä»¥ä¸ã®ããã«ãã¦ããããã®è¨å®ã®å ´åã¯ãã®å¤ã ã¨ããåèæ å ±ã¨ãã¦ãèãä¸ããããã£ã¨ãã¥ã¼ãã³ã°ãããè¯ãå¤ãåºãã¨ã¯æãã¾ãã®ã§åã ã§ã試ãä¸ããã
- nginx.conf
worker_processes auto; events { worker_connections 10240; accept_mutex_delay 100ms; } daemon off; http { include mime.types; access_log off; server { listen 8080 ssl http2; ssl_ciphers AESGCM:HIGH:!aNULL:!MD5; server_name localhost; root /usr/local/trusterd/htdocs; ssl_certificate /usr/local/trusterd/ssl/server.crt; ssl_certificate_key /usr/local/trusterd/ssl/server.key; ssl_buffer_size 4k; } }
- nginx + TLS + HTTP/2ã§index.htmlã«åä¸ãã³ããã¼ã¯ãã©ã¡ã¼ã¿ã§ã¢ã¯ã»ã¹
$ h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/index.html starting benchmark... spawning thread #0: 25 total client(s). 250000 total requests spawning thread #1: 25 total client(s). 250000 total requests spawning thread #2: 25 total client(s). 250000 total requests spawning thread #3: 25 total client(s). 250000 total requests TLS Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Application protocol: h2 progress: 10% done progress: 20% done progress: 30% done progress: 40% done progress: 50% done progress: 60% done progress: 70% done progress: 80% done progress: 90% done progress: 100% done finished in 10.24s, 97662.68 req/s, 16.02MB/s requests: 1000000 total, 1000000 started, 1000000 done, 1000000 succeeded, 0 failed, 0 errored, 0 timeout status codes: 1000000 2xx, 0 3xx, 0 4xx, 0 5xx traffic: 172004900 bytes total, 132000000 bytes headers (space savings 24.14%), 22000000 bytes data min max mean sd +/- sd time for request: 142us 297.97ms 79.58ms 44.96ms 71.00% time for connect: 35.99ms 770.81ms 170.07ms 166.40ms 85.00% time to 1st byte: 38.84ms 938.26ms 209.14ms 188.24ms 85.00% req/s (client) : 977.32 1552.77 1273.85 233.02 43.00%
- nginx + TLS + HTTP/2ã§logo.pngã¸ã®ã¢ã¯ã»ã¹
$ h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/logo.png starting benchmark... spawning thread #0: 25 total client(s). 250000 total requests spawning thread #1: 25 total client(s). 250000 total requests spawning thread #2: 25 total client(s). 250000 total requests spawning thread #3: 25 total client(s). 250000 total requests TLS Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Application protocol: h2 progress: 10% done progress: 20% done progress: 30% done progress: 40% done progress: 50% done progress: 60% done progress: 70% done progress: 80% done progress: 90% done progress: 100% done finished in 15.92s, 62829.21 req/s, 520.57MB/s requests: 1000000 total, 1000000 started, 1000000 done, 1000000 succeeded, 0 failed, 0 errored, 0 timeout status codes: 1000000 2xx, 0 3xx, 0 4xx, 0 5xx traffic: 8688004900 bytes total, 136000000 bytes headers (space savings 23.60%), 8525000000 bytes data min max mean sd +/- sd time for request: 206us 285.12ms 137.15ms 48.74ms 70.04% time for connect: 19.46ms 923.68ms 227.51ms 201.02ms 84.00% time to 1st byte: 30.66ms 1.16s 286.52ms 229.84ms 77.00% req/s (client) : 628.66 869.51 721.25 71.74 50.00%
ã¨ãããããªå¤ã«ãªãã¾ãããã¾ã¨ãã§è¡¨ã«ãã¾ãã
ã¾ã¨ã
ã¨ãããã¨ã§ãç»åå¤æãTLS + HTTP/2ãªãµã¼ãã¨ãã¦å®è£ ããä¸ã§ãããã«å¯¾ãããã³ããã¼ã¯ã«ãããç»åå¤æãµã¼ãã¨ãã¦ããããªãã«ã¯ããã¨ãããã¨ããããã¾ããã
ä»åç¨ããã³ã³ãã³ãã®ãã¡ã¤ã«ãµã¤ãºã¯ä»¥ä¸ã§ãã
-rw-rw-r-- 1 matsumotory matsumotory 22 Dec 13 19:18 index.html -rw-rw-r-- 1 matsumotory matsumotory 8.4K Dec 13 19:40 logo.png
ãã³ããã¼ã¯ãã©ã¡ã¼ã¿ã¯ä»¥ä¸ã®ã¨ããã§ãã
h2load -t 4 -c 100 -m 100 -n 1000000 https://127.0.0.1:8080/
HTTP/2 + TLSã®ç»åå¤æã®ãã³ããã¼ã¯ã¨åèã®ããã«nginxã§ãè¨æ¸¬ããçµæã®ã¾ã¨ãã¯ã以ä¸ï¼req/secï¼ã¨ãªãã¾ããã
index.html(22byte) | logo.png(8.4kb) | logo.png?type=half(5.6kb) | |
---|---|---|---|
trusterd | 294449.34 | 80349.06 | 70663.46 |
nginx | 97662.68 | 62829.21 | - |
ã¨ãããã¨ã§ãtrusterdã使ãã°ç°¡åã«mrubyã§ç»åå¤æãµã¼ãã¿ãããªãã®ããµã¯ã£ã¨ä½ãã¦ããããéããã¨ãã話ã§ãããããªãéã¹ããµã¼ããªã®ã§ãæ¯éRubyã§è¨å®ãæ¸ãã¦è²ã ã¨ã試ãä¸ããã
ä»å¾ãfd cacheã®æ©è½ãå®è£ ããã¨ããã³ããã¼ã¯çã«ã¯ããã®2åã®æ§è½ãåºããã¨ã¯trusterdã®fd cacheã®ãããã¿ã¤ãå®è£ ã§ç¢ºèªãã¦ããã®ã§ãããããå®è£ ãããã¨æã£ã¦ããã¾ãã