Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core_assertions.rb: Relax assert_linear_performance #7554

Merged
merged 1 commit into from Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/ruby/test_regexp.rb
Expand Up @@ -1783,7 +1783,7 @@ def test_linear_time_p

def test_linear_performance
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
end
end
Expand Down
39 changes: 25 additions & 14 deletions tool/lib/core_assertions.rb
Expand Up @@ -738,23 +738,34 @@ def assert_all_assertions_foreach(msg = nil, *keys, &block)
end
alias all_assertions_foreach assert_all_assertions_foreach

def assert_linear_performance(factor: 10_000, first: factor, max: 2, rehearsal: first, pre: ->(n) {n})
n = first
arg = pre.call(n)
tmax = (0..rehearsal).map do
# Expect +seq+ to respond to +first+ and +each+ methods, e.g.,
# Array, Range, Enumerator::ArithmeticSequence and other
# Enumerable-s, and each elements should be size factors.
#
# :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)
yield arg
(Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
end.max

(first >= factor ? 2 : 1).upto(max) do |i|
n = i * factor
t = tmax * factor
arg = pre.call(n)
message = "[#{i}]: #{n} in #{t}s"
yield(*arg)
t = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st)
assert_operator 0, :<=, t
t.nonzero?
end
times.compact!
tmin, tmax = times.minmax
tmax *= tmax / tmin
tmax = 10**Math.log10(tmax).ceil

seq.each do |i|
next if i == first
t = (tmax * i).to_f
*arg = pre.call(i)
message = "[#{i}]: in #{t}s"
Timeout.timeout(t, nil, message) do
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield arg
yield(*arg)
assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
end
end
Expand Down