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

Commit 1e5ea31

Browse files
committed
Merge pull request #1348 from rspec/benchmark-require-relative
Benchmark require relative and update to new rspec-support method
2 parents 347ba21 + 6c4ecea commit 1e5ea31

20 files changed

+216
-64
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
ruby -v
2+
3+
function run_benchmark_with_load_path_size {
4+
pushd tmp
5+
mkdir -p boot_time_benchmark
6+
7+
local load_path_size=$1
8+
for (( i=0; i < $load_path_size; i++ )); do
9+
mkdir -p "boot_time_benchmark/dir_$i"
10+
done
11+
12+
local load_path=`ruby -e 'puts Array.new(Integer(ARGV.first)) { |i| "boot_time_benchmark/dir_#{i}" }.join(":")' $load_path_size`
13+
14+
echo "3 runs with $load_path_size dirs on load path, booting 50 times, using $2"
15+
for i in {1..3}; do
16+
time (for i in {1..50}; do ruby -I$load_path:../lib:../../rspec-support/lib -e 'require "rspec/core"'; done)
17+
done
18+
popd
19+
}
20+
21+
run_benchmark_with_load_path_size 10 "require"
22+
run_benchmark_with_load_path_size 100 "require"
23+
run_benchmark_with_load_path_size 1000 "require"
24+
25+
export REQUIRE_RELATIVE=1
26+
27+
run_benchmark_with_load_path_size 10 "require_relative"
28+
run_benchmark_with_load_path_size 100 "require_relative"
29+
run_benchmark_with_load_path_size 1000 "require_relative"
30+
31+
: <<'result_comment'
32+
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
33+
~/code/rspec-core/tmp ~/code/rspec-core
34+
3 runs with 10 dirs on load path, booting 50 times, using require
35+
36+
real 0m3.815s
37+
user 0m3.205s
38+
sys 0m0.519s
39+
40+
real 0m3.850s
41+
user 0m3.234s
42+
sys 0m0.527s
43+
44+
real 0m3.840s
45+
user 0m3.225s
46+
sys 0m0.525s
47+
~/code/rspec-core
48+
~/code/rspec-core/tmp ~/code/rspec-core
49+
3 runs with 100 dirs on load path, booting 50 times, using require
50+
51+
real 0m5.086s
52+
user 0m3.887s
53+
sys 0m1.107s
54+
55+
real 0m5.063s
56+
user 0m3.870s
57+
sys 0m1.098s
58+
59+
real 0m5.061s
60+
user 0m3.871s
61+
sys 0m1.097s
62+
~/code/rspec-core
63+
~/code/rspec-core/tmp ~/code/rspec-core
64+
3 runs with 1000 dirs on load path, booting 50 times, using require
65+
66+
real 0m18.850s
67+
user 0m11.057s
68+
sys 0m7.679s
69+
70+
real 0m18.783s
71+
user 0m11.012s
72+
sys 0m7.657s
73+
74+
real 0m18.747s
75+
user 0m10.992s
76+
sys 0m7.639s
77+
~/code/rspec-core
78+
~/code/rspec-core/tmp ~/code/rspec-core
79+
3 runs with 10 dirs on load path, booting 50 times, using require_relative
80+
81+
real 0m3.794s
82+
user 0m3.200s
83+
sys 0m0.506s
84+
85+
real 0m3.769s
86+
user 0m3.180s
87+
sys 0m0.502s
88+
89+
real 0m3.787s
90+
user 0m3.192s
91+
sys 0m0.502s
92+
~/code/rspec-core
93+
~/code/rspec-core/tmp ~/code/rspec-core
94+
3 runs with 100 dirs on load path, booting 50 times, using require_relative
95+
96+
real 0m4.626s
97+
user 0m3.620s
98+
sys 0m0.910s
99+
100+
real 0m4.652s
101+
user 0m3.642s
102+
sys 0m0.915s
103+
104+
real 0m4.678s
105+
user 0m3.662s
106+
sys 0m0.924s
107+
~/code/rspec-core
108+
~/code/rspec-core/tmp ~/code/rspec-core
109+
3 runs with 1000 dirs on load path, booting 50 times, using require_relative
110+
111+
real 0m14.400s
112+
user 0m8.615s
113+
sys 0m5.675s
114+
115+
real 0m14.495s
116+
user 0m8.672s
117+
sys 0m5.711s
118+
119+
real 0m14.541s
120+
user 0m8.705s
121+
sys 0m5.727s
122+
~/code/rspec-core
123+
result_comment

