core_assertions.rb: Relax assert_linear_performance
* Use an `Enumerable` as factors, instead of three arguments. * Include `assert_operator` time in rehearsal time. * Round up max expected time.
This commit is contained in:
parent
10e4fa3a0f
commit
ccd2dbc4c1
Notes:
git
2023-03-17 17:41:24 +00:00
@ -1783,7 +1783,7 @@ class TestRegexp < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_linear_performance
|
def test_linear_performance
|
||||||
pre = ->(n) {[Regexp.new("a?" * n + "a" * n), "a" * n]}
|
pre = ->(n) {[Regexp.new("a?" * n + "a" * n), "a" * n]}
|
||||||
assert_linear_performance(factor: 29, first: 10, max: 1, pre: pre) do |re, s|
|
assert_linear_performance([10, 29], pre: pre) do |re, s|
|
||||||
re =~ s
|
re =~ s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -738,23 +738,34 @@ eom
|
|||||||
end
|
end
|
||||||
alias all_assertions_foreach assert_all_assertions_foreach
|
alias all_assertions_foreach assert_all_assertions_foreach
|
||||||
|
|
||||||
def assert_linear_performance(factor: 10_000, first: factor, max: 2, rehearsal: first, pre: ->(n) {n})
|
# Expect +seq+ to respond to +first+ and +each+ methods, e.g.,
|
||||||
n = first
|
# Array, Range, Enumerator::ArithmeticSequence and other
|
||||||
arg = pre.call(n)
|
# Enumerable-s, and each elements should be size factors.
|
||||||
tmax = (0..rehearsal).map do
|
#
|
||||||
|
# :yield: each elements of +seq+.
|
||||||
|
def assert_linear_performance(seq, rehearsal: nil, pre: ->(n) {n})
|
||||||
|
first = seq.first
|
||||||
|
*arg = pre.call(first)
|
||||||
|
times = (0..(rehearsal || (2 * first))).filter_map do
|
||||||
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
yield arg
|
yield(*arg)
|
||||||
(Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
|
t = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
|
||||||
end.max
|
assert_operator 0, :<=, t
|
||||||
|
t.nonzero?
|
||||||
|
end
|
||||||
|
times.compact!
|
||||||
|
tmin, tmax = times.minmax
|
||||||
|
tmax *= tmax / tmin
|
||||||
|
tmax = 10**Math.log10(tmax).ceil
|
||||||
|
|
||||||
(first >= factor ? 2 : 1).upto(max) do |i|
|
seq.each do |i|
|
||||||
n = i * factor
|
next if i == first
|
||||||
t = tmax * factor
|
t = (tmax * i).to_f
|
||||||
arg = pre.call(n)
|
*arg = pre.call(i)
|
||||||
message = "[#{i}]: #{n} in #{t}s"
|
message = "[#{i}]: in #{t}s"
|
||||||
Timeout.timeout(t, nil, message) do
|
Timeout.timeout(t, nil, message) do
|
||||||
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
yield arg
|
yield(*arg)
|
||||||
assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
|
assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user