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(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

#optsObject (readonly)

Returns the value of attribute opts.



18
19
20
# File 'lib/rubybench_runner/base_runner.rb', line 18

def opts
  @opts
end

#repo_pathObject (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_urlObject (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_nameObject



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_installObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rubybench_runner/base_runner.rb', line 220

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
  Dir.chdir(File.join(dest_dir, "support", "setup")) do
    system(comm)
  end
end

#check_dependenciesObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubybench_runner/base_runner.rb', line 114

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



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_tmpdirObject



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_dirObject



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_scriptObject



241
242
243
244
245
# File 'lib/rubybench_runner/base_runner.rb', line 241

def download_script
  log("Downloading script...")
  content = open(script_url).read
  File.write(script_full_path, content)
end

#gemfile_contentObject



82
83
84
# File 'lib/rubybench_runner/base_runner.rb', line 82

def gemfile_content
  nil
end


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rubybench_runner/base_runner.rb', line 192

def print_results
  if @error
    @output = <<~OUTPUT
      An error occurred while running the benchmarks:
      Error: #{@error.message}
      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_resultsObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rubybench_runner/base_runner.rb', line 176

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:

  • (Boolean)


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

def require_db?
  false
end

#runObject



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
  write_gemfile
  bundle_install
  setup_db
  download_script
  run_benchmarks
  process_results
  print_results
ensure
  cleanup(after: true)
end

#run_benchmarksObject



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rubybench_runner/base_runner.rb', line 163

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_dirObject



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_pathObject



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_nameObject



94
95
96
# File 'lib/rubybench_runner/base_runner.rb', line 94

def script_name
  "script.rb"
end

#script_pathObject



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

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

#setup_dbObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rubybench_runner/base_runner.rb', line 127

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



247
248
249
250
# File 'lib/rubybench_runner/base_runner.rb', line 247

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