SQL 如何在 rails 控制台中获取执行时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3522250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:15:56  来源:igfitidea点击:

How to get execution time in rails console?

sqlruby-on-railstimeconsole

提问by Bohdan

I want compare time of execution Post.alland SELECT * FROM posts(or some other statements) How can i get execution time of Post.all?

我想比较执行时间Post.allSELECT * FROM posts(或其他一些语句)如何获得执行时间Post.all

回答by Shadwell

timing = Benchmark.measure { Post.all }

The various attributes of the object returned (Benchmark::Tms) are provided here.

返回的对象的各种属性 (Benchmark::Tms) 在此处提供。

回答by Bohdan

With benchmark-ipsgem:

使用benchmark-ipsgem:

2.3.0 :001 > require 'benchmark/ips'
=> true 
2.3.0 :002 > Benchmark.ips do |x|
2.3.0 :003 >       x.report("add: ")        { 1+2 }
2.3.0 :004?>       x.report("div: ") {1/2}
2.3.0 :005?>       x.report("iis: ") {1/2.0}
2.3.0 :006?>   end
Warming up --------------------------------------
           add:    280.299k i/100ms
           div:    278.189k i/100ms
           iis:    266.526k i/100ms
Calculating -------------------------------------
           add:      11.381M (± 4.5%) i/s -     56.901M in   5.010669s
           div:       9.879M (± 4.6%) i/s -     49.518M in   5.024084s
           iis:       9.289M (± 4.2%) i/s -     46.376M in   5.001639s

回答by Igor Kasyanchuk

Here is my version of how to measure performance automatically in rails console using gem: https://github.com/igorkasyanchuk/execution_time

这是我如何使用 gem 在 rails 控制台中自动测量性能的版本:https: //github.com/igorkasynchuk/execution_time

It's showing similar information which you already see during requests.

它显示了您在请求期间已经看到的类似信息。

Sample:

样本:

[METRICS] Completed in 908.3ms | Allocations: 2894 | ActiveRecord: 0.9ms (queries: 13)

[METRICS] Completed in 908.3ms | Allocations: 2894 | ActiveRecord: 0.9ms (queries: 13)