summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2023-03-28 18:00:13 +0900
committernagachika <nagachika@ruby-lang.org>2023-03-29 15:27:07 +0900
commit66ad6e533e968cb6a8f54674b1c3a3fd0a316893 (patch)
tree90af5953ecac62c70f1872ee2e4244ef90a05fe6
parent8db69f684b425bf65be3ac13a3d415be0e5dc5df (diff)
downloadruby-66ad6e533e968cb6a8f54674b1c3a3fd0a316893.tar.gz
Added assert_linear_performance for URI tests
-rw-r--r--tool/lib/core_assertions.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/tool/lib/core_assertions.rb b/tool/lib/core_assertions.rb
index 5d9af12ca1..5ae9019c46 100644
--- a/tool/lib/core_assertions.rb
+++ b/tool/lib/core_assertions.rb
@@ -726,6 +726,39 @@ eom
end
alias all_assertions_foreach assert_all_assertions_foreach
+ # 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))).map do
+ st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ 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.fdiv(first)
+ *arg = pre.call(i)
+ message = "[#{i}]: in #{t}s"
+ Timeout.timeout(t, Timeout::Error, message) do
+ st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ yield(*arg)
+ assert_operator (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st), :<=, t, message
+ end
+ end
+ end
+
def diff(exp, act)
require 'pp'
q = PP.new(+"")