benchmarks/require_relative_v_require.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,27 @@
4949
# virtually no penalty
5050
#
5151
# ###################################
52+
53+
__END__
54+
55+
Ruby 2.0:
56+
57+
➜ rspec-core git:(benchmark-require-relative) REQUIRE_RELATIVE=1 bundle exec ruby benchmarks/require_relative_v_require.rb
58+
0.000000 0.030000 1.470000 ( 1.481949)
59+
0.000000 0.020000 1.440000 ( 1.462620)
60+
0.000000 0.020000 1.470000 ( 1.491825)
61+
➜ rspec-core git:(benchmark-require-relative) bundle exec ruby benchmarks/require_relative_v_require.rb
62+
0.000000 0.010000 1.510000 ( 1.549906)
63+
0.000000 0.010000 1.530000 ( 1.546252)
64+
0.000000 0.020000 1.510000 ( 1.531644)
65+
66+
Ruby 2.1:
67+
68+
➜ rspec-core git:(benchmark-require-relative) bundle exec ruby benchmarks/require_relative_v_require.rb
69+
0.000000 0.020000 1.570000 ( 1.613217)
70+
0.000000 0.020000 1.600000 ( 1.618540)
71+
0.010000 0.020000 1.570000 ( 1.608205)
72+
➜ rspec-core git:(benchmark-require-relative) REQUIRE_RELATIVE=1 bundle exec ruby benchmarks/require_relative_v_require.rb
73+
0.000000 0.020000 1.480000 ( 1.515131)
74+
0.000000 0.010000 1.480000 ( 1.527766)
75+
0.000000 0.020000 1.490000 ( 1.515631)

lib/rspec/core.rb

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
1-
require_rspec = if defined?(require_relative)
2-
lambda do |path|
3-
require_relative path
4-
end
5-
else # for 1.8.7
6-
lambda do |path|
7-
require "rspec/#{path}"
8-
end
9-
end
10-
11-
require 'set'
121
require 'time'
132
require 'rbconfig'
143

15-
require_rspec['core/version']
16-
17-
require 'rspec/support/caller_filter'
18-
require 'rspec/core/warnings'
19-
20-
require_rspec['core/flat_map']
21-
require_rspec['core/filter_manager']
22-
require_rspec['core/dsl']
23-
require_rspec['core/notifications']
24-
require_rspec['core/reporter']
25-
26-
require_rspec['core/hooks']
27-
require_rspec['core/memoized_helpers']
28-
require_rspec['core/metadata']
29-
require_rspec['core/pending']
30-
require_rspec['core/formatters']
31-
require_rspec['core/ordering']
32-
33-
require_rspec['core/world']
34-
require_rspec['core/configuration']
35-
require_rspec['core/option_parser']
36-
require_rspec['core/configuration_options']
37-
require_rspec['core/command_line']
38-
require_rspec['core/runner']
39-
require_rspec['core/example']
40-
require_rspec['core/shared_example_group/collection']
41-
require_rspec['core/shared_example_group']
42-
require_rspec['core/example_group']
4+
require "rspec/support"
5+
RSpec::Support.require_rspec_support "caller_filter"
6+
7+
RSpec::Support.define_optimized_require_for_rspec(:core) { |f| require_relative f }
8+
9+
%w[
10+
version
11+
warnings
12+
13+
flat_map
14+
filter_manager
15+
dsl
16+
notifications
17+
reporter
18+
19+
hooks
20+
memoized_helpers
21+
metadata
22+
pending
23+
formatters
24+
ordering
25+
26+
world
27+
configuration
28+
option_parser
29+
configuration_options
30+
command_line
31+
runner
32+
example
33+
shared_example_group/collection
34+
shared_example_group
35+
example_group
36+
].each { |name| RSpec::Support.require_rspec_core name }
4337

4438
module RSpec
4539
autoload :SharedContext, 'rspec/core/shared_context'

lib/rspec/core/configuration.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'fileutils'
2-
require 'rspec/core/backtrace_formatter'
3-
require 'rspec/core/ruby_project'
4-
require 'rspec/core/formatters/deprecation_formatter'
2+
3+
RSpec::Support.require_rspec_core "backtrace_formatter"
4+
RSpec::Support.require_rspec_core "ruby_project"
5+
RSpec::Support.require_rspec_core "formatters/deprecation_formatter"
56

