Skip to content

Commit

Permalink
benchmarks duration even if it fails (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: fsainz <[email protected]>
  • Loading branch information
fsainz and fsainz authored Jul 24, 2022
1 parent 7a36041 commit 39cac08
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/worker_tools/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ module Benchmark
attr_accessor :benchmark

def with_wrapper_benchmark(&block)
@benchmark = ::Benchmark.measure(&block)
ensure
model.meta['duration'] = @benchmark.real.round if model.respond_to?(:meta)
benchmark = ::Benchmark.measure do
block.call
rescue StandardError => e
@benchmark_error = e
end

model.meta['duration'] = benchmark.real.round if model.respond_to?(:meta)
raise @benchmark_error if @benchmark_error
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/worker_tools/benchmark_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ def run; end
@importer.perform(@import)
expect(@importer.model.meta['duration']).must_equal 2
end

it 'assigns the duration even if the block fails' do
def @importer.run
sleep 1
raise StandardError
end

::Benchmark.unstub(:measure)

assert_raises(StandardError) { @importer.perform(@import) }
expect(@importer.model.meta['duration'] >= 1).must_equal true
end
end
end

0 comments on commit 39cac08

Please sign in to comment.