Skip to content

Commit a450e93

Browse files
committed
Extract Benchmark.measure on assert_cpu_usage_low
1 parent 447f426 commit a450e93

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

tool/lib/test/unit/assertions.rb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -798,33 +798,37 @@ def assert_no_warning(pat, msg = nil)
798798
MIN_MEASURABLE = 1.0 / MIN_HZ
799799

800800
def assert_cpu_usage_low(msg = nil, pct: 0.05, wait: 1.0, stop: nil)
801-
require 'benchmark'
802-
803801
wait = EnvUtil.apply_timeout_scale(wait)
804802
if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
805803
warn "test #{msg || 'assert_cpu_usage_low'} too short to be accurate"
806804
end
807-
tms = Benchmark.measure(msg || '') do
808-
if stop
809-
th = Thread.start {sleep wait; stop.call}
810-
yield
811-
th.join
812-
else
813-
begin
814-
Timeout.timeout(wait) {yield}
815-
rescue Timeout::Error
816-
end
805+
806+
t0, r0 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC)
807+
808+
if stop
809+
th = Thread.start {sleep wait; stop.call}
810+
yield
811+
th.join
812+
else
813+
begin
814+
Timeout.timeout(wait) {yield}
815+
rescue Timeout::Error
817816
end
818817
end
819818

820-
max = pct * tms.real
819+
t1, r1 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC)
820+
821+
total = t1.utime - t0.utime + t1.stime - t0.stime + t1.cutime - t0.cutime + t1.cstime - t0.cstime
822+
real = r1 - r0
823+
824+
max = pct * real
821825
min_measurable = MIN_MEASURABLE
822826
min_measurable *= 1.30 # add a little (30%) to account for misc. overheads
823827
if max < min_measurable
824828
max = min_measurable
825829
end
826830

827-
assert_operator tms.total, :<=, max, msg
831+
assert_operator total, :<=, max, msg
828832
end
829833

830834
def assert_is_minus_zero(f)

0 commit comments

Comments
 (0)