67
module RSpec
78
module Core
@@ -422,7 +423,7 @@ def mock_with(framework)
422423
"Pass a module or one of #{MOCKING_ADAPTERS.keys.inspect}"
423424
end
424425

425-
require "rspec/core/mocking_adapters/#{const_name.to_s.downcase}"
426+
RSpec::Support.require_rspec_core "mocking_adapters/#{const_name.to_s.downcase}"
426427
RSpec::Core::MockingAdapters.const_get(const_name)
427428
end
428429

lib/rspec/core/configuration_options.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'erb'
22
require 'shellwords'
3+
require 'set'
34

45
module RSpec
56
module Core

lib/rspec/core/drb_command_line.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'drb/drb'
2-
require 'rspec/core/drb_options'
2+
RSpec::Support.require_rspec_core "drb_options"
33

44
module RSpec
55
module Core

lib/rspec/core/formatters.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/legacy_formatter'
1+
RSpec::Support.require_rspec_core 'formatters/legacy_formatter'
22

33
# ## Built-in Formatters
44
#

lib/rspec/core/formatters/base_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/helpers'
1+
RSpec::Support.require_rspec_core "formatters/helpers"
22
require 'stringio'
33

44
module RSpec

lib/rspec/core/formatters/base_text_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/base_formatter'
1+
RSpec::Support.require_rspec_core "formatters/base_formatter"
22
require 'set'
33

44
module RSpec

lib/rspec/core/formatters/deprecation_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/helpers'
1+
RSpec::Support.require_rspec_core "formatters/helpers"
22
require 'set'
33

44
module RSpec

lib/rspec/core/formatters/documentation_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/base_text_formatter'
1+
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
22

33
module RSpec
44
module Core

lib/rspec/core/formatters/html_formatter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require 'rspec/core/formatters/base_text_formatter'
2-
require 'rspec/core/formatters/html_printer'
1+
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
2+
RSpec::Support.require_rspec_core "formatters/html_printer"
33

44
module RSpec
55
module Core
@@ -134,7 +134,7 @@ def percent_done
134134
# could output links to images or other files produced during the specs.
135135
#
136136
def extra_failure_content(exception)
137-
require 'rspec/core/formatters/snippet_extractor'
137+
RSpec::Support.require_rspec_core "formatters/snippet_extractor"
138138
backtrace = exception.backtrace.map {|line| configuration.backtrace_formatter.backtrace_line(line)}
139139
backtrace.compact!
140140
@snippet_extractor ||= SnippetExtractor.new

lib/rspec/core/formatters/json_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/base_formatter'
1+
RSpec::Support.require_rspec_core "formatters/base_formatter"
22
require 'json'
33

44
module RSpec

lib/rspec/core/formatters/legacy_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/helpers'
1+
RSpec::Support.require_rspec_core "formatters/helpers"
22
require 'stringio'
33

44
module RSpec

lib/rspec/core/formatters/progress_formatter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'rspec/core/formatters/base_text_formatter'
1+
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
2+
23
module RSpec
34
module Core
45
module Formatters

lib/rspec/core/notifications.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'rspec/core/formatters/helpers'
1+
RSpec::Support.require_rspec_core "formatters/helpers"
22

33
module RSpec::Core
44
module Notifications

lib/rspec/core/option_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def parser(options)
9292
end
9393

9494
parser.on('--init', 'Initialize your project with RSpec.') do |cmd|
95-
require 'rspec/core/project_initializer'
95+
RSpec::Support.require_rspec_core "project_initializer"
9696
ProjectInitializer.new.run
9797
exit
9898
end

lib/rspec/core/ordering.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Core
33
if defined?(::Random)
44
RandomNumberGenerator = ::Random
55
else
6-
require 'rspec/core/backport_random'
6+
RSpec::Support.require_rspec_core "backport_random"
77
RandomNumberGenerator = RSpec::Core::Backports::Random
88
end
99

lib/rspec/core/rake_task.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require 'rspec/support/warnings'
1+
require 'rspec/support'
2+
RSpec::Support.require_rspec_support "warnings"
3+
24
require 'rake'
35
require 'rake/tasklib'
46
require 'shellwords'

0 commit comments

Comments
 (0)