Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Benchmark require relative and update to new rspec-support method #1348

Merged
merged 7 commits into from
Feb 25, 2014
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
123 changes: 123 additions & 0 deletions benchmarks/boot_time_with_many_load_path_dirs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
ruby -v

function run_benchmark_with_load_path_size {
pushd tmp
mkdir -p boot_time_benchmark

local load_path_size=$1
for (( i=0; i < $load_path_size; i++ )); do
mkdir -p "boot_time_benchmark/dir_$i"
done

local load_path=`ruby -e 'puts Array.new(Integer(ARGV.first)) { |i| "boot_time_benchmark/dir_#{i}" }.join(":")' $load_path_size`

echo "3 runs with $load_path_size dirs on load path, booting 50 times, using $2"
for i in {1..3}; do
time (for i in {1..50}; do ruby -I$load_path:../lib:../../rspec-support/lib -e 'require "rspec/core"'; done)
done
popd
}

run_benchmark_with_load_path_size 10 "require"
run_benchmark_with_load_path_size 100 "require"
run_benchmark_with_load_path_size 1000 "require"

export REQUIRE_RELATIVE=1

run_benchmark_with_load_path_size 10 "require_relative"
run_benchmark_with_load_path_size 100 "require_relative"
run_benchmark_with_load_path_size 1000 "require_relative"

: <<'result_comment'
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
~/code/rspec-core/tmp ~/code/rspec-core
3 runs with 10 dirs on load path, booting 50 times, using require

real 0m3.815s
user 0m3.205s
sys 0m0.519s

real 0m3.850s
user 0m3.234s
sys 0m0.527s

real 0m3.840s
user 0m3.225s
sys 0m0.525s
~/code/rspec-core
~/code/rspec-core/tmp ~/code/rspec-core
3 runs with 100 dirs on load path, booting 50 times, using require

real 0m5.086s
user 0m3.887s
sys 0m1.107s

real 0m5.063s
user 0m3.870s
sys 0m1.098s

real 0m5.061s
user 0m3.871s
sys 0m1.097s
~/code/rspec-core
~/code/rspec-core/tmp ~/code/rspec-core
3 runs with 1000 dirs on load path, booting 50 times, using require

real 0m18.850s
user 0m11.057s
sys 0m7.679s

real 0m18.783s
user 0m11.012s
sys 0m7.657s

real 0m18.747s
user 0m10.992s
sys 0m7.639s
~/code/rspec-core
~/code/rspec-core/tmp ~/code/rspec-core
3 runs with 10 dirs on load path, booting 50 times, using require_relative

real 0m3.794s
user 0m3.200s
sys 0m0.506s

real 0m3.769s
user 0m3.180s
sys 0m0.502s

real 0m3.787s
user 0m3.192s
sys 0m0.502s
~/code/rspec-core
~/code/rspec-core/tmp ~/code/rspec-core
3 runs with 100 dirs on load path, booting 50 times, using require_relative

real 0m4.626s
user 0m3.620s
sys 0m0.910s

real 0m4.652s
user 0m3.642s
sys 0m0.915s

real 0m4.678s
user 0m3.662s
sys 0m0.924s
~/code/rspec-core
~/code/rspec-core/tmp ~/code/rspec-core
3 runs with 1000 dirs on load path, booting 50 times, using require_relative

real 0m14.400s
user 0m8.615s
sys 0m5.675s

real 0m14.495s
user 0m8.672s
sys 0m5.711s

real 0m14.541s
user 0m8.705s
sys 0m5.727s
~/code/rspec-core
result_comment
24 changes: 24 additions & 0 deletions benchmarks/require_relative_v_require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@
# virtually no penalty
#
# ###################################

__END__

Ruby 2.0:

➜ rspec-core git:(benchmark-require-relative) REQUIRE_RELATIVE=1 bundle exec ruby benchmarks/require_relative_v_require.rb
0.000000 0.030000 1.470000 ( 1.481949)
0.000000 0.020000 1.440000 ( 1.462620)
0.000000 0.020000 1.470000 ( 1.491825)
➜ rspec-core git:(benchmark-require-relative) bundle exec ruby benchmarks/require_relative_v_require.rb
0.000000 0.010000 1.510000 ( 1.549906)
0.000000 0.010000 1.530000 ( 1.546252)
0.000000 0.020000 1.510000 ( 1.531644)

Ruby 2.1:

➜ rspec-core git:(benchmark-require-relative) bundle exec ruby benchmarks/require_relative_v_require.rb
0.000000 0.020000 1.570000 ( 1.613217)
0.000000 0.020000 1.600000 ( 1.618540)
0.010000 0.020000 1.570000 ( 1.608205)
➜ rspec-core git:(benchmark-require-relative) REQUIRE_RELATIVE=1 bundle exec ruby benchmarks/require_relative_v_require.rb
0.000000 0.020000 1.480000 ( 1.515131)
0.000000 0.010000 1.480000 ( 1.527766)
0.000000 0.020000 1.490000 ( 1.515631)
72 changes: 33 additions & 39 deletions lib/rspec/core.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
require_rspec = if defined?(require_relative)
lambda do |path|
require_relative path
end
else # for 1.8.7
lambda do |path|
require "rspec/#{path}"
end
end

require 'set'
require 'time'
require 'rbconfig'

require_rspec['core/version']

require 'rspec/support/caller_filter'
require 'rspec/core/warnings'

require_rspec['core/flat_map']
require_rspec['core/filter_manager']
require_rspec['core/dsl']
require_rspec['core/notifications']
require_rspec['core/reporter']

require_rspec['core/hooks']
require_rspec['core/memoized_helpers']
require_rspec['core/metadata']
require_rspec['core/pending']
require_rspec['core/formatters']
require_rspec['core/ordering']

require_rspec['core/world']
require_rspec['core/configuration']
require_rspec['core/option_parser']
require_rspec['core/configuration_options']
require_rspec['core/command_line']
require_rspec['core/runner']
require_rspec['core/example']
require_rspec['core/shared_example_group/collection']
require_rspec['core/shared_example_group']
require_rspec['core/example_group']
require "rspec/support"
RSpec::Support.require_rspec_support "caller_filter"

RSpec::Support.define_optimized_require_for_rspec(:core) { |f| require_relative f }

%w[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sorta miss the line-per-require format - it allowed for groupings and it was easier to read even though it was repetitive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, think I agree...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially had that, but we had more duplicated on each line (the RSpec::Support.require_rspec_core bit) than was different on each line (the file name). So I collapsed it.

What do you think about keeping the array.each but putting each file on its own line (with optional blank lines in there for grouping)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fair to me.

The require method could also take varargs so that RSpec::Support.require_rspec_core is up-front instead of trailing the array… but I don't have strong feelings about that TBH.

version
warnings

flat_map
filter_manager
dsl
notifications
reporter

hooks
memoized_helpers
metadata
pending
formatters
ordering

world
configuration
option_parser
configuration_options
command_line
runner
example
shared_example_group/collection
shared_example_group
example_group
].each { |name| RSpec::Support.require_rspec_core name }

module RSpec
autoload :SharedContext, 'rspec/core/shared_context'
Expand Down
9 changes: 5 additions & 4 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'fileutils'
require 'rspec/core/backtrace_formatter'
require 'rspec/core/ruby_project'
require 'rspec/core/formatters/deprecation_formatter'

RSpec::Support.require_rspec_core "backtrace_formatter"
RSpec::Support.require_rspec_core "ruby_project"
RSpec::Support.require_rspec_core "formatters/deprecation_formatter"

module RSpec
module Core
Expand Down Expand Up @@ -422,7 +423,7 @@ def mock_with(framework)
"Pass a module or one of #{MOCKING_ADAPTERS.keys.inspect}"
end

require "rspec/core/mocking_adapters/#{const_name.to_s.downcase}"
RSpec::Support.require_rspec_core "mocking_adapters/#{const_name.to_s.downcase}"
RSpec::Core::MockingAdapters.const_get(const_name)
end

Expand Down
1 change: 1 addition & 0 deletions lib/rspec/core/configuration_options.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'erb'
require 'shellwords'
require 'set'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An aside, are we not worried about polluting stdlib here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a new require; see 322d272.

We prefer not to pollute stdlib, but it's not an absolute rule.


module RSpec
module Core
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/drb_command_line.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'drb/drb'
require 'rspec/core/drb_options'
RSpec::Support.require_rspec_core "drb_options"

module RSpec
module Core
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/legacy_formatter'
RSpec::Support.require_rspec_core 'formatters/legacy_formatter'

# ## Built-in Formatters
#
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/base_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/helpers'
RSpec::Support.require_rspec_core "formatters/helpers"
require 'stringio'

module RSpec
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/base_text_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/base_formatter'
RSpec::Support.require_rspec_core "formatters/base_formatter"
require 'set'

module RSpec
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/deprecation_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/helpers'
RSpec::Support.require_rspec_core "formatters/helpers"
require 'set'

module RSpec
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/documentation_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/base_text_formatter'
RSpec::Support.require_rspec_core "formatters/base_text_formatter"

module RSpec
module Core
Expand Down
6 changes: 3 additions & 3 deletions lib/rspec/core/formatters/html_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rspec/core/formatters/base_text_formatter'
require 'rspec/core/formatters/html_printer'
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
RSpec::Support.require_rspec_core "formatters/html_printer"

module RSpec
module Core
Expand Down Expand Up @@ -134,7 +134,7 @@ def percent_done
# could output links to images or other files produced during the specs.
#
def extra_failure_content(exception)
require 'rspec/core/formatters/snippet_extractor'
RSpec::Support.require_rspec_core "formatters/snippet_extractor"
backtrace = exception.backtrace.map {|line| configuration.backtrace_formatter.backtrace_line(line)}
backtrace.compact!
@snippet_extractor ||= SnippetExtractor.new
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/json_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/base_formatter'
RSpec::Support.require_rspec_core "formatters/base_formatter"
require 'json'

module RSpec
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/formatters/legacy_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/helpers'
RSpec::Support.require_rspec_core "formatters/helpers"
require 'stringio'

module RSpec
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/core/formatters/progress_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rspec/core/formatters/base_text_formatter'
RSpec::Support.require_rspec_core "formatters/base_text_formatter"

module RSpec
module Core
module Formatters
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/notifications.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rspec/core/formatters/helpers'
RSpec::Support.require_rspec_core "formatters/helpers"

module RSpec::Core
module Notifications
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def parser(options)
end

parser.on('--init', 'Initialize your project with RSpec.') do |cmd|
require 'rspec/core/project_initializer'
RSpec::Support.require_rspec_core "project_initializer"
ProjectInitializer.new.run
exit
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Core
if defined?(::Random)
RandomNumberGenerator = ::Random
else
require 'rspec/core/backport_random'
RSpec::Support.require_rspec_core "backport_random"
RandomNumberGenerator = RSpec::Core::Backports::Random
end

Expand Down
4 changes: 3 additions & 1 deletion lib/rspec/core/rake_task.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'rspec/support/warnings'
require 'rspec/support'
RSpec::Support.require_rspec_support "warnings"

require 'rake'
require 'rake/tasklib'
require 'shellwords'
Expand Down
Loading