针对几个矩阵的Ruby基准测试

时间:2011-11-29 08:58:25

标签: ruby

使用ruby Benchmark stdlib或同等宝石对几种矩阵进行基准测试的DRYest方法是什么?例如,如果我对一些SQL进行基准测试,我可能想尝试表大小,各种查询,查询数等组合的所有组合。

1 个答案:

答案 0 :(得分:3)

require 'benchmark'

table_sizes = [100, 1000]
queries = ["QUERY1", "QUERY2"]
num_queries = [1, 2, 3]
combis = table_sizes.product(queries, num_queries)

Benchmark.bm(20) do |x|
  combis.each do |combi| # or |size,query,num| and use those
    x.report( combi.inspect ){ sleep rand } #do something with combi
  end
end

输出:

                           user     system      total        real
[100, "QUERY1", 1]     0.000000   0.000000   0.000000 (  0.524222)
[100, "QUERY1", 2]     0.000000   0.000000   0.000000 (  0.075334)
[100, "QUERY1", 3]     0.000000   0.000000   0.000000 (  0.041683)
[100, "QUERY2", 1]     0.000000   0.000000   0.000000 (  0.227865)
[100, "QUERY2", 2]     0.000000   0.000000   0.000000 (  0.558131)
[100, "QUERY2", 3]     0.000000   0.000000   0.000000 (  0.284121)
[1000, "QUERY1", 1]    0.000000   0.000000   0.000000 (  0.496799)
[1000, "QUERY1", 2]    0.000000   0.000000   0.000000 (  0.304552)
[1000, "QUERY1", 3]    0.000000   0.000000   0.000000 (  0.918314)
[1000, "QUERY2", 1]    0.000000   0.000000   0.000000 (  0.332485)
[1000, "QUERY2", 2]    0.000000   0.000000   0.000000 (  0.379680)
[1000, "QUERY2", 3]    0.000000   0.000000   0.000000 (  0.804835)