【问题标题】:Ruby Benchmarking a line that runs multiple times [closed]Ruby基准测试一条多次运行的线[关闭]
【发布时间】:2018-04-22 20:06:42
【问题描述】:

假设我有一个超时的脚本,我想看看它大部分时间都花在了哪里。我知道我可以对任何给定的代码行进行基准测试,但我的理解是,如果我这样做:

Benchmark.measure { test = "a"*1_000_000 }

它将测量每次到达该行所花费的时间。但我想知道我的程序的哪些部分导致整体速度变慢,被多次调用。是否有一种简单的方法可以在基准测试程序的整个过程中将线条组合在一起?

更新:现在意识到这里的正确术语是分析而不是基准测试。

【问题讨论】:

  • @CarySwoveland 不确定特斯拉与它有什么关系,但谢谢!
  • 您有可发布的脚本吗?是不是很多行代码?它是否进行任何外部 API 调用?
  • @lacostenycoder 没有 API 调用,这是一个二进制堆实现
  • This 可能感兴趣。您可能希望添加标签“分析”。

标签: ruby debugging profiling benchmarking


【解决方案1】:

根据您的应用程序,有许多 gem 可能会有所帮助,但您可以尝试其中一些:

https://github.com/flyerhzm/bullet

https://github.com/SamSaffron/memory_profiler

https://github.com/MiniProfiler/rack-mini-profiler

https://github.com/tmm1/stackprof

https://github.com/oozou/ruby-prof-flamegraph

另一种粗略的方法可能是添加Logger 并在您怀疑可能耗时的任何行之前和之后记录。

我们以ruby-prof-flamegraph 为例:

安装gem:

gem install ruby-prof-flamegraph

以及示例实现:

#example.rb

require 'ruby-prof'
require 'ruby-prof-flamegraph'

rubyprof_dir = Gem::Specification.find_by_name('ruby-prof').gem_dir
require "#{rubyprof_dir}/test/prime"

# Profile the code
result = RubyProf.profile do
  run_primes(200)
end

# Print a graph profile to text
printer = RubyProf::FlameGraphPrinter.new(result)
printer.print(STDOUT, {})

要生成图像:

bundle exec ruby example.rb | \
    ~/GitHub/FlameGraph/flamegraph.pl --countname=ms --width=728 > example.svg

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多