Class: RubybenchRunner::BaseRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubybench_runner/base_runner.rb

Direct Known Subclasses

RailsRunner

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

Instance Method Summary collapse

Constructor Details

#initialize(script, opts = {}) ⇒ BaseRunner

Returns a new instance of BaseRunner.



33
34
35
36
37
38
39
40
# File 'lib/rubybench_runner/base_runner.rb', line 33

def initialize(script, opts = {})
  @script_name = script
  @opts = OpenStruct.new(DEFAULT_OPTS.merge(opts))
  @script_url = @opts.url || script_full_url(script)
  @results = []
  @error = nil
  @output = ""
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



21
22
23
# File 'lib/rubybench_runner/base_runner.rb', line 21

def opts
  @opts
end

#repo_pathObject (readonly)

Returns the value of attribute repo_path.



21
22
23
# File 'lib/rubybench_runner/base_runner.rb', line 21

def repo_path
  @repo_path
end

#script_urlObject (readonly)

Returns the value of attribute script_url.



21
22
23
# File 'lib/rubybench_runner/base_runner.rb', line 21

def script_url
  @script_url
end

Instance Method Details

#benchmark_nameObject



137
138
139
# File 'lib/rubybench_runner/base_runner.rb', line 137

def benchmark_name
  raise "Override the `benchmark_name` method in your class"
end

#bundle_installObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rubybench_runner/base_runner.rb', line 259

def bundle_install
  return if without_bundle?

  log("Installing gems...")
  comm = "bundle install"
  if opts.db == "mysql2"
    comm += " --without postgres"
  elsif opts.db == "postgres"
    comm += " --without mysql"
  else
    comm += " --without mysql postgres"
  end
  comm += " > /dev/null 2>&1" if !opts.verbose
  Dir.chdir(File.dirname(gemfile_location)) do
    system(comm)
  end

  if require_db?
    Dir.chdir(File.join(dest_dir, "support", "setup")) do
      system(comm)
    end
  end
end

#check_dependenciesObject



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rubybench_runner/base_runner.rb', line 145

def check_dependencies
  return if opts.skip_dependencies_check || !require_db?

  if opts.db == "postgres"
    require_gem 'pg'
  elsif opts.db == "mysql2"
    require_gem 'mysql2'
  end

  log("Checking dependencies...")
  RubybenchRunner::DependenciesChecker.check(pg: opts.db == "postgres", mysql: opts.db == "mysql2")
end

#commandObject



129
130
131
132
133
134
135
# File 'lib/rubybench_runner/base_runner.rb', line 129

def command
  if without_bundle?
    "ruby #{script_name}"
  else
    "BUNDLE_GEMFILE=#{gemfile_location} bundle exec ruby #{script_name}"
  end
end

#copy_helpers(dest) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubybench_runner/base_runner.rb', line 95

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_tmpdirObject



90
91
92
93
# File 'lib/rubybench_runner/base_runner.rb', line 90

def create_tmpdir
  FileUtils.mkdir_p(dest_dir)
  copy_helpers(dest_dir)
end

#dest_dirObject



85
86
87
88
# File 'lib/rubybench_runner/base_runner.rb', line 85

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_scriptObject



283
284
285
286
287
288
289
290
# File 'lib/rubybench_runner/base_runner.rb', line 283

def download_script
  log("Downloading script...")
  content = open(script_url).read
  File.write(script_full_path, content)
rescue OpenURI::HTTPError => e
  log("Script download failed #{script_url}", f: true)
  @error = e
end

#gemfile_contentObject



113
114
115
# File 'lib/rubybench_runner/base_runner.rb', line 113

def gemfile_content
  nil
end

#is_repo_path_valid?Boolean

Returns:



62
63
64
# File 'lib/rubybench_runner/base_runner.rb', line 62

def is_repo_path_valid?
  true
end

#possible_repo_pathObject



58
59
60
# File 'lib/rubybench_runner/base_runner.rb', line 58

def possible_repo_path
  File.join(Dir.home, benchmark_name.downcase)
end


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rubybench_runner/base_runner.rb', line 228

