基准测试 Rails 模型方法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对 Rails 应用程序进行性能测试可能就是您所需要的。我认为这就是独立 Rails 所能做的一切。
Performance testing your rails application might be all you need. I think that's all you can do with standalone Rails.
Rails 添加了一个基准生成器。
它会在处创建文件
它会在运行bundle后自动将ips-benchmark gem添加到您的gemfile中,但如果它不只是将其添加到您的gemfile中的开发部分下,
。在终端中运行它,
因为它在基准文件中加载 config/environment.rb 所有模型都可以访问。
Rails added a benchmark generator.
it creates the file at
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
since it loads config/environment.rb in the benchmark file all models are accessible.