rubybench_runner 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +7 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +44 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +43 -0
  9. data/Rakefile +12 -0
  10. data/bin/console +15 -0
  11. data/bin/setup +8 -0
  12. data/exe/rubybench_runner +101 -0
  13. data/lib/rubybench_runner.rb +17 -0
  14. data/lib/rubybench_runner/base_runner.rb +233 -0
  15. data/lib/rubybench_runner/configurations.rb +68 -0
  16. data/lib/rubybench_runner/dependencies_checker.rb +39 -0
  17. data/lib/rubybench_runner/helpers_version +1 -0
  18. data/lib/rubybench_runner/rails/.bundle/config +4 -0
  19. data/lib/rubybench_runner/rails/benchmarks/assets/javascripts/application.js +3 -0
  20. data/lib/rubybench_runner/rails/benchmarks/assets/javascripts/one.js.erb +5 -0
  21. data/lib/rubybench_runner/rails/benchmarks/assets/javascripts/two.js +1 -0
  22. data/lib/rubybench_runner/rails/benchmarks/form_partials/first.html.erb +17 -0
  23. data/lib/rubybench_runner/rails/benchmarks/form_partials/heavy/_item.html.erb +12 -0
  24. data/lib/rubybench_runner/rails/benchmarks/form_partials/heavy/_second.html.erb +7 -0
  25. data/lib/rubybench_runner/rails/benchmarks/form_partials/heavy/_third.html.erb +8 -0
  26. data/lib/rubybench_runner/rails/benchmarks/form_partials/layouts/application.html.erb +29 -0
  27. data/lib/rubybench_runner/rails/benchmarks/partials/first.html.erb +19 -0
  28. data/lib/rubybench_runner/rails/benchmarks/partials/heavy/_item.html.erb +0 -0
  29. data/lib/rubybench_runner/rails/benchmarks/partials/heavy/_second.html.erb +7 -0
  30. data/lib/rubybench_runner/rails/benchmarks/partials/heavy/_third.html.erb +3 -0
  31. data/lib/rubybench_runner/rails/benchmarks/partials/layouts/application.html.erb +29 -0
  32. data/lib/rubybench_runner/rails/benchmarks/support/benchmark_rails.rb +14 -0
  33. data/lib/rubybench_runner/rails/benchmarks/support/echo_channel.rb +13 -0
  34. data/lib/rubybench_runner/rails/benchmarks/support/request_helper.rb +9 -0
  35. data/lib/rubybench_runner/rails/benchmarks/support/url_generation_base.rb +79 -0
  36. data/lib/rubybench_runner/rails/benchmarks/views/posts/_form.html.erb +29 -0
  37. data/lib/rubybench_runner/rails/benchmarks/views/posts/edit.html.erb +6 -0
  38. data/lib/rubybench_runner/rails/benchmarks/views/posts/index.html.erb +32 -0
  39. data/lib/rubybench_runner/rails/benchmarks/views/posts/index.json.jbuilder +4 -0
  40. data/lib/rubybench_runner/rails/benchmarks/views/posts/new.html.erb +5 -0
  41. data/lib/rubybench_runner/rails/benchmarks/views/posts/show.html.erb +19 -0
  42. data/lib/rubybench_runner/rails/benchmarks/views/posts/show.json.jbuilder +1 -0
  43. data/lib/rubybench_runner/rails_runner.rb +91 -0
  44. data/lib/rubybench_runner/support/benchmark_runner.rb +62 -0
  45. data/lib/rubybench_runner/support/helpers.rb +11 -0
  46. data/lib/rubybench_runner/support/setup/.bundle/config +4 -0
  47. data/lib/rubybench_runner/support/setup/Gemfile +11 -0
  48. data/lib/rubybench_runner/support/setup/bm_create_string_columns_setup.rb +16 -0
  49. data/lib/rubybench_runner/support/setup/bm_destroy_setup.rb +12 -0
  50. data/lib/rubybench_runner/support/setup/bm_discourse_setup.rb +164 -0
  51. data/lib/rubybench_runner/support/setup/bm_finders_setup.rb +37 -0
  52. data/lib/rubybench_runner/support/setup/bm_preload_setup.rb +41 -0
  53. data/lib/rubybench_runner/support/setup/bm_save_setup.rb +12 -0
  54. data/lib/rubybench_runner/support/setup/bm_scope_all_setup.rb +32 -0
  55. data/lib/rubybench_runner/support/setup/bm_scope_where_setup.rb +30 -0
  56. data/lib/rubybench_runner/support/setup/bm_validations_invalid_setup.rb +18 -0
  57. data/lib/rubybench_runner/support/setup/bm_validations_valid_setup.rb +30 -0
  58. data/lib/rubybench_runner/support/setup/bm_with_default_scope_setup.rb +29 -0
  59. data/lib/rubybench_runner/version.rb +5 -0
  60. data/rubybench_runner.gemspec +34 -0
  61. metadata +200 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ca15668599b749d7424f2af741069ee545fe8896858c4c672fcf09c79472ee95
