1require 'benchmark_driver/output/simple'
2
3# This replicates the legacy benchmark/driver.rb behavior.
4class BenchmarkDriver::Output::Driver < BenchmarkDriver::Output::Simple
5  def initialize(*)
6    super
7    @stdout = $stdout
8    @strio  = StringIO.new
9    $stdout = IOMultiplexer.new(@stdout, @strio)
10  end
11
12  def with_benchmark(*)
13    super
14  ensure
15    logfile = "bmlog-#{Time.now.strftime('%Y%m%d-%H%M%S')}.#{$$}.log"
16    puts "\nLog file: #{logfile}"
17
18    $stdout = @stdout
19    File.write(logfile, @strio.tap(&:rewind).read)
20  end
21
22  class IOMultiplexer
23    def initialize(io1, io2)
24      @io1 = io1
25      @io2 = io2
26    end
27
28    [:write, :sync, :sync=, :puts, :print, :flush].each do |method|
29      define_method(method) do |*args|
30        @io1.send(method, *args)
31        @io2.send(method, *args)
32      end
33    end
34  end
35  private_constant :IOMultiplexer
36end
37