summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-04-03 14:27:36 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2024-09-10 16:00:29 +0900
commite67ef294fe2f6f6778c063ac7f777e5605b52052 (patch)
treed56facb5da7401e5e4cafa7529a8d82c34508c1a
parent7bb789bf99dfd9b44f92287bf93893e76d1bac28 (diff)
core_assertions.rb: Prefer CPU time clocks
To prevent influence from other processes.
-rw-r--r--tool/lib/core_assertions.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/tool/lib/core_assertions.rb b/tool/lib/core_assertions.rb
index 8761780cfb..5777d79741 100644
--- a/tool/lib/core_assertions.rb
+++ b/tool/lib/core_assertions.rb
@@ -726,18 +726,36 @@ eom
end
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.,
# Array, Range, Enumerator::ArithmeticSequence and other
# Enumerable-s, and each elements should be size factors.
#
# :yield: each elements of +seq+.
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.
rjit_enabled = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
measure = proc do |arg, message|
- st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ st = Process.clock_gettime(PERFORMANCE_CLOCK)
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
t
end