Skip to content

Instantly share code, notes, and snippets.

@matsumotory
Last active June 5, 2018 12:57
Show Gist options
  • Save matsumotory/dd7186668c1902ef334c3bbeb46fb405 to your computer and use it in GitHub Desktop.
Save matsumotory/dd7186668c1902ef334c3bbeb46fb405 to your computer and use it in GitHub Desktop.

10msec程度のレスポンスタイムのURLを作る

worker_processes  1;
events {
    worker_connections  1024;
}

daemon off;
master_process off;
error_log logs/error.log warn;

http {
    include       mime.types;

    server {
        listen       48080;
        server_name  localhost;
        root /home/ubuntu/DEV/ngx_mruby/build/nginx/html/;

        # test for hello world and cache option
        location /mruby {
            mruby_rewrite_handler_code 'Nginx::Async.sleep 10; Nginx.echo "done"';
        }
    }
}

ngx_mrubyのblockingとnon-blocking http clientを比較する

worker_processes  1;
events {
    worker_connections  1024;
}

daemon off;
master_process off;
error_log logs/error.log warn;

http {
    include       mime.types;

    server {
        listen       58080;
        server_name  localhost;
        root /home/ubuntu/DEV/ngx_mruby/build/nginx/html/;

        location /enable_return {
            mruby_content_handler_code '
              Nginx.rputs"hoge"
              return if true
              Nginx.rputs "foo"
            ';
        }

        location /sub_req_proxy_pass {
            proxy_pass http://127.0.0.1:48080/mruby;
            mruby_output_body_filter_code '
              a = 1
            ';
        }

        location /async_http_sub_request_with_proxy_pass {
            mruby_rewrite_handler_code '
              Nginx::Async::HTTP.sub_request "/sub_req_proxy_pass"
              res = Nginx::Async::HTTP.last_response
              Nginx.rputs res.body
            ';
        }

        location /http_sub_request_with_proxy_pass {
            mruby_rewrite_handler_code '
              Nginx.rputs HttpRequest.new.get("http://127.0.0.1:48080/mruby")["body"]
            ';
        }
    }
}

ベンチマーク

blocking

$ ab -k -c 100 -n 1000 http://127.0.0.1:58080/http_sub_request_with_proxy_pass
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.13.11
Server Hostname:        127.0.0.1
Server Port:            58080

Document Path:          /http_sub_request_with_proxy_pass
Document Length:        5 bytes

Concurrency Level:      100
Time taken for tests:   12.284 seconds
Complete requests:      1000
Failed requests:        0
Keep-Alive requests:    1000
Total transferred:      127000 bytes
HTML transferred:       5000 bytes
Requests per second:    81.41 [#/sec] (mean)
Time per request:       1228.355 [ms] (mean)
Time per request:       12.284 [ms] (mean, across all concurrent requests)
Transfer rate:          10.10 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.3      0       6
Processing:    13 1168 216.5   1205    1297
Waiting:       13 1168 216.5   1205    1297
Total:         19 1169 215.3   1205    1297

non-blocking

$ ab -k -c 100 -n 1000 http://127.0.0.1:58080/async_http_sub_request_with_proxy_pass
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.13.11
Server Hostname:        127.0.0.1
Server Port:            58080

Document Path:          /async_http_sub_request_with_proxy_pass
Document Length:        5 bytes

Concurrency Level:      100
Time taken for tests:   0.254 seconds
Complete requests:      1000
Failed requests:        0
Keep-Alive requests:    1000
Total transferred:      127000 bytes
HTML transferred:       5000 bytes
Requests per second:    3941.80 [#/sec] (mean)
Time per request:       25.369 [ms] (mean)
Time per request:       0.254 [ms] (mean, across all concurrent requests)
Transfer rate:          488.88 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.9      0       5
Processing:    15   24   4.6     24      33
Waiting:       15   24   4.6     24      33
Total:         15   24   4.7     24      33

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     27
  75%     28
  80%     29
  90%     31
  95%     33
  98%     33
  99%     33
 100%     33 (longest request)

81req/secが3941req/secと50倍はやくなりました

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment