Extract Benchmark.measure on assert_cpu_usage_low
This commit is contained in:
parent
447f426b5c
commit
a450e9304d
Notes:
git
2025-01-10 04:27:34 +00:00
@ -798,33 +798,37 @@ EOT
|
|||||||
MIN_MEASURABLE = 1.0 / MIN_HZ
|
MIN_MEASURABLE = 1.0 / MIN_HZ
|
||||||
|
|
||||||
def assert_cpu_usage_low(msg = nil, pct: 0.05, wait: 1.0, stop: nil)
|
def assert_cpu_usage_low(msg = nil, pct: 0.05, wait: 1.0, stop: nil)
|
||||||
require 'benchmark'
|
|
||||||
|
|
||||||
wait = EnvUtil.apply_timeout_scale(wait)
|
wait = EnvUtil.apply_timeout_scale(wait)
|
||||||
if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
|
if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
|
||||||
warn "test #{msg || 'assert_cpu_usage_low'} too short to be accurate"
|
warn "test #{msg || 'assert_cpu_usage_low'} too short to be accurate"
|
||||||
end
|
end
|
||||||
tms = Benchmark.measure(msg || '') do
|
|
||||||
if stop
|
t0, r0 = Process.times, Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
th = Thread.start {sleep wait; stop.call}
|
|
||||||
yield
|
if stop
|
||||||
th.join
|
th = Thread.start {sleep wait; stop.call}
|
||||||
else
|
yield
|
||||||
begin
|
th.join
|
||||||
Timeout.timeout(wait) {yield}
|
else
|
||||||
rescue Timeout::Error
|
begin
|
||||||
end
|
Timeout.timeout(wait) {yield}
|
||||||
|
rescue Timeout::Error
|
||||||
end
|
end
|
||||||
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 = MIN_MEASURABLE
|
||||||
min_measurable *= 1.30 # add a little (30%) to account for misc. overheads
|
min_measurable *= 1.30 # add a little (30%) to account for misc. overheads
|
||||||
if max < min_measurable
|
if max < min_measurable
|
||||||
max = min_measurable
|
max = min_measurable
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_operator tms.total, :<=, max, msg
|
assert_operator total, :<=, max, msg
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_is_minus_zero(f)
|
def assert_is_minus_zero(f)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user