4
+ data.tar.gz: 71201b051732cc8cf52865e8bc0a86406eb9059e2fbd9cf89678f674d17016a0
5
+ SHA512:
6
+ metadata.gz: b4f64c8f8020652216c1be336580b4e8d4d7e5e7e605658fd631ee34fdd9338b8fd756b29273307aabb68c77445bb8578ed608289c9c7d062e42c5bcc3d01bfa
7
+ data.tar.gz: 645cfc04e1f8b679bb799be1cb50a5379b165113c56309517809830928534421a062cbe60834bfe93833b5668ad314a67bc01e94cb982558478cdf979ceb8b39
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ .byebug_history
9
+ *.gem
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at asooomaasoooma90@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in rubybench_runner.gemspec
6
+ gemspec
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubybench_runner (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ byebug (11.0.1)
11
+ jaro_winkler (1.5.2)
12
+ minitest (5.11.3)
13
+ mysql2 (0.5.2)
14
+ parallel (1.17.0)
15
+ parser (2.6.3.0)
16
+ ast (~> 2.4.0)
17
+ pg (1.1.4)
18
+ rainbow (3.0.0)
19
+ rake (10.5.0)
20
+ rubocop (0.71.0)
21
+ jaro_winkler (~> 1.5.1)
22
+ parallel (~> 1.10)
23
+ parser (>= 2.6)
24
+ rainbow (>= 2.2.2, < 4.0)
25
+ ruby-progressbar (~> 1.7)
26
+ unicode-display_width (>= 1.4.0, < 1.7)
27
+ ruby-progressbar (1.10.1)
28
+ unicode-display_width (1.6.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 2.0)
35
+ byebug
36
+ minitest (~> 5.0)
37
+ mysql2 (= 0.5.2)
38
+ pg (= 1.1.4)
39
+ rake (~> 10.0)
40
+ rubocop (~> 0.70)
41
+ rubybench_runner!
42
+
43
+ BUNDLED WITH
44
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 OsamaSayegh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # RubybenchRunner
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubybench_runner`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rubybench_runner'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubybench_runner
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubybench_runner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the RubybenchRunner project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubybench_runner/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rubybench_runner'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $:.unshift File.expand_path('../lib', __dir__)
5
+
6
+ require 'rubybench_runner'
7
+
8
+ require 'optparse'
9
+
10
+ command = ARGV[0]
11
+
12
+ options = {}
13
+ OptionParser.new do |opts|
14
+ opts.banner = <<~BANNER
15
+ Usage: rubybench_runner [command] [args] [options]
16
+
17
+ commands:
18
+ * run <repo_name> <script_url> <local_path> [options]
19
+ <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)
22
+
23
+ * help
24
+ Show help
25
+
26
+ Available options:
27
+ BANNER
28
+ opts.on("--db=DB", "Select database to use for the benchmarks. Valud options are: `postgres` and `mysql`") do |v|
29
+ v = v.strip.downcase
30
+ pg_options = %w{pg postgres postgresql}
31
+ mysql_options = %w{mysql mysql2}
32
+ if pg_options.include?(v)
33
+ options[:db] = "postgres"
34
+ elsif mysql_options.include?(v)
35
+ options[:db] = "mysql2"
36
+ else
37
+ puts <<~OUTPUT
38
+ Invalid --db value \"#{v}\". Valid options are: (case insensitive)
39
+ #{pg_options.first} (aliases: #{pg_options[1..-1].join(", ")})
40
+ #{mysql_options.first} (aliases: #{mysql_options[1..-1].join(", ")})
41
+ OUTPUT
42
+ exit 1
43
+ end
44
+ end
45
+
46
+ opts.on("-c", "--repeat-count=COUNT", Integer, "Number of times to run the benchmark (default 2)") do |c|
47
+ options[:repeat_count] = c
48
+ end
49
+
50
+ opts.on("--skip-dep-check", "Skip dependency checks performed before the benchmarks are run") do
51
+ options[:skip_dependencies_check] = true
52
+ end
53
+
54
+ opts.on("-r", "--round=ROUND", Integer, "Round results to the nearest value with a precision of N digits (default 2)") do |r|
55
+ options[:round] = r
56
+ end
57
+
58
+ opts.on("--quiet", "Be quite when the the benchmarks are being run") do
59
+ options[:quiet] = true
60
+ end
61
+
62
+ opts.on("--verbose", "Show extra informative output while the benchmarks are running") do
63
+ options[:verbose] = true
64
+ end
65
+
66
+ opts.on("--fresh", "Remove installed gems from previous runs and reinstall them for this run") do
67
+ options[:fresh_run] = true
68
+ end
69
+
70
+ opts.on("--cleanup", "Remove gems installed during this run after the run is complete") do
71
+ options[:cleanup] = true
72
+ end
73
+
74
+ opts.on("-wps", "--with-prepared-statements", "Run benchmarks with prepared statements") do
75
+ options[:wps] = true
76
+ end
77
+
78
+ opts.on("-v", "--version", "Display installed version of RubybenchRunner") do
79
+ puts RubybenchRunner::VERSION
80
+ exit 0
81
+ end
82
+
83
+ opts.on("-h", "--help", "Show help") do
84
+ puts opts
85
+ exit 0
86
+ end
87
+ end.parse!
88
+
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"
95
+ puts opts
96
+ else
97
+ puts "Unknown command #{command}"
98
+ exit 1
99
+ end
100
+
101
+ # "https://github.com/ruby-bench/ruby-bench-suite/blob/master/rails/benchmarks/bm_activerecord_destroy.rb"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+ require 'json'
5
+ require 'yaml'
6
+ require 'fileutils'
7
+ require 'tmpdir'
8
+ require 'ostruct'
9
+
10
+ require 'rubybench_runner/version'
11
+ require 'rubybench_runner/base_runner'
12
+ require 'rubybench_runner/dependencies_checker'
13
+ require 'rubybench_runner/configurations'
14
+ require 'rubybench_runner/rails_runner'
15
+
16
+ module RubybenchRunner
17
+ end
@@ -0,0 +1,233 @@
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}"
7
+ end
8
+ klass.new(script_url, repo_path, opts).run
9
+ end
10
+
11
+ class BaseRunner
12
+ HELPERS_VERSION_FILE = "helpers_version"
13
+
14
+ COPY_FOLDERS = %w{
15
+ support
16
+ rails
17
+ }
18
+ attr_reader :script_url, :repo_path, :opts
19
+
20
+ DEFAULT_OPTS = {
21
+ repeat_count: 1,
22
+ quiet: false,
23
+ fresh_run: false,
24
+ skip_dependencies_check: false,
25
+ cleanup: false,
26
+ wps: false,
27
+ round: 2
28
+ }
29
+
30
+ def initialize(url, repo_path, opts = {})
31
+ @script_url = normalize_url(url)
32
+ @repo_path = repo_path
33
+ @opts = OpenStruct.new(DEFAULT_OPTS.merge(opts))
34
+ @results = []
35
+ @error = nil
36
+ @output = ""
37
+ end
38
+
39
+ def run
40
+ create_tmpdir
41
+ cleanup(before: true)
42
+ check_dependencies
43
+ download_script
44
+ write_gemfile
45
+ bundle_install
46
+ setup_db
47
+ run_benchmarks
48
+ process_results
49
+ print_results
50
+ ensure
51
+ cleanup(after: true)
52
+ end
53
+
54
+ def dest_dir
55
+ # directory where all helpers and gems will be installed/copied to
56
+ @dest_dir ||= File.join(Dir.tmpdir, "rubybench_runner_tmp")
57
+ end
58
+
59
+ def create_tmpdir
60
+ FileUtils.mkdir_p(dest_dir)
61
+ copy_helpers(dest_dir)
62
+ end
63
+
64
+ def copy_helpers(dest)
65
+ gem_helpers_version_path = File.join(__dir__, HELPERS_VERSION_FILE)
66
+ current_helpers_version_path = File.join(dest, HELPERS_VERSION_FILE)
67
+ if !File.exists?(current_helpers_version_path) ||
68
+ File.read(gem_helpers_version_path) != File.read(current_helpers_version_path)
69
+ FileUtils.cp(gem_helpers_version_path, current_helpers_version_path)
70
+ COPY_FOLDERS.each do |folder|
71
+ origin = File.join(__dir__, folder)
72
+ destination = File.join(dest, folder)
73
+ FileUtils.cp_r(origin, destination)
74
+ end
75
+ end
76
+ end
77
+
78
+ def save_dir
79
+ raise "Override the `save_dir` method in your subclass #{self.class.name}"
80
+ end
81
+
82
+ def gemfile_content
83
+ nil
84
+ end
85
+
86
+ def script_path
87
+ @script_path ||= File.join(save_dir, "benchmarks")
88
+ end
89
+
90
+ def script_full_path
91
+ @script_full_path ||= File.join(script_path, script_name)
92
+ end
93
+
94
+ def script_name
95
+ "script.rb"
96
+ end
97
+
98
+ def command
99
+ if without_bundle?
100
+ "ruby #{script_name}"
101
+ else
102
+ "BUNDLE_GEMFILE=#{gemfile_location} bundle exec ruby #{script_name}"
103
+ end
104
+ end
105
+
106
+ def benchmark_name
107
+ raise "Override the `benchmark_name` method in your class"
108
+ end
109
+
110
+ def require_db?
111
+ false
112
+ end
113
+
114
+ def check_dependencies
115
+ return if opts.skip_dependencies_check
116
+ log("Checking dependencies...")
117
+ RubybenchRunner::DependenciesChecker.check
118
+ end
119
+
120
+ def setup_db
121
+ end
122
+
123
+ def run_benchmarks
124
+ log("Running benchmarks...")
125
+ Dir.chdir(script_path) do
126
+ opts.repeat_count.times do |n|
127
+ res = `#{command}`
128
+ @results[n] = res
129
+ @results[n] = JSON.parse(res)
130
+ end
131
+ end
132
+ rescue => err
133
+ @error = err
134
+ end
135
+
136
+ def process_results
137
+ return if @error
138
+ @results.map! do |res|
139
+ res.each do |key, val|
140
+ if Float === val
141
+ res[key] = val.round(opts.round)
142
+ end
143
+ end
144
+ res
145
+ end
146
+
147
+ @results.sort_by! do |res|
148
+ res['iterations_per_second']
149
+ end
150
+ end
151
+
152
+ def print_results
153
+ if @error
154
+ @output = <<~OUTPUT
155
+ An error occurred while running the benchmarks:
156
+ Error: #{@error.message}
157
+ Backtrace:
158
+ #{@error.backtrace.join("\n")}
159
+ ------
160
+ Raw results until this error:
161
+ #{@results}
162
+ OUTPUT
163
+ else
164
+ label = @results.first['label']
165
+ version = @results.first['version']
166
+ @output = "#{benchmark_name} version #{version}\n"
167
+ @output += "Benchmark name: #{label}\n"
168
+ @output += "Results (#{@results.size} runs):\n"
169
+ @results.map!.with_index do |res, ind|
170
+ res.delete('label')
171
+ res.delete('version')
172
+ { "run #{ind + 1}" => res }
173
+ end
174
+ @output += "\n"
175
+ @output += @results.to_yaml.sub("---\n", "")
176
+ end
177
+ puts @output
178
+ end
179
+
180
+ def bundle_install
181
+ return if without_bundle?
182
+
183
+ log("Installing gems...")
184
+ comm = "bundle install"
185
+ comm += " > /dev/null 2>&1" if !opts.verbose
186
+ Dir.chdir(File.dirname(gemfile_location)) do
187
+ system(comm)
188
+ end
189
+ Dir.chdir(File.join(dest_dir, "support", "setup")) do
190
+ system(comm)
191
+ end
192
+ end
193
+
194
+ def download_script
195
+ log("Downloading script...")
196
+ content = open(script_url).read
197
+ File.write(script_full_path, content)
198
+ end
199
+
200
+ def write_gemfile
201
+ return if without_bundle?
202
+ File.write(gemfile_location, gemfile_content)
203
+ end
204
+
205
+ private
206
+
207
+ def log(msg)
208
+ return if opts.quiet
209
+ puts msg
210
+ end
211
+
212
+ def without_bundle?
213
+ !gemfile_content
214
+ end
215
+
216
+ def gemfile_location
217
+ File.join(save_dir, "Gemfile")
218
+ end
219
+
220
+ def cleanup(before: false, after: false)
221
+ FileUtils.rm_f(script_full_path)
222
+ FileUtils.rm_rf(dest_dir) if (opts.fresh_run && before) || (opts.cleanup && after)
223
+ end
224
+
225
+ def normalize_url(url)
226
+ if url =~ /^(https?:\/\/github.com)/
227
+ url.sub($1, "https://raw.githubusercontent.com").sub("/blob", "")
228
+ else
229
+ url
230
+ end
231
+ end
232
+ end
233
+ end