matsumoto-r/ngx_mruby — pull request #7 by cubicdaiya (2012-12-24 23:18:51+00:00)
Title: performance improvement patch

Hi, this patch makes ngx_mruby run greatly faster.
Previous ngx_mruby parses a mrb file or string with processing each request.
This patch makes each handler has one mrb_state and init mrb_state only at first.

The following is the benchmark report with ab.

# my environment

* OS -> Linux Mint 13(Linux bokko-mint 3.2.0-35-generic #55-Ubuntu SMP Wed Dec 5 17:42:16 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux)
* GCC 4.6.3(Ubuntu/Linaro 4.6.3-1ubuntu5)
* nginx-1.3.8

## nginx configuration

```conf
worker_processes 4;

events {
worker_connections 1024;
}

http {
server {
listen 8000;
server_name localhost;
location /mruby {
mruby_post_read_handler /home/bokko/workspace/nginx/ngx_mruby/example/post_read.mrb;
}
}
}
```

# not applied this patch

```zsh
$ ab -n 10000 -c 2 "http://localhost:8080/mruby" (git)-[improvements/mrb_singleton]
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software: nginx/1.2.6
Server Hostname: localhost
Server Port: 8080

Document Path: /mruby
Document Length: 323 bytes

Concurrency Level: 2
Time taken for tests: 6.166 seconds
Complete requests: 10000
Failed requests: 5972
(Connect: 0, Receive: 0, Length: 5972, Exceptions: 0)
Write errors: 0
Non-2xx responses: 5964
Total transferred: 3977276 bytes
HTML transferred: 2447996 bytes
Requests per second: 1621.86 [#/sec] (mean)
Time per request: 1.233 [ms] (mean)
Time per request: 0.617 [ms] (mean, across all concurrent requests)
Transfer rate: 629.94 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 1 1 4.4 1 312
Waiting: 1 1 4.4 1 312
Total: 1 1 4.4 1 312

Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 1
99% 2
100% 312 (longest request)
$
```

# applied this patch

```zsh
$ ab -n 10000 -c 2 "http://localhost:8080/mruby" (git)-[improvements/mrb_singleton]
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software: nginx/1.2.6
Server Hostname: localhost
Server Port: 8080

Document Path: /mruby
Document Length: 282 bytes

Concurrency Level: 2
Time taken for tests: 1.346 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Non-2xx responses: 10000
Total transferred: 4740000 bytes
HTML transferred: 2820000 bytes
Requests per second: 7428.93 [#/sec] (mean)
Time per request: 0.269 [ms] (mean)
Time per request: 0.135 [ms] (mean, across all concurrent requests)
Transfer rate: 3438.78 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.1 0 4
Waiting: 0 0 0.1 0 4
Total: 0 0 0.1 0 4

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 0
100% 4 (longest request)
```