Benchmarking in the Rails Console

Public, Rails Errors, Ruby General

harrylevine

Created: Dec 22, 2017     Updated: Jan 01, 2018


timing = Benchmark.measure { Post.all }

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

Here is a sample application of this in the rails console:

[10] pry(main)> Benchmark.measure { Directory::CountyZipcode.pluck(:state).uniq.sort }
  [read] (24.0ms)  SELECT `county_zipcodes`.`state` FROM `county_zipcodes`
=> #<Benchmark::Tms:0x007fa731d4e8a8
 @cstime=0.0,
 @cutime=0.0,
 @label="",
 @real=0.18990400014445186,
 @stime=0.03000000000000025,
 @total=0.16999999999999726,
 @utime=0.13999999999999702>

[11] pry(main)> Benchmark.measure { Directory::CountyZipcode.select(:state).order(:state).distinct.map(&:state) }
  [read] Directory::CountyZipcode Load (35.3ms)  SELECT DISTINCT `county_zipcodes`.`state` FROM `county_zipcodes` ORDER BY `county_zipcodes`.`state` ASC
=> #<Benchmark::Tms:0x007fa7372831c0
 @cstime=0.0,
 @cutime=0.0,
 @label="",
 @real=0.052678000181913376,
 @stime=0.009999999999999787,
 @total=0.02999999999999936,
 @utime=0.019999999999999574>

https://stackoverflow.com/a/3522311/3899985