ruby-changes:46888
From: normal <ko1@a...>
Date: Sat, 3 Jun 2017 10:04:35 +0900 (JST)
Subject: [ruby-changes:46888] normal:r59003 (trunk): test: attempt to reduce failures in assert_cpu_usage_low
normal 2017-06-03 10:04:30 +0900 (Sat, 03 Jun 2017) New Revision: 59003 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59003 Log: test: attempt to reduce failures in assert_cpu_usage_low Try to make this test less fragile by taking into account the worst case kernel timing resolution. [ruby-core:81540] * test/lib/test/unit/assertions.rb (assert_cpu_usage_low): clamp measurement to minimum measurable time and warn about tests being too short to measure * test/ruby/test_io.rb (test_copy_stream_no_busy_wait): remove pct kwarg and rely on assert_cpu_usage_low defaults Modified files: trunk/test/lib/test/unit/assertions.rb trunk/test/ruby/test_io.rb Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 59002) +++ test/ruby/test_io.rb (revision 59003) @@ -536,7 +536,7 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L536 msg = 'r58534 [ruby-core:80969] [Backport #13533]' IO.pipe do |r,w| r.nonblock = true - assert_cpu_usage_low(msg, pct: 0.11) do + assert_cpu_usage_low(msg) do th = Thread.new { IO.copy_stream(r, IO::NULL) } sleep 0.1 w.close Index: test/lib/test/unit/assertions.rb =================================================================== --- test/lib/test/unit/assertions.rb (revision 59002) +++ test/lib/test/unit/assertions.rb (revision 59003) @@ -715,10 +715,24 @@ eom https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L715 skip end - def assert_cpu_usage_low(msg = nil, pct: 0.015) + def assert_cpu_usage_low(msg = nil, pct: 0.01) require 'benchmark' + tms = Benchmark.measure(msg || '') { yield } max = pct * tms.real + if tms.real < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c + warn "test #{msg || 'assert_cpu_usage_low'} too short to be accurate" + end + + # kernel resolution can limit the minimum time we can measure + # [ruby-core:81540] + min_hz = windows? ? 67 : 100 + min_measurable = 1.0 / min_hz + min_measurable *= 1.10 # add a little (10%) to account for misc. overheads + if max < min_measurable + max = min_measurable + end + assert_operator tms.total, :<=, max, msg end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/