summaryrefslogtreecommitdiff
path: root/tool
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-03-17 15:53:18 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-03-18 02:41:02 +0900
commitccd2dbc4c15ffb5bde0141a98ec02603c1597243 (patch)
treee4d180ba3ccf4c1c04e0e9cb0f0c2f5abda5bd70 /tool
parent10e4fa3a0f05f72733d132794cedd08906b40196 (diff)
downloadruby-ccd2dbc4c15ffb5bde0141a98ec02603c1597243.tar.gz
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.
Diffstat (limited to 'tool')
-rw-r--r--tool/lib/core_assertions.rb39
1 files changed, 25 insertions, 14 deletions
diff --git a/tool/lib/core_assertions.rb b/tool/lib/core_assertions.rb
index 119deccb9f..d9d633a0ac 100644
--- a/tool/lib/core_assertions.rb
+++ b/tool/lib/core_assertions.rb
@@ -738,23 +738,34 @@ 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)
+ 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