rspec_chunked 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 76485af77917941cf4e9afbc37c5580077bb3943fb1b9bebdaa5c9ae3c83c8f4
4
+ data.tar.gz: 814d02ffe139f175ae2c2ef8bb2b64d14b8c8407ee37d381fcc00c333b072ee6
5
+ SHA512:
6
+ metadata.gz: 87c877e3ef437d78d6dda51fa63e1c4168c046731891c587ff50db107778a24535427536d885d9f576aa016f10b8fdb91b42062ab9545df7bcbe9fdc4670562a
7
+ data.tar.gz: b0e7f004c432ada92eb8ed2997626e62f8c65ec7b3331af1d98a4d879481e460a05387a5cfcb5fcaadb275e9481193def45aa8cc7c36a38357735da05a322d35
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 owen2345
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # RspecChunked
2
+ This gem permits to run rspec tests in parallel by chunking tests into defined groups and balancing by file size.
3
+ If ordering is not enough, permits to balance manually by moving x percentage of tests files from group A into group B.
4
+
5
+ ## Installation
6
+ - Add this line to your application's Gemfile:
7
+ ```ruby
8
+ group :test do
9
+ gem 'rspec_chunked'
10
+ end
11
+ ```
12
+
13
+ - And then execute:
14
+ `bundle install`
15
+
16
+ - Add manual balance
17
+ ```ruby
18
+ # config/initializers/rspec_chunked.rb
19
+ return unless defined?(RspecChunked::ChunkedTests)
20
+
21
+ RspecChunked::ChunkedTests.balance_settings = { 1 => { to: 2, percentage: 15 }, 4 => { to: 3, percentage: 10 } }
22
+ ```
23
+ Balance tests by moving 15% tests files from group 1 into group 2 and moving 10% tests files from group 4 into group 3
24
+
25
+ ## Usage
26
+ ` CI_JOBS=3 CI_JOB=1 rake rspec_chunked `
27
+ - `CI_JOBS`: quantity of groups/workers to be split
28
+ - `CI_JOB`: current group/worker to be executed, limit: 1 until CI_JOBS
29
+
30
+ ### Github workflow result
31
+ - Before:
32
+ ![Before](/docs/before.png?raw=true)
33
+
34
+ - Current:
35
+ ![Current](/docs/current.png?raw=true)
36
+
37
+ - Github workflow sample:
38
+ ````yaml
39
+ rspec_tests:
40
+ name: Rspec tests
41
+ runs-on: ubuntu-latest
42
+ strategy:
43
+ fail-fast: false
44
+ matrix:
45
+ ci_job: [ 1, 2, 3, 4 ] # enumerize jobs
46
+ env:
47
+ CI_JOBS: 4 # define total jobs (must match with matrix above)
48
+
49
+ steps:
50
+ - uses: actions/checkout@v2
51
+ - name: Backend tests
52
+ env:
53
+ CI_JOB: ${{ matrix.ci_job }}
54
+ run: docker-compose run test /bin/sh -c "CI_JOBS=$CI_JOBS CI_JOB=$CI_JOB rake rspec_chunked"
55
+
56
+ ````
57
+
58
+ ## Contributing
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/owen2345/rspec_chunked. 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.
60
+
61
+ ## License
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RspecChunked'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec_chunked/chunked_tests'
4
+ module RspecChunked
5
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecChunked
4
+ class ChunkedTests
5
+ class << self
6
+ attr_accessor :balance_settings
7
+ end
8
+ attr_accessor :qty_groups, :job_number, :balance_settings
9
+
10
+ def initialize(qty_groups, job_number)
11
+ @qty_groups = qty_groups
12
+ @job_number = job_number - 1
13
+ @balance_settings = self.class.balance_settings || {}
14
+ end
15
+
16
+ def run
17
+ balanced_tests = balance_tests(group_tests, balance_settings)
18
+ tests = balanced_tests[job_number]
19
+ qty = balanced_tests.flatten.count
20
+ Kernel.puts "**** running #{tests.count}/#{qty} tests (group number: #{job_number + 1})"
21
+ Kernel.exec "bundle exec rspec #{tests.join(' ')}"
22
+ end
23
+
24
+ private
25
+
26
+ def group_tests
27
+ res = Array.new(qty_groups).map { [] }
28
+ test_files.each_slice(qty_groups).each do |group|
29
+ group.each_with_index do |path, index|
30
+ res[index] << path
31
+ end
32
+ end
33
+ res
34
+ end
35
+
36
+ def test_files
37
+ Dir['spec/**/*_spec.rb'].sort_by do |path|
38
+ File.size(File.join(__dir__, '../', path)).to_f
39
+ end
40
+ end
41
+
42
+ # Balance tests by moving x percentage tests files from group 1 into group 2
43
+ # Sample balance_tests(data, { 1 => { to: 2, percentage: 15 }, 3 => { to: 0, percentage: 10 } })
44
+ # @return (Array) same array with balance applied tests
45
+ def balance_tests(data, balance_data)
46
+ balance_data.each do |index, info|
47
+ from = index - 1
48
+ to = info[:to] - 1
49
+ move_tests(data, from, to, info[:percentage]) if data[from] && data[to]
50
+ end
51
+ data
52
+ end
53
+
54
+ def move_tests(data, from, to, percentage)
55
+ qty_tests = ((data[from].count * percentage) / 100).round
56
+ moving_tests = data[from][-qty_tests..-1]
57
+ data[to] += moving_tests
58
+ data[from] -= moving_tests
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecChunked
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecChunked
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ desc 'Run chunked rspec tests on specific qty, sample: CI_JOBS=3 CI_JOB=1 rake rspec_chunked'
4
+ task :rspec_chunked do
5
+ qty_jobs = (ENV['CI_JOBS'] || 3).to_i
6
+ job_number = (ENV['CI_JOB'] || 1).to_i
7
+
8
+ service = RspecChunked::ChunkedTests.new(qty_jobs, job_number)
9
+ service.run
10
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_chunked
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - owen2345
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: 'Run chunked rspec tests on specific qty, sample: CI_JOBS=3 CI_JOB=1
70
+ rake rspec_chunked'
71
+ email:
72
+ - owenperedo@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/rspec_chunked.rb
81
+ - lib/rspec_chunked/chunked_tests.rb
82
+ - lib/rspec_chunked/railtie.rb
83
+ - lib/rspec_chunked/version.rb
84
+ - lib/tasks/rspec_chunked_tasks.rake
85
+ homepage: https://github.com/owen2345/rspec_chunked
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.1.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: 'Run chunked rspec tests on specific qty, sample: CI_JOBS=3 CI_JOB=1 rake
108
+ rspec_chunked'
109
+ test_files: []