mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
assert_cpu_usage_low with timeout scale
* test/lib/test/unit/assertions.rb (assert_cpu_usage_low): apply the timeout scale to measuring period. this assertion is very runtime environment dependent.
This commit is contained in:
parent
42f0a8fd6f
commit
c4cbaef216
2 changed files with 18 additions and 9 deletions
|
@ -761,15 +761,27 @@ eom
|
||||||
MIN_HZ = MiniTest::Unit::TestCase.windows? ? 67 : 100
|
MIN_HZ = MiniTest::Unit::TestCase.windows? ? 67 : 100
|
||||||
MIN_MEASURABLE = 1.0 / MIN_HZ
|
MIN_MEASURABLE = 1.0 / MIN_HZ
|
||||||
|
|
||||||
def assert_cpu_usage_low(msg = nil, pct: 0.05)
|
def assert_cpu_usage_low(msg = nil, pct: 0.05, wait: 0.1, stop: nil)
|
||||||
require 'benchmark'
|
require 'benchmark'
|
||||||
|
|
||||||
tms = Benchmark.measure(msg || '') { yield }
|
wait = EnvUtil.apply_timeout_scale(wait)
|
||||||
max = pct * tms.real
|
if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
|
||||||
if tms.real < 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
|
||||||
|
th = Thread.start {sleep wait; stop.call}
|
||||||
|
yield
|
||||||
|
th.join
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
Timeout.timeout(wait) {yield}
|
||||||
|
rescue Timeout::Error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
max = pct * tms.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
|
||||||
|
|
|
@ -567,11 +567,8 @@ class TestIO < Test::Unit::TestCase
|
||||||
msg = 'r58534 [ruby-core:80969] [Backport #13533]'
|
msg = 'r58534 [ruby-core:80969] [Backport #13533]'
|
||||||
IO.pipe do |r,w|
|
IO.pipe do |r,w|
|
||||||
r.nonblock = true
|
r.nonblock = true
|
||||||
assert_cpu_usage_low(msg) do
|
assert_cpu_usage_low(msg, stop: ->{w.close}) do
|
||||||
th = Thread.new { IO.copy_stream(r, IO::NULL) }
|
IO.copy_stream(r, IO::NULL)
|
||||||
sleep 0.1
|
|
||||||
w.close
|
|
||||||
th.join
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue