Skip to content

Commit 4ede817

Browse files
committed
Extract benchmark_env since it doesn't change inside the loop
1 parent 027d36c commit 4ede817

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

lib/benchmark_suite.rb

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,8 @@ def run
7777
script_path,
7878
].compact
7979

80-
# When the Ruby running this script is not the first Ruby in PATH, shell commands
81-
# like `bundle install` in a child process will not use the Ruby being benchmarked.
82-
# It overrides PATH to guarantee the commands of the benchmarked Ruby will be used.
83-
env = {}
84-
ruby_path = `#{ruby.shelljoin} -e 'print RbConfig.ruby' 2> #{File::NULL}`
85-
if ruby_path != RbConfig.ruby
86-
env["PATH"] = "#{File.dirname(ruby_path)}:#{ENV["PATH"]}"
87-
88-
# chruby sets GEM_HOME and GEM_PATH in your shell. We have to unset it in the child
89-
# process to avoid installing gems to the version that is running run_benchmarks.rb.
90-
["GEM_HOME", "GEM_PATH"].each do |var|
91-
env[var] = nil if ENV.key?(var)
92-
end
93-
end
94-
9580
# Do the benchmarking
96-
result = BenchmarkRunner.check_call(cmd.shelljoin, env: env, raise_error: false)
81+
result = BenchmarkRunner.check_call(cmd.shelljoin, env: benchmark_env, raise_error: false)
9782

9883
if result[:success]
9984
bench_data[bench_name] = JSON.parse(File.read(result_json_path)).tap do |json|
@@ -112,6 +97,28 @@ def run
11297

11398
private
11499

100+
def benchmark_env
101+
@benchmark_env ||= begin
102+
# When the Ruby running this script is not the first Ruby in PATH, shell commands
103+
# like `bundle install` in a child process will not use the Ruby being benchmarked.
104+
# It overrides PATH to guarantee the commands of the benchmarked Ruby will be used.
105+
env = {}
106+
ruby_path = `#{ruby.shelljoin} -e 'print RbConfig.ruby' 2> #{File::NULL}`
107+
108+
if ruby_path != RbConfig.ruby
109+
env["PATH"] = "#{File.dirname(ruby_path)}:#{ENV["PATH"]}"
110+
111+
# chruby sets GEM_HOME and GEM_PATH in your shell. We have to unset it in the child
112+
# process to avoid installing gems to the version that is running run_benchmarks.rb.
113+
["GEM_HOME", "GEM_PATH"].each do |var|
114+
env[var] = nil if ENV.key?(var)
115+
end
116+
end
117+
118+
env
119+
end
120+
end
121+
115122
def bench_file_grouping
116123
bench_file_grouping = {}
117124

0 commit comments

Comments
 (0)