benchmark-ips icon indicating copy to clipboard operation
benchmark-ips copied to clipboard

Allow to return the report output to a String instead of printing it on STDOUT

Open fidalgo opened this issue 7 years ago • 10 comments

It would be nice, to have a way to capture the output to a String in order to print it or send it to an external service, like Bugsnag. Also in a PASS like heroku it's hard to get the reports because the logs are combined from logger, stdout and stderr.

By output I mean the full report:

Calculating -------------------------------------
            addition    71.254k i/100ms
           addition2    68.658k i/100ms
           addition3    83.079k i/100ms
addition-test-long-label
                        70.129k i/100ms
-------------------------------------------------
            addition     4.955M (± 8.7%) i/s -     24.155M
           addition2    24.011M (± 9.5%) i/s -    114.246M
           addition3    23.958M (±10.1%) i/s -    115.064M
addition-test-long-label
                         5.014M (± 9.1%) i/s -     24.545M

Comparison:
           addition2: 24011974.8 i/s
           addition3: 23958619.8 i/s - 1.00x slower
addition-test-long-label:  5014756.0 i/s - 4.79x slower
            addition:  4955278.9 i/s - 4.85x slower

fidalgo avatar Nov 11 '16 19:11 fidalgo

That should be easy to do, IIRC. Wanna take a whack at it?

zenspider avatar Feb 06 '17 22:02 zenspider

Meanwhile maybe you can do something like:

def capture_ips_report(label, &block)
  old_stdout = $stdout
  $stdout    = StringIO.new

  ::Benchmark.ips do |x|
    # add more stuff here
    x.report(label) do
      block.call
    end
  end

  # return the captured output
  $stdout.string
ensure
  $stdout = old_stdout
end

ddnexus avatar Jan 23 '18 04:01 ddnexus

The function that does the printing should just take an optional output stream. It's the simplest and most flexible solution - can output to pipe, stderr, stringio, etc

ioquatix avatar Feb 14 '18 22:02 ioquatix

if you don't want the comparison results, you create a suite and have that output to the appropriate output.

capture! will not be captured, that may be possible at some time.

my_stream = StringIO.new
Benchmark.ips(:quiet => true, :suite => MySuite.new(my_stream)) do |x|
  x.report(label) { block.call }
end

MySuite can probably be ripped off of StdoutReport

I'll see if I can tweak that file to let you output to any file. The changes to Compare will probably be more work.

kbrock avatar Oct 19 '18 20:10 kbrock

Does #89 meet your needs? Please comment there if you need something different

--K

kbrock avatar Oct 25 '18 17:10 kbrock

Thanks @kbrock I made a comment on the PR.

ioquatix avatar Oct 25 '18 20:10 ioquatix

Just wondering what the status of this is @nateberkopec @ioquatix @kbrock as this is a big pain point for us too

JacobEvelyn avatar Feb 08 '21 20:02 JacobEvelyn

I dont really remember why I closed #89. I think someone needs to take #89 and reopen it, I can't because the original branch was deleted.

nateberkopec avatar Feb 09 '21 17:02 nateberkopec

@nateberkopec do you want me to push this again?

kbrock avatar Feb 18 '21 23:02 kbrock

@kbrock Yes, please! Sorry I ever closed it

nateberkopec avatar Feb 22 '21 20:02 nateberkopec

Closed by #111

nateberkopec avatar Mar 08 '23 00:03 nateberkopec