def print_results
  if @error
    @output = "      An error occurred while running the benchmarks:\n      Error \#{@error.class}: \#{@error.message}\n      Backtrace:\n      \#{@error.backtrace.join(\"\\n\")}\n    OUTPUT\n    if @results.size > 0\n      @output += <<~OUTPUT\n        ------\n        Raw results until this error:\n        \#{@results}\n      OUTPUT\n    end\n  else\n    label = @results.first['label']\n    version = @results.first['version']\n    @output = \"\#{benchmark_name} version \#{version}\\n\"\n    @output += \"Results (\#{@results.size} runs):\\n\"\n    @results.map!.with_index do |res, ind|\n      res.delete('label')\n      res.delete('version')\n      { \"run \#{ind + 1}\" => res }\n    end\n    @output += \"\\n\"\n    @output += @results.to_yaml.sub(\"---\\n\", \"\")\n  end\n  puts @output\nend\n"

#process_resultsObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rubybench_runner/base_runner.rb', line 212

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

Returns:



141
142
143
# File 'lib/rubybench_runner/base_runner.rb', line 141

def require_db?
  false
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubybench_runner/base_runner.rb', line 42

def run
  set_repo_path
  cleanup(before: true)
  create_tmpdir
  check_dependencies
  write_gemfile
  bundle_install
  setup_db
  download_script
  run_benchmarks
  process_results
  print_results
ensure
  cleanup(after: true)
end

#run_benchmarksObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rubybench_runner/base_runner.rb', line 194

def run_benchmarks
  return if @error
  log("Running benchmarks...")
  Dir.chdir(script_path) do
    opts.repeat_count.times do |n|
      res, err = Open3.capture3(command)
      if err.size == 0
        @results[n] = res
        @results[n] = JSON.parse(res)
      else
        raise ShellError.new(err)
      end
    end
  end
rescue => err
  @error = err
end

#save_dirObject



109
110
111
# File 'lib/rubybench_runner/base_runner.rb', line 109

def save_dir
  raise "Override the `save_dir` method in your subclass #{self.class.name}"
end

#script_full_pathObject



121
122
123
# File 'lib/rubybench_runner/base_runner.rb', line 121

def script_full_path
  @script_full_path ||= File.join(script_path, script_name)
end

#script_nameObject



125
126
127
# File 'lib/rubybench_runner/base_runner.rb', line 125

def script_name
  "script.rb"
end

#script_pathObject



117
118
119
# File 'lib/rubybench_runner/base_runner.rb', line 117

def script_path
  @script_path ||= File.join(save_dir, "benchmarks")
end

#set_repo_pathObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubybench_runner/base_runner.rb', line 66

def set_repo_path
  name = benchmark_name.downcase
  from_opts = false
  if path = opts.send(name)
    from_opts = true
    @repo_path = path
  else
    @repo_path = possible_repo_path
    log("Running #{name} benchmark #{@script_name}: #{name} path is #{possible_repo_path}\n")
  end

  unless is_repo_path_valid?
    output = "Cannot find #{name} at #{@repo_path}."
    output += "Perhaps try:\n\nrubybench_runner run #{name}/#{@script_name} --#{name}=/path/to/#{name}" unless from_opts
    log(output, f: true)
    exit 1
  end
end

#setup_dbObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rubybench_runner/base_runner.rb', line 158

def setup_db
  return if !require_db?

  log("Checking database...")
  config = RubybenchRunner::Configurations.new(mysql_map: true)
  if opts.db == "postgres"
    conn_config = config["postgres"]
    rubybench_db = conn_config[:dbname]
    conn_config[:dbname] = "postgres"
    conn = PG.connect(conn_config)
    begin
      res = conn.exec("SELECT 1 FROM pg_database WHERE datname = '#{rubybench_db}'")
      if !res.first
        conn.exec("CREATE DATABASE #{rubybench_db}")
        log("Created PostgreSQL database with the name '#{rubybench_db}'")
      end
    ensure
      conn.close
    end
  elsif opts.db == "mysql2"
    conn_config = config["mysql2"]
    rubybench_db = conn_config[:database]
    conn_config[:database] = "mysql"
    client = Mysql2::Client.new(conn_config)
    begin
      res = client.query("SHOW DATABASES LIKE '#{rubybench_db}'")
      if !res.first
        client.query("CREATE DATABASE #{rubybench_db}")
        log("Created MySQL database with the name '#{rubybench_db}'")
      end
    ensure
      client.close
    end
  end
end

#write_gemfileObject



292
293
294
295
# File 'lib/rubybench_runner/base_runner.rb', line 292

def write_gemfile
  return if without_bundle?
  File.write(gemfile_location, gemfile_content)
end