基准测试 Rails 模型方法

发布于 2024-08-22 03:10:38 字数 937 浏览 0 评论 0原文

Rails 中有类似 Ruby Benchmark 的东西吗?我过去曾使用 Ruby 基准测试来比较不同的代码,但没有一个与 Rails 相关。我想在一些基准测试中使用我的应用程序模型来做一些事情......

#!/usr/bin/ruby
require 'benchmark'

Benchmark.bmbm do |x|
  x.report("Benchmark 1") do 
    1_000_000.times do
      # do something here...
    end
  end

  x.report("Benchmark 2") do
    1_000_000.times do
      # Do something else here...
    end
  end
end

这给了我一些像这样的输出:

Rehearsal -----------------------------------------------
Benchmark 1   0.070000   0.000000   0.070000 (  0.069236)
Benchmark 2   0.070000   0.000000   0.070000 (  0.069227)
-------------------------------------- total: 0.140000sec

                  user     system      total        real
Benchmark 1   0.070000   0.000000   0.070000 (  0.069793)
Benchmark 2   0.070000   0.000000   0.070000 (  0.069203)

我研究了脚本/性能/基准测试器和脚本/性能/分析器,但我无法弄清楚找出一种比较方法的方法。我也考虑过在测试中这样做,但我没有任何断言,所以这似乎没有意义。

Is there something similar to Ruby Benchmark within Rails? I have used Ruby benchmark in the past to compare different bits of code, but none of it was Rails related. I would like to use my application models in some benchmarking to do something along the lines of...

#!/usr/bin/ruby
require 'benchmark'

Benchmark.bmbm do |x|
  x.report("Benchmark 1") do 
    1_000_000.times do
      # do something here...
    end
  end

  x.report("Benchmark 2") do
    1_000_000.times do
      # Do something else here...
    end
  end
end

Which gives me some output like this:

Rehearsal -----------------------------------------------
Benchmark 1   0.070000   0.000000   0.070000 (  0.069236)
Benchmark 2   0.070000   0.000000   0.070000 (  0.069227)
-------------------------------------- total: 0.140000sec

                  user     system      total        real
Benchmark 1   0.070000   0.000000   0.070000 (  0.069793)
Benchmark 2   0.070000   0.000000   0.070000 (  0.069203)

I looked into script/performance/benchmarker and script/performance/profiler, but I couldn't figure out a way to compare methods. I also looked into doing it within a test, but I don't have any sort of assertions, so that didn't seem to make sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-08-29 03:10:38

对 Rails 应用程序进行性能测试可能就是您所需要的。我认为这就是独立 Rails 所能做的一切。

Performance testing your rails application might be all you need. I think that's all you can do with standalone Rails.

雨夜星沙 2024-08-29 03:10:38

Rails 添加了一个基准生成器。

rails g benchmark some_benchmark

它会在处创建文件

script/benchmarks/some_benchmark.rb

它会在运行bundle后自动将ips-benchmark gem添加到您的gemfile中,但如果它不只是将其添加到您的gemfile中的开发部分下,

。在终端中运行它,

ruby script/benchmarks/some_benchmark.rb

因为它在基准文件中加载 config/environment.rb 所有模型都可以访问。

Rails added a benchmark generator.

rails g benchmark some_benchmark

it creates the file at

script/benchmarks/some_benchmark.rb

It should automatically add the ips-benchmark gem to your gemfile after running bundle but if it doesn't just add it under the development section in your gemfile.

run it in the terminal by doing

ruby script/benchmarks/some_benchmark.rb

since it loads config/environment.rb in the benchmark file all models are accessible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文