From 7ce4b716bdb5bcfc8b30ffcd034ce7aded1f72b9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 12 Mar 2023 18:50:39 +0900 Subject: Add test for linear performance --- tool/lib/core_assertions.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tool') diff --git a/tool/lib/core_assertions.rb b/tool/lib/core_assertions.rb index 872402727a..12bff170b1 100644 --- a/tool/lib/core_assertions.rb +++ b/tool/lib/core_assertions.rb @@ -738,6 +738,29 @@ eom end alias all_assertions_foreach assert_all_assertions_foreach + def assert_linear_performance(factor: 10_000, first: factor, max: 2, pre: ->(n) {n}) + n = first + arg = pre.call(n) + tmax = (0..factor).map do + st = Process.clock_gettime(Process::CLOCK_MONOTONIC) + yield arg + (Process.clock_gettime(Process::CLOCK_MONOTONIC) - st) + end.max + + 1.upto(max) do |i| + i += 1 if first >= factor + n = i * factor + t = tmax * factor + arg = pre.call(n) + message = "[#{i}]: #{n} in #{t}s" + Timeout.timeout(t, nil, 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(+"") -- cgit v1.2.1