Module: RSpecPower::PerformanceHelpers
- Defined in:
- lib/rspec_power/performance.rb
Instance Method Summary collapse
-
#with_maximum_execution_time(max_duration_ms) ⇒ Object
Ensures the given block completes within the specified duration (milliseconds).
Instance Method Details
#with_maximum_execution_time(max_duration_ms) ⇒ Object
Ensures the given block completes within the specified duration (milliseconds). Raises RSpec::Expectations::ExpectationNotMetError if the threshold is exceeded.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rspec_power/performance.rb', line 5 def with_maximum_execution_time(max_duration_ms) raise ArgumentError, "with_maximum_execution_time requires a block" unless block_given? max_ms = max_duration_ms.to_f start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield ensure elapsed_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000.0 # Do not override an existing exception if $!.nil? && elapsed_ms > max_ms formatted_elapsed = format("%.3f", elapsed_ms) formatted_limit = format("%.3f", max_ms) raise RSpec::Expectations::ExpectationNotMetError, "Execution time exceeded: #{formatted_elapsed}ms > #{formatted_limit}ms" end end |