世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页Ruby
随风 · 练气

Ruby 性能测试

随风发布于2874 次阅读

1. 介绍

当开发到一定程序,积累了越来越多的代码,整个网站的运行速度难免会遇到一定的瓶颈。或者说有时候有两个方法实现一个功能时,却不知哪个方法是运行起来比较快的,这个时候就需要进行性能测试了,主要是运行速度的测试。

2. 使用

ruby 的标准库就有提供来实现这个性能测试的库,文档地址可见于: http://ruby-doc.org/stdlib-2.2.2/libdoc/benchmark/rdoc/Benchmark.html

要使用起来也是很简单的,只要对照文档给出的例子,依样画葫芦。

require 'benchmark'

n = 5000000
Benchmark.bm(7) do |x|
  x.report("for:")   { for i in 1..n; a = "1"; end }
  x.report("times:") { n.times do   ; a = "1"; end }
  x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
end

输出如下:

              user     system      total        real
for:      0.470000   0.010000   0.480000 (  0.500931)
times:    0.470000   0.010000   0.480000 (  0.564162)
upto:     0.480000   0.010000   0.490000 (  0.571432

还能有统计信息的输出。

require 'benchmark'
include Benchmark         # we need the CAPTION and FORMAT constants

n = 5000000
Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
  tf = x.report("for:")   { for i in 1..n; a = "1"; end }
  tt = x.report("times:") { n.times do   ; a = "1"; end }
  tu = x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  [tf+tt+tu, (tf+tt+tu)/3]
end

输出如下:

              user     system      total        real
for:      0.450000   0.000000   0.450000 (  0.478545)
times:    0.400000   0.000000   0.400000 (  0.437174)
upto:     0.420000   0.010000   0.430000 (  0.450468)
>total:   1.270000   0.010000   1.280000 (  1.366186)
>avg:     0.423333   0.003333   0.426667 (  0.455395)

3. 插件介绍

标准库提供的性能测试功能已经蛮好的,这里会介绍几个关于性能测试功能的插件。

3.1 benchmark-ips

标准库中测试一些功能,运行多少次是要自己写的,而benchmark-ips这个插件就提供了这个功能。


require 'benchmark/ips'

ARRAY = [*1..100]

def fast
  ARRAY[0]
end

def slow
  ARRAY.first
end

Benchmark.ips do |x|
  x.report('Array#[0]') { fast }
  x.report('Array#first') { slow }
  x.compare!
end

输出如下:

Calculating -------------------------------------
           Array#[0]   168.907k i/100ms
         Array#first   173.803k i/100ms
-------------------------------------------------
           Array#[0]      9.264M (± 7.4%) i/s -     45.943M
         Array#first      8.405M (± 9.2%) i/s -     41.365M

Comparison:
           Array#[0]:  9264184.5 i/s
         Array#first:  8404746.2 i/s - 1.10x slower
3.2 derailed_benchmarks

derailed_benchmarks是对 ruby 应用,例如 rails,起作用的。

测试 gem 启动所占用的内存。

$ bundle exec derailed bundle:mem
TOP: 83.728 MiB
  factory_girl_rails: 0.9258 MiB
    factory_girl_rails/railtie: 0.9258 MiB
      factory_girl: 0.8516 MiB
  bootstrap_form: 0.875 MiB
  mysql2: 0.7603 MiB
    mysql2/mysql2: 0.6665 MiB
  unicorn: 0.5527 MiB
  weixin_rails_middleware: 0.5508 MiB
  slim-rails: 0.418 MiB
    slim: 0.4063 MiB
  jbuilder: 0.3984 MiB (Also required by: lina)
  faker: 0.3242 MiB
  bootstrap-datepicker-rails: 0.3047 MiB
  puma: 0.3027 MiB

还能测出是否有内存泄露。

$ bundle exec derailed exec perf:mem_over_time
Booting: production
rails365_pro already exists
Endpoint: "/"
/home/yinsigan/.rvm/gems/ruby-2.2.2/gems/derailed_benchmarks-1.1.3/lib/derailed_benchmarks/tasks.rb:72: warning: already initialized constant DERAILED_APP
/home/yinsigan/.rvm/gems/ruby-2.2.2/gems/derailed_benchmarks-1.1.3/lib/derailed_benchmarks/tasks.rb:23: warning: previous definition of DERAILED_APP was here
PID: 11112
123.71728515625
138.86767578125
138.90673828125
3.3 memory_profiler

memory_profiler是一个关于内存使用的测试工具。

require 'memory_profiler'

report = MemoryProfiler.report do
  require 'mina'
end

report.pretty_print

3.4 fast-ruby

fast-ruby是一个关于 ruby 各种方法比较的资源。

完结。

本站文章均为原创内容,如需转载请注明出处,谢谢。

0 条回复
暂无回复~~
喜欢
统计信息
    学员: 29063
    视频数量: 1973
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top