rubybench_runner 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a40314304c75a5bdd11df55abb49b8df15acb8d18b56ff648bc4b605283e578
4
- data.tar.gz: 668309018a75e3c6aae9c8e2dc6b4cff38ea0803a2596b2d310819660fe9db43
3
+ metadata.gz: 4883f9435daeba8053226ff6256bd319f3af8f2200f3bfba7c96cb230fdd7e9b
4
+ data.tar.gz: 204755d83b2a496893bce8cb22c7288abeccd4ec44943d44c4703e90f57be32e
5
5
  SHA512:
6
- metadata.gz: 01ac42da2673c614f869789cc5da2ef64fbcded01ddac548eb6ff2d6494edc18b6c912b31d1687aa4379007e470aaac9dfa5a324070e7f50c1946152ddb90f69
7
- data.tar.gz: 4fa380cad1ac800a25174e6e8bce37683e273e8753816005c882fdc23b6cc8683f7ea87214f5337413340b0bee06c6a7468344f82c8ee30854fe5809237c5e71
6
+ metadata.gz: 75f582c907dc3c795312be9fcd7f49adffd0d0f342d56fda1b06f88501aed0db1e32a8c530fa35b9d6e6737729dedce341874a43eba0ed788ba36e8300642fc6
7
+ data.tar.gz: ee1e4932c05689b15fadadaee4e20ed232b4e897051e354e81544992221af7d79a123eb75d6808048241bf75df10e975c35ecfda69f0ee5d02f50416602aaa3f
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ - 2019-06-18: 0.1.4
4
+
5
+ - Various enhancements and refactoring (# e07199a)
6
+
3
7
  - 2019-06-16: 0.1.3
4
8
 
5
9
  - FIX: Do not install gems needed for dp setup unless the benchmark need them (# efad3ce)
@@ -4,10 +4,18 @@
4
4
  $:.unshift File.expand_path('../lib', __dir__)
5
5
 
6
6
  require 'rubybench_runner'
7
-
8
7
  require 'optparse'
9
8
 
9
+ RUN = "run"
10
+ HELP = "help"
11
+
10
12
  command = ARGV[0]
13
+ repo = ""
14
+ script = ""
15
+
16
+ if command == RUN
17
+ repo, script = ARGV[1].split("/", 2)
18
+ end
11
19
 
12
20
  options = {}
13
21
  OptionParser.new do |opts|
@@ -15,12 +23,11 @@ OptionParser.new do |opts|
15
23
  Usage: rubybench_runner [command] [args] [options]
16
24
 
17
25
  commands:
18
- * run <repo_name> <script_url> <local_path> [options]
26
+ * #{RUN} <repo_name>/<script_name> [options]
19
27
  <repo_name> is one the repos that are benchmarked on rubybench.org. Valid options are: `rails`
20
- <script_url> is a direct link to the benchmark you want to run.
21
- <local_path> is a path to your local clone of repo_name (e.g. ~/rails)
28
+ <script_name> is the name of the benchmark from rubybench.org e.g. bm_activerecord_finders_find_by_attributes.rb.
22
29
 
23
- * help
30
+ * #{HELP}
24
31
  Show help
25
32
 
26
33
  Available options:
@@ -43,7 +50,7 @@ OptionParser.new do |opts|
43
50
  end
44
51
  end
45
52
 
46
- opts.on("-c", "--repeat-count=COUNT", Integer, "Number of times to run the benchmark (default 2)") do |c|
53
+ opts.on("-c", "--repeat-count=COUNT", Integer, "Number of times to run the benchmark (default 1)") do |c|
47
54
  options[:repeat_count] = c
48
55
  end
49
56
 
@@ -71,7 +78,7 @@ OptionParser.new do |opts|
71
78
  options[:cleanup] = true
72
79
  end
73
80
 
74
- opts.on("-wps", "--with-prepared-statements", "Run benchmarks with prepared statements") do
81
+ opts.on("--wps", "--with-prepared-statements", "Run benchmarks with prepared statements") do
75
82
  options[:wps] = true
76
83
  end
77
84
 
@@ -84,14 +91,23 @@ OptionParser.new do |opts|
84
91
  puts opts
85
92
  exit 0
86
93
  end
94
+
95
+ opts.on("--url=URL", "Direct link to a benchmark script") do |url|
96
+ options[:url] = url
97
+ end
98
+
99
+ RubybenchRunner::SUPPORTED_REPOS.each do |name, _|
100
+ if repo == name.to_s
101
+ opts.on("--#{name}=PATH", "Provide local path to #{name}") do |path|
102
+ options[name] = File.expand_path(path)
103
+ end
104
+ end
105
+ end
87
106
  end.parse!
88
107
 
89
- if command == "run"
90
- repo_name = ARGV[1]
91
- script_url = ARGV[2]
92
- repo_path = File.expand_path(ARGV[3])
93
- RubybenchRunner.run(repo_name, script_url, repo_path, options)
94
- elsif command == "help"
108
+ if command == RUN
109
+ RubybenchRunner.run(repo, script, options)
110
+ elsif command == HELP
95
111
  puts opts
96
112
  else
97
113
  puts "Unknown command #{command}"
@@ -6,12 +6,16 @@ require 'yaml'
6
6
  require 'fileutils'
7
7
  require 'tmpdir'
8
8
  require 'ostruct'
9
+ require 'open3'
9
10
 
10
11
  require 'rubybench_runner/version'
11
- require 'rubybench_runner/base_runner'
12
12
  require 'rubybench_runner/dependencies_checker'
13
13
  require 'rubybench_runner/configurations'
14
+ require 'rubybench_runner/base_runner'
14
15
  require 'rubybench_runner/rails_runner'
15
16
 
16
17
  module RubybenchRunner
18
+ SUPPORTED_REPOS = {
19
+ rails: RailsRunner
20
+ }
17
21
  end
@@ -1,11 +1,14 @@
1
1
  module RubybenchRunner
2
- def self.run(repo, script_url, repo_path, opts = {})
3
- if repo == "rails"
4
- klass = RubybenchRunner::RailsRunner
5
- else
6
- raise "Unknown repo #{repo}"
2
+ class ShellError < StandardError; end
3
+
4
+ def self.run(repo, script, opts = {})
5
+ SUPPORTED_REPOS.each do |name, klass|
6
+ if repo == name.to_s
7
+ klass.new(script, opts).run
8
+ return
9
+ end
7
10
  end
8
- klass.new(script_url, repo_path, opts).run
11
+ raise "Unknown repo #{repo}"
9
12
  end
10
13
 
11
14
  class BaseRunner
@@ -27,18 +30,19 @@ module RubybenchRunner
27
30
  round: 2
28
31
  }
29
32
 
30
- def initialize(url, repo_path, opts = {})
31
- @script_url = normalize_url(url)
32
- @repo_path = repo_path
33
+ def initialize(script, opts = {})
34
+ @script_name = script
33
35
  @opts = OpenStruct.new(DEFAULT_OPTS.merge(opts))
36
+ @script_url = @opts.url || script_full_url(script)
34
37
  @results = []
35
38
  @error = nil
36
39
  @output = ""
37
40
  end
38
41
 
39
42
  def run
40
- create_tmpdir
43
+ set_repo_path
41
44
  cleanup(before: true)
45
+ create_tmpdir
42
46
  check_dependencies
43
47
  write_gemfile
44
48
  bundle_install
@@ -51,6 +55,33 @@ module RubybenchRunner
51
55
  cleanup(after: true)
52
56
  end
53
57
 
58
+ def possible_repo_path
59
+ File.join(Dir.home, benchmark_name.downcase)
60
+ end
61
+
62
+ def is_repo_path_valid?
63
+ true
64
+ end
65
+
66
+ def set_repo_path
67
+ name = benchmark_name.downcase
68
+ from_opts = false
69
+ if path = opts.send(name)
70
+ from_opts = true
71
+ @repo_path = path
72
+ else
73
+ @repo_path = possible_repo_path
74
+ log("Running #{name} benchmark #{@script_name}: #{name} path is #{possible_repo_path}\n")
75
+ end
76
+
77
+ unless is_repo_path_valid?
78
+ output = "Cannot find #{name} at #{@repo_path}."
79
+ output += "Perhaps try:\n\nrubybench_runner run #{name}/#{@script_name} --#{name}=/path/to/#{name}" unless from_opts
80
+ log(output, f: true)
81
+ exit 1
82
+ end
83
+ end
84
+
54
85
  def dest_dir
55
86
  # directory where all helpers and gems will be installed/copied to
56
87
  @dest_dir ||= File.join(Dir.tmpdir, "rubybench_runner_tmp")
@@ -161,12 +192,17 @@ module RubybenchRunner
161
192
  end
162
193
 
163
194
  def run_benchmarks
195
+ return if @error
164
196
  log("Running benchmarks...")
165
197
  Dir.chdir(script_path) do
166
198
  opts.repeat_count.times do |n|
167
- res = `#{command}`
168
- @results[n] = res
169
- @results[n] = JSON.parse(res)
199
+ res, err = Open3.capture3(command)
200
+ if err.size == 0
201
+ @results[n] = res
202
+ @results[n] = JSON.parse(res)
203
+ else
204
+ raise ShellError.new(err)
205
+ end
170
206
  end
171
207
  end
172
208
  rescue => err
@@ -193,18 +229,21 @@ module RubybenchRunner
193
229
  if @error
194
230
  @output = <<~OUTPUT
195
231
  An error occurred while running the benchmarks:
196
- Error: #{@error.message}
232
+ Error #{@error.class}: #{@error.message}
197
233
  Backtrace:
198
234
  #{@error.backtrace.join("\n")}
199
- ------
200
- Raw results until this error:
201
- #{@results}
202
235
  OUTPUT
236
+ if @results.size > 0
237
+ @output += <<~OUTPUT
238
+ ------
239
+ Raw results until this error:
240
+ #{@results}
241
+ OUTPUT
242
+ end
203
243
  else
204
244
  label = @results.first['label']
205
245
  version = @results.first['version']
206
246
  @output = "#{benchmark_name} version #{version}\n"
207
- @output += "Benchmark name: #{label}\n"
208
247
  @output += "Results (#{@results.size} runs):\n"
209
248
  @results.map!.with_index do |res, ind|
210
249
  res.delete('label')
@@ -245,6 +284,9 @@ module RubybenchRunner
245
284
  log("Downloading script...")
246
285
  content = open(script_url).read
247
286
  File.write(script_full_path, content)
287
+ rescue OpenURI::HTTPError => e
288
+ log("Script download failed #{script_url}", f: true)
289
+ @error = e
248
290
  end
249
291
 
250
292
  def write_gemfile
@@ -254,8 +296,8 @@ module RubybenchRunner
254
296
 
255
297
  private
256
298
 
257
- def log(msg)
258
- return if opts.quiet
299
+ def log(msg, f: false)
300
+ return if opts.quiet && !f
259
301
  puts msg
260
302
  end
261
303
 
@@ -272,12 +314,11 @@ module RubybenchRunner
272
314
  FileUtils.rm_rf(dest_dir) if (opts.fresh_run && before) || (opts.cleanup && after)
273
315
  end
274
316
 
275
- def normalize_url(url)
276
- if url =~ /^(https?:\/\/github.com)/
277
- url.sub($1, "https://raw.githubusercontent.com").sub("/blob/", "/")
278
- else
279
- url
317
+ def script_full_url(script)
318
+ if script !~ /\.rb$/
319
+ script += ".rb"
280
320
  end
321
+ "https://raw.githubusercontent.com/ruby-bench/ruby-bench-suite/master/#{benchmark_name.downcase}/benchmarks/#{script}"
281
322
  end
282
323
 
283
324
  # small hack for dynamically installing/requiring gems.
@@ -8,6 +8,10 @@ module RubybenchRunner
8
8
  command
9
9
  end
10
10
 
11
+ def is_repo_path_valid?
12
+ File.exists?(File.join(repo_path, "rails.gemspec"))
13
+ end
14
+
11
15
  def database_url
12
16
  return @db_url if @db_url
13
17
  raw_config = RubybenchRunner::Configurations.new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubybenchRunner
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubybench_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Osama Sayegh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-16 00:00:00.000000000 Z
11
+ date: 2019-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug