benchmark-ips
benchmark-ips copied to clipboard
Allow to return the report output to a String instead of printing it on STDOUT
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
That should be easy to do, IIRC. Wanna take a whack at it?
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
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
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.
Does #89 meet your needs? Please comment there if you need something different
--K
Thanks @kbrock I made a comment on the PR.
Just wondering what the status of this is @nateberkopec @ioquatix @kbrock as this is a big pain point for us too
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 do you want me to push this again?
@kbrock Yes, please! Sorry I ever closed it
Closed by #111