evanphx / benchmark-ips

Provides iteration per second benchmarking for Ruby
MIT License
1.72k stars 97 forks source link

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

Closed fidalgo closed 1 year ago

fidalgo commented 7 years ago

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
zenspider commented 7 years ago

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

ddnexus commented 6 years ago

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
ioquatix commented 6 years ago

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

kbrock commented 5 years ago

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 commented 5 years ago

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

--K

ioquatix commented 5 years ago

Thanks @kbrock I made a comment on the PR.

JacobEvelyn commented 3 years ago

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

nateberkopec commented 3 years ago

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.

kbrock commented 3 years ago

@nateberkopec do you want me to push this again?

nateberkopec commented 3 years ago

@kbrock Yes, please! Sorry I ever closed it

nateberkopec commented 1 year ago

Closed by #111