aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-03-16 17:19:01 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-03-16 18:51:02 +0900
commitcae4342dd559e34c1ce6219593f77f0ad80286da (patch)
tree388e3e7361bb8b59d524a01967fe1d47063cc3e2
parent5cffa69c1babb80be17d2544a430dce0f2c22b4e (diff)
downloadruby-cae4342dd559e34c1ce6219593f77f0ad80286da.tar.gz
core_assertions.rb: Refine `assert_linear_performance`
* Use an `Enumerable` as factors, instead of three arguments.
-rw-r--r--test/ruby/test_regexp.rb2
-rw-r--r--tool/lib/core_assertions.rb27
2 files changed, 17 insertions, 12 deletions
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index a2997af106..81dac20648 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -1783,7 +1783,7 @@ class TestRegexp < Test::Unit::TestCase
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
diff --git a/tool/lib/core_assertions.rb b/tool/lib/core_assertions.rb
index 119deccb9f..c195c91482 100644
--- a/tool/lib/core_assertions.rb
+++ b/tool/lib/core_assertions.rb
@@ -738,23 +738,28 @@ eom
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)
+ tmax = (0..(rehearsal || first)).map do
st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
- yield arg
+ 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"
+ seq.each do |i|
+ next if i == first
+ t = tmax * i / first
+ *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