Class: RubybenchRunner::BaseRunner
- Inherits:
-
Object
- Object
- RubybenchRunner::BaseRunner
- Defined in:
- lib/rubybench_runner/base_runner.rb
Direct Known Subclasses
Constant Summary collapse
- HELPERS_VERSION_FILE =
"helpers_version"- COPY_FOLDERS =
%w{ support rails }
- DEFAULT_OPTS =
{ repeat_count: 1, quiet: false, fresh_run: false, skip_dependencies_check: false, cleanup: false, wps: false, round: 2 }
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#repo_path ⇒ Object
readonly
Returns the value of attribute repo_path.
-
#script_url ⇒ Object
readonly
Returns the value of attribute script_url.
Instance Method Summary collapse
- #benchmark_name ⇒ Object
- #bundle_install ⇒ Object
- #check_dependencies ⇒ Object
- #command ⇒ Object
- #copy_helpers(dest) ⇒ Object
- #create_tmpdir ⇒ Object
- #dest_dir ⇒ Object
- #download_script ⇒ Object
- #gemfile_content ⇒ Object
-
#initialize(url, repo_path, opts = {}) ⇒ BaseRunner
constructor
A new instance of BaseRunner.
- #print_results ⇒ Object
- #process_results ⇒ Object
- #require_db? ⇒ Boolean
- #run ⇒ Object
- #run_benchmarks ⇒ Object
- #save_dir ⇒ Object
- #script_full_path ⇒ Object
- #script_name ⇒ Object
- #script_path ⇒ Object
- #setup_db ⇒ Object
- #write_gemfile ⇒ Object
Constructor Details
#initialize(url, repo_path, opts = {}) ⇒ BaseRunner
Returns a new instance of BaseRunner.
30 31 32 33 34 35 36 37 |
# File 'lib/rubybench_runner/base_runner.rb', line 30 def initialize(url, repo_path, opts = {}) @script_url = normalize_url(url) @repo_path = repo_path @opts = OpenStruct.new(DEFAULT_OPTS.merge(opts)) @results = [] @error = nil @output = "" end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
18 19 20 |
# File 'lib/rubybench_runner/base_runner.rb', line 18 def opts @opts end |
#repo_path ⇒ Object (readonly)
Returns the value of attribute repo_path.
18 19 20 |
# File 'lib/rubybench_runner/base_runner.rb', line 18 def repo_path @repo_path end |
#script_url ⇒ Object (readonly)
Returns the value of attribute script_url.
18 19 20 |
# File 'lib/rubybench_runner/base_runner.rb', line 18 def script_url @script_url end |
Instance Method Details
#benchmark_name ⇒ Object
106 107 108 |
# File 'lib/rubybench_runner/base_runner.rb', line 106 def benchmark_name raise "Override the `benchmark_name` method in your class" end |
#bundle_install ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rubybench_runner/base_runner.rb', line 180 def bundle_install return if without_bundle? log("Installing gems...") comm = "bundle install" comm += " > /dev/null 2>&1" if !opts.verbose Dir.chdir(File.dirname(gemfile_location)) do system(comm) end Dir.chdir(File.join(dest_dir, "support", "setup")) do system(comm) end end |
#check_dependencies ⇒ Object
114 115 116 117 118 |
# File 'lib/rubybench_runner/base_runner.rb', line 114 def check_dependencies return if opts.skip_dependencies_check log("Checking dependencies...") RubybenchRunner::DependenciesChecker.check end |
#command ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/rubybench_runner/base_runner.rb', line 98 def command if without_bundle? "ruby #{script_name}" else "BUNDLE_GEMFILE=#{gemfile_location} bundle exec ruby #{script_name}" end end |
#copy_helpers(dest) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubybench_runner/base_runner.rb', line 64 def copy_helpers(dest) gem_helpers_version_path = File.join(__dir__, HELPERS_VERSION_FILE) current_helpers_version_path = File.join(dest, HELPERS_VERSION_FILE) if !File.exists?(current_helpers_version_path) || File.read(gem_helpers_version_path) != File.read(current_helpers_version_path) FileUtils.cp(gem_helpers_version_path, current_helpers_version_path) COPY_FOLDERS.each do |folder| origin = File.join(__dir__, folder) destination = File.join(dest, folder) FileUtils.cp_r(origin, destination) end end end |
#create_tmpdir ⇒ Object
59 60 61 62 |
# File 'lib/rubybench_runner/base_runner.rb', line 59 def create_tmpdir FileUtils.mkdir_p(dest_dir) copy_helpers(dest_dir) end |
#dest_dir ⇒ Object
54 55 56 57 |
# File 'lib/rubybench_runner/base_runner.rb', line 54 def dest_dir # directory where all helpers and gems will be installed/copied to @dest_dir ||= File.join(Dir.tmpdir, "rubybench_runner_tmp") end |
#download_script ⇒ Object
194 195 196 197 198 |
# File 'lib/rubybench_runner/base_runner.rb', line 194 def download_script log("Downloading script...") content = open(script_url).read File.write(script_full_path, content) end |
#gemfile_content ⇒ Object
82 83 84 |
# File 'lib/rubybench_runner/base_runner.rb', line 82 def gemfile_content nil end |
#print_results ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rubybench_runner/base_runner.rb', line 152 def print_results if @error @output = <<~OUTPUT An error occurred while running the benchmarks: Error: #{@error.} Backtrace: #{@error.backtrace.join("\n")} ------ Raw results until this error: #{@results} OUTPUT else label = @results.first['label'] version = @results.first['version'] @output = "#{benchmark_name} version #{version}\n" @output += "Benchmark name: #{label}\n" @output += "Results (#{@results.size} runs):\n" @results.map!.with_index do |res, ind| res.delete('label') res.delete('version') { "run #{ind + 1}" => res } end @output += "\n" @output += @results.to_yaml.sub("---\n", "") end puts @output end |
#process_results ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rubybench_runner/base_runner.rb', line 136 def process_results return if @error @results.map! do |res| res.each do |key, val| if Float === val res[key] = val.round(opts.round) end end res end @results.sort_by! do |res| res['iterations_per_second'] end end |
#require_db? ⇒ Boolean
110 111 112 |
# File 'lib/rubybench_runner/base_runner.rb', line 110 def require_db? false end |
#run ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rubybench_runner/base_runner.rb', line 39 def run create_tmpdir cleanup(before: true) check_dependencies download_script write_gemfile bundle_install setup_db run_benchmarks process_results print_results ensure cleanup(after: true) end |
#run_benchmarks ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rubybench_runner/base_runner.rb', line 123 def run_benchmarks log("Running benchmarks...") Dir.chdir(script_path) do opts.repeat_count.times do |n| res = `#{command}` @results[n] = res @results[n] = JSON.parse(res) end end rescue => err @error = err end |
#save_dir ⇒ Object
78 79 80 |
# File 'lib/rubybench_runner/base_runner.rb', line 78 def save_dir raise "Override the `save_dir` method in your subclass #{self.class.name}" end |
#script_full_path ⇒ Object
90 91 92 |
# File 'lib/rubybench_runner/base_runner.rb', line 90 def script_full_path @script_full_path ||= File.join(script_path, script_name) end |
#script_name ⇒ Object
94 95 96 |
# File 'lib/rubybench_runner/base_runner.rb', line 94 def script_name "script.rb" end |
#script_path ⇒ Object
86 87 88 |
# File 'lib/rubybench_runner/base_runner.rb', line 86 def script_path @script_path ||= File.join(save_dir, "benchmarks") end |
#setup_db ⇒ Object
120 121 |
# File 'lib/rubybench_runner/base_runner.rb', line 120 def setup_db end |
#write_gemfile ⇒ Object
200 201 202 203 |
# File 'lib/rubybench_runner/base_runner.rb', line 200 def write_gemfile return if without_bundle? File.write(gemfile_location, gemfile_content) end |