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

Update test libraries from ruby/ruby 2023-06-02 #78

Merged
merged 1 commit into from
Jun 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,4 @@ RDoc::Task.new do |doc|
doc.rdoc_dir = "_site" # for github pages
end

task :sync_tool do
require 'fileutils'
FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
end

task :default => :test
17 changes: 17 additions & 0 deletions rakelib/sync_tool.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
task :sync_tool, [:from] do |t, from: nil|
from ||= (File.identical?(__dir__, "rakelib") ? "../ruby/tool" : File.dirname(__dir__))

require 'fileutils'

{
"rakelib/sync_tool.rake" => "rakelib",
"lib/core_assertions.rb" => "test/lib",
"lib/envutil.rb" => "test/lib",
"lib/find_executable.rb" => "test/lib",
"lib/helper.rb" => "test/lib",
}.each do |src, dest|
FileUtils.mkpath(dest)
FileUtils.cp "#{from}/#{src}", dest
rescue Errno::ENOENT
end
end
45 changes: 33 additions & 12 deletions test/lib/core_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -738,35 +738,56 @@ def assert_all_assertions_foreach(msg = nil, *keys, &block)
end
alias all_assertions_foreach assert_all_assertions_foreach

%w[
CLOCK_THREAD_CPUTIME_ID CLOCK_PROCESS_CPUTIME_ID
CLOCK_MONOTONIC
].find do |clk|
if Process.const_defined?(clk)
[clk.to_sym, Process.const_get(clk)].find do |clk|
Process.clock_gettime(clk)
rescue
# Constants may be defined but not implemented, e.g., mingw.
else
PERFORMANCE_CLOCK = clk
end
end
end

# 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})
pend "No PERFORMANCE_CLOCK found" unless defined?(PERFORMANCE_CLOCK)

# Timeout testing generally doesn't work when RJIT compilation happens.
rjit_enabled = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
measure = proc do |arg, message|
st = Process.clock_gettime(PERFORMANCE_CLOCK)
yield(*arg)
t = (Process.clock_gettime(PERFORMANCE_CLOCK) - st)
assert_operator 0, :<=, t, message unless rjit_enabled
t
end

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?
measure[arg, "rehearsal"].nonzero?
end
times.compact!
tmin, tmax = times.minmax
tmax *= tmax / tmin
tmax = 10**Math.log10(tmax).ceil
tbase = 10 ** Math.log10(tmax * ([(tmax / tmin), 2].max ** 2)).ceil
info = "(tmin: #{tmin}, tmax: #{tmax}, tbase: #{tbase})"

seq.each do |i|
next if i == first
t = tmax * i.fdiv(first)
t = tbase * i.fdiv(first)
*arg = pre.call(i)
message = "[#{i}]: in #{t}s"
message = "[#{i}]: in #{t}s #{info}"
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
measure[arg, message]
end
end
end
Expand Down