mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rake/cpu_counter.rb: use Etc.nprocessors
* lib/rake/cpu_counter.rb (count): prefer Etc.nprocessors, which is hundreds times faster. * test/rake/test_rake_cpu_counter.rb: add tests for features, and remove useless tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48127 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5940b1f6fc
commit
26143970c1
2 changed files with 59 additions and 29 deletions
|
@ -44,6 +44,18 @@ module Rake
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'etc'
|
||||||
|
rescue LoadError
|
||||||
|
else
|
||||||
|
if Etc.respond_to?(:nprocessors)
|
||||||
|
undef count
|
||||||
|
def count
|
||||||
|
return Etc.nprocessors
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def count_via_java_runtime
|
def count_via_java_runtime
|
||||||
Java::Java.lang.Runtime.getRuntime.availableProcessors
|
Java::Java.lang.Runtime.getRuntime.availableProcessors
|
||||||
rescue StandardError
|
rescue StandardError
|
||||||
|
|
|
@ -8,43 +8,61 @@ class TestRakeCpuCounter < Rake::TestCase
|
||||||
@cpu_counter = Rake::CpuCounter.new
|
@cpu_counter = Rake::CpuCounter.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_count_via_win32
|
def test_count
|
||||||
if Rake::Win32.windows? then
|
num = @cpu_counter.count
|
||||||
assert_kind_of Numeric, @cpu_counter.count_via_win32
|
skip 'cannot count CPU' if num == nil
|
||||||
else
|
assert_kind_of Numeric, num
|
||||||
assert_nil @cpu_counter.count_via_win32
|
assert_operator num, :>=, 1
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_in_path_command
|
def test_count_with_default_nil
|
||||||
with_ruby_in_path do |ruby|
|
def @cpu_counter.count; nil; end
|
||||||
assert_equal ruby, @cpu_counter.in_path_command(ruby)
|
assert_equal(8, @cpu_counter.count_with_default(8))
|
||||||
end
|
assert_equal(4, @cpu_counter.count_with_default)
|
||||||
rescue Errno::ENOENT => e
|
|
||||||
raise unless e.message =~ /\bwhich\b/
|
|
||||||
|
|
||||||
skip 'cannot find which for this test'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_run
|
def test_count_with_default_raise
|
||||||
with_ruby_in_path do |ruby|
|
def @cpu_counter.count; raise; end
|
||||||
assert_equal 7, @cpu_counter.run(ruby, '-e', 'puts 3 + 4')
|
assert_equal(8, @cpu_counter.count_with_default(8))
|
||||||
end
|
assert_equal(4, @cpu_counter.count_with_default)
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_ruby_in_path
|
class TestClassMethod < Rake::TestCase
|
||||||
ruby = File.basename Gem.ruby
|
def setup
|
||||||
ruby_dir = File.dirname Gem.ruby
|
super
|
||||||
|
|
||||||
begin
|
@klass = Class.new(Rake::CpuCounter)
|
||||||
orig_path, ENV['PATH'] =
|
end
|
||||||
ENV['PATH'], [ruby_dir, *ENV['PATH']].join(File::PATH_SEPARATOR)
|
|
||||||
|
|
||||||
yield ruby
|
def test_count
|
||||||
ensure
|
@klass.class_eval do
|
||||||
ENV['PATH'] = orig_path
|
def count; 8; end
|
||||||
|
end
|
||||||
|
assert_equal(8, @klass.count)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_count_nil
|
||||||
|
counted = false
|
||||||
|
@klass.class_eval do
|
||||||
|
define_method(:count) do
|
||||||
|
counted = true
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(4, @klass.count)
|
||||||
|
assert_equal(true, counted)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_count_raise
|
||||||
|
counted = false
|
||||||
|
@klass.class_eval do
|
||||||
|
define_method(:count) do
|
||||||
|
counted = true
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(4, @klass.count)
|
||||||
|
assert_equal(true, counted)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue