本文介绍了运行基准测试,但不打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的基准:

benchmark_result = Benchmark.bm do |x|
  x.report { send(test_name) }
end

运行此命令时,我看到了两个地方的输出:

When I run this, I'm seeing output from two places:

  1. report块中的send(test_name).我想继续看到此输出.
  2. Benchmark块的输出,即结果基准报告打印到控制台.我不希望这种情况发生.
  1. The send(test_name) in the report block. I want to continue seeing this output.
  2. The output from the Benchmark block, i.e. the resulting benchmark report is printed to the console. I don't want this to happen.

我从此处中看到了如何临时隐藏控制台输出.但是问题是我希望内部块继续打印其输出.我只是不想看到基准测试结果.

I've seen from here how to temporarily hide the console output. But the problem is that I want the inner block to continue printing its output. I just don't want to see the benchmark results.

推荐答案

当您调用由Benchmark.bmBenchmark.benchmark发送到块的Benchmark::Report对象的report方法时,它将打印到STDOUT.如果您只是对基准指标感兴趣而无需打印报告,则可以执行以下操作:

When you call the report method on the Benchmark::Report object sent to the block by Benchmark.bm or Benchmark.benchmark, it will print to STDOUT. If you're just interested in the benchmark metrics without printing a report, you can do this:

benchmark_result = Benchmark.measure do
  send(test_name)
end

它返回一个如下所示的Benchmark::Tms对象:

It returns a Benchmark::Tms object that looks like this:

 => #<Benchmark::Tms:0x007fb5b1b40118
 @cstime=0.0,
 @cutime=0.0,
 @label="",
 @real=4.5693013817071915e-05,
 @stime=0.0,
 @total=0.0,
 @utime=0.0>

如果您只是对执行块所用的实时时间感兴趣,请执行以下操作(返回Float):

If you're just interested in the elapsed real time used to execute your block, do the following (returns a Float):

benchmark_result = Benchmark.realtime do
  send(test_name)
end

这篇关于运行基准测试,但不打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:24