mirror of
https://github.com/ruby/ruby.git
synced 2025-09-17 17:43:59 +02:00
core_assertions.rb: Prefer CPU time clocks
To prevent influence from other processes.
This commit is contained in:
parent
7bb789bf99
commit
e67ef294fe
1 changed files with 20 additions and 2 deletions
|
@ -726,18 +726,36 @@ eom
|
||||||
end
|
end
|
||||||
alias all_assertions_foreach assert_all_assertions_foreach
|
alias all_assertions_foreach assert_all_assertions_foreach
|
||||||
|
|
||||||
|
%w[
|
||||||
|
CLOCK_THREAD_CPUTIME_ID CLOCK_PROCESS_CPUTIME_ID
|
||||||
|
CLOCK_MONOTONIC
|
||||||
|
].find do |clk|
|
||||||
|
if Process.const_defined?(clk)
|
||||||
|
clk = clk.to_sym
|
||||||
|
begin
|
||||||
|
Process.clock_gettime(clk)
|
||||||
|
rescue
|
||||||
|
# Constants may be defined but not implemented, e.g., mingw.
|
||||||
|
else
|
||||||
|
PERFORMANCE_CLOCK = clk
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Expect +seq+ to respond to +first+ and +each+ methods, e.g.,
|
# Expect +seq+ to respond to +first+ and +each+ methods, e.g.,
|
||||||
# Array, Range, Enumerator::ArithmeticSequence and other
|
# Array, Range, Enumerator::ArithmeticSequence and other
|
||||||
# Enumerable-s, and each elements should be size factors.
|
# Enumerable-s, and each elements should be size factors.
|
||||||
#
|
#
|
||||||
# :yield: each elements of +seq+.
|
# :yield: each elements of +seq+.
|
||||||
def assert_linear_performance(seq, rehearsal: nil, pre: ->(n) {n})
|
def assert_linear_performance(seq, rehearsal: nil, pre: ->(n) {n})
|
||||||
|
pend "No PERFORMANCE_CLOCK found" unless defined?(PERFORMANCE_CLOCK)
|
||||||
|
|
||||||
# Timeout testing generally doesn't work when RJIT compilation happens.
|
# Timeout testing generally doesn't work when RJIT compilation happens.
|
||||||
rjit_enabled = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
|
rjit_enabled = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
|
||||||
measure = proc do |arg, message|
|
measure = proc do |arg, message|
|
||||||
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
st = Process.clock_gettime(PERFORMANCE_CLOCK)
|
||||||
yield(*arg)
|
yield(*arg)
|
||||||
t = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
|
t = (Process.clock_gettime(PERFORMANCE_CLOCK) - st)
|
||||||
assert_operator 0, :<=, t, message unless rjit_enabled
|
assert_operator 0, :<=, t, message unless rjit_enabled
|
||||||
t
|
t
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue