diff options
author | Hiroshi SHIBATA <[email protected]> | 2025-01-10 12:59:29 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2025-01-10 13:27:16 +0900 |
commit | a450e9304db8e540977958ddb5c60a65363a4ea9 (patch) | |
tree | 2f6a81f77e420afba76b2f3ed0b80342fe56190f | |
parent | 447f426b5c35d58004f6faa72e5f4761fb9fc41f (diff) |
Extract Benchmark.measure on assert_cpu_usage_low
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12544
-rw-r--r-- | tool/lib/test/unit/assertions.rb | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/tool/lib/test/unit/assertions.rb b/tool/lib/test/unit/assertions.rb index fe3351107e..19581fc3ab 100644 --- a/tool/lib/test/unit/assertions.rb +++ b/tool/lib/test/unit/assertions.rb @@ -798,33 +798,37 @@ EOT MIN_MEASURABLE = 1.0 / MIN_HZ def assert_cpu_usage_low(msg = nil, pct: 0.05, wait: 1.0, stop: nil) - require 'benchmark' - wait = EnvUtil.apply_timeout_scale(wait) if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c warn "test #{msg || 'assert_cpu_usage_low'} too short to be accurate" end - tms = Benchmark.measure(msg || '') do - if stop - th = Thread.start {sleep wait; stop.call} - yield - th.join - else - begin - Timeout.timeout(wait) {yield} - rescue Timeout::Error - end + + t0, r0 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC) + + if stop + th = Thread.start {sleep wait; stop.call} + yield + th.join + else + begin + Timeout.timeout(wait) {yield} + rescue Timeout::Error end end - max = pct * tms.real + t1, r1 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC) + + total = t1.utime - t0.utime + t1.stime - t0.stime + t1.cutime - t0.cutime + t1.cstime - t0.cstime + real = r1 - r0 + + max = pct * real min_measurable = MIN_MEASURABLE min_measurable *= 1.30 # add a little (30%) to account for misc. overheads if max < min_measurable max = min_measurable end - assert_operator tms.total, :<=, max, msg + assert_operator total, :<=, max, msg end def assert_is_minus_zero(f) |