性能对比 cruby 2.1 vs jruby 1.7.9 vs rubinius 2.2.3

1603 查看

圣诞节的时候发布了Ruby 2.1,使用RGenGC,性能提升了5%到15%. 那么,新版的Ruby实现和JRuby、Rubinius相比又如何呢?

Posăceanu Marian做了一个性能测试,对比了cruby、jruby和rubinius.

Marian使用的是ApacheBench,2.3. 性能测试有更好的工具,例如siege。但是ApacheBench的优势是足够简单。

使用如下命令启动ApacheBench:

ab -n400 -c16 -T'application/json' http://localhost:3000/entries

cruby

# config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true

before_fork do |server, worker|
  # et cetera
end

命令:

unicorn_rails -c config/unicorn.rb -p 3000

结果:

Concurrency Level:      16
Time taken for tests:   6.769 seconds
Complete requests:      400
Failed requests:        0
Write errors:           0
Total transferred:      3611600 bytes
HTML transferred:       3346800 bytes
Requests per second:    59.09 [#/sec] (mean)
Time per request:       270.766 [ms] (mean)
Time per request:       16.923 [ms] (mean, across all concurrent requests)
Transfer rate:          521.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.0      0       5
Processing:    49  267  36.8    270     331
Waiting:       44  266  36.8    269     330
Total:         49  267  36.3    270     331

Percentage of the requests served within a certain time (ms)
  50%    270
  66%    283
  75%    288
  80%    293
  90%    308
  95%    315
  98%    324
  99%    327
 100%    331 (longest request)

Rubinius 2.2.3

# config/puma.rb
threads 8,32
workers 1

preload_app!

on_worker_boot do
  # et cetera
end

命令

puma -C config/puma.rb -b tcp://localhost:3000

结果

Concurrency Level:      16
Time taken for tests:   9.383 seconds
Complete requests:      400
Failed requests:        0
Write errors:           0
Total transferred:      3590400 bytes
HTML transferred:       3346800 bytes
Requests per second:    42.63 [#/sec] (mean)
Time per request:       375.311 [ms] (mean)
Time per request:       23.457 [ms] (mean, across all concurrent requests)
Transfer rate:          373.69 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    84  371 105.4    348     731
Waiting:       83  363 104.1    338     728
Total:         84  371 105.3    348     732

Percentage of the requests served within a certain time (ms)
  50%    348
  66%    390
  75%    431
  80%    458
  90%    526
  95%    571
  98%    640
  99%    683
 100%    732 (longest request)

jruby

# config/puma.rb
threads 8,32

preload_app!

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

命令:

puma -C config/puma.rb -b tcp://localhost:3000

注意需要替换Gem:

# gem 'pg'
gem 'activerecord-jdbcpostgresql-adapter'

结果:

Concurrency Level:      16
Time taken for tests:   4.019 seconds
Complete requests:      400
Failed requests:        0
Write errors:           0
Total transferred:      3590400 bytes
HTML transferred:       3346800 bytes
Requests per second:    99.53 [#/sec] (mean)
Time per request:       160.760 [ms] (mean)
Time per request:       10.048 [ms] (mean, across all concurrent requests)
Transfer rate:          872.42 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    35  158  31.5    157     389
Waiting:       34  151  27.1    149     261
Total:         36  158  31.5    157     389

Percentage of the requests served within a certain time (ms)
  50%    157
  66%    166
  75%    173
  80%    177
  90%    189
  95%    204
  98%    232
  99%    260
 100%    389 (longest request)

结论

每个机器上的情况可能有所差异,在你自己的机器上运行上面的测试代码,结果会比较准确。

在多核机器上,jruby和rubinius的性能会有明显提升。

jruby       99.53 #################################
cruby       59.09 ###################
rubinius    42.63 ##############

可以看出,在性能上,JRuby具有明显优势。不过,使用JRuby的话,一些C库需要替换。好在现在大多数gem都支持JRuby了。

更新 感谢ShiningRay指出,jruby开启--serverinvokedynamic性能会更好


原文 Is Rubinius 2.2.3 production ready ?
编译 SegmentFault