问题描述
我有一个类似的基准:
benchmark_result = Benchmark.bm do |x|
x.report { send(test_name) }
end
运行此命令时,我看到了两个地方的输出:
When I run this, I'm seeing output from two places:
-
report
块中的send(test_name)
.我想继续看到此输出. - Benchmark块的输出,即结果基准报告打印到控制台.我不希望这种情况发生.
- The
send(test_name)
in thereport
block. I want to continue seeing this output. - 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.bm
或Benchmark.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
这篇关于运行基准测试,但不打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!