heroku/rack-queue-metrics — pull request #1 by ryandotsmith (2013-03-11 03:50:39+00:00)
Title: performance considerations

I had heard reports from users of rack-queue-metrics that under high load, this gem causes the web server to grind to a halt. I began to better undersand what this gem was doing and came across some big performance improvements. In summary, making lots of syscalls for every web request is going to cost a lot of latency. Given that customers using this gem are latency sensitive I am proposing that we rethink our approach.

* Remove individual queue time
* Report queue depth every second.

Here is my performance review for a trivial rack app:

## System Information

```bash
$ lscpu
Architecture: x86_64
CPU op-mode(s): 64-bit
CPU(s): 8
Thread(s) per core: 1
Core(s) per socket: 1
CPU socket(s): 8
Vendor ID: GenuineIntel
CPU family: 6
Model: 26
Stepping: 5
CPU MHz: 2133.408
Hypervisor vendor: Xen
Virtualization type: para
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 4096K

$ free
total used free shared buffers cached
Mem: 7362688 732928 6629760 0 49340 322892
-/+ buffers/cache: 360696 7001992
Swap: 0 0 0

$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]

$ unicorn -v
unicorn v4.6.2

$ rackup -v
Rack 1.2 (Release: 1.5)
```

## Without rack-queue-metrics

```bash
$ cat config.ru
run Proc.new {|env|
[200, {'Content-Type' => 'text/plain'}, ["ok"]]
}

$ cat unicorn.rb
worker_processes Integer(ENV["UNICORN_WORKERS"] || 3)
timeout 15
preload_app true
```

```bash
$ bundle exec unicorn -p $PORT -c unicorn.rb &
$ ab -c 10 -n 10000 http://localhost:8000/

...

50% 4
66% 4
75% 4
80% 5
90% 5
95% 7
98% 10
99% 11
100% 18 (longest request)
```

## With rack-queue-metrics

```bash
$ cat config.ru
require 'raindrops'
require 'rack-queue-metrics'
use Raindrops::Middleware
use Rack::QueueMetrics::Middleware
run Proc.new {|env|
[200, {'Content-Type' => 'text/plain'}, ["ok"]]
}

$ cat unicorn.rb
worker_processes Integer(ENV["UNICORN_WORKERS"] || 3)
timeout 15
preload_app true
```

```bash
$ bundle exec unicorn -p $PORT -c unicorn.rb &
$ ab -c 10 -n 10000 http://localhost:8000/

...

50% 26
66% 27
75% 28
80% 29
90% 31
95% 33
98% 35
99% 37
100% 90 (longest request)
```

## With included performance improvements

```bash
$ bundle exec unicorn -p $PORT -c unicorn.rb &
$ ab -c 10 -n 10000 http://localhost:8000/

...

50% 4
66% 4
75% 5
80% 5
90% 5
95% 7
98% 11
99% 12
100% 14 (longest request)
```