-
Notifications
You must be signed in to change notification settings - Fork 48
Add option to balance jobs by example instead of by file #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grk
wants to merge
3
commits into
briandunn:master
Choose a base branch
from
missive:balance-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rspec/core/sandbox' | ||
|
|
||
| module Flatware | ||
| module RSpec | ||
| # groups examples into one job per worker. | ||
| # reads from persisted example statuses, if available, | ||
| # and attempts to ballence the jobs accordingly. | ||
| class ExampleJobBuilder | ||
| attr_reader :args, :workers | ||
|
|
||
| def initialize(args, workers:) | ||
| @args = args | ||
| @workers = workers | ||
|
|
||
| load_configuration_and_examples | ||
| end | ||
|
|
||
| def jobs | ||
| timed_examples, untimed_examples = timed_and_untimed_examples | ||
| buckets = Array.new([@examples_to_run.size, workers].min) { Bucket.new } | ||
|
|
||
| balance_jobs( | ||
| buckets: buckets, | ||
| timed_examples: timed_examples, | ||
| untimed_examples: untimed_examples | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def balance_jobs(buckets:, timed_examples:, untimed_examples:) | ||
| timed_examples.each do |(example_id, time)| | ||
| buckets.min_by(&:runtime).add_example(example_id, time) | ||
| end | ||
|
|
||
| untimed_examples.each_with_index do |example_id, index| | ||
| offset = (timed_examples.size + index) % buckets.size | ||
| buckets[offset].add_example(example_id) | ||
| end | ||
|
|
||
| buckets.map { |bucket| Job.new(bucket.examples, args) } | ||
| end | ||
|
|
||
| def timed_and_untimed_examples | ||
| timed_examples = [] | ||
| untimed_examples = [] | ||
|
|
||
| @examples_to_run.each do |example_id| | ||
| if (time = example_runtimes[example_id]) | ||
| timed_examples << [example_id, time] | ||
| else | ||
| untimed_examples << example_id | ||
| end | ||
| end | ||
|
|
||
| [timed_examples.sort_by! { |(_id, time)| -time }, untimed_examples] | ||
| end | ||
|
|
||
| def load_persisted_example_statuses | ||
| ::RSpec::Core::ExampleStatusPersister.load_from(@example_status_persistence_file_path || '') | ||
| end | ||
|
|
||
| def example_runtimes | ||
| @example_runtimes ||= load_persisted_example_statuses.each_with_object({}) do |status_entry, runtimes| | ||
| next unless status_entry.fetch(:status) =~ /pass/i | ||
|
|
||
| runtimes[status_entry[:example_id]] = status_entry[:run_time].to_f | ||
| end | ||
| end | ||
|
|
||
| def load_configuration_and_examples | ||
| configuration = ::RSpec.configuration | ||
| configuration.define_singleton_method(:command) { 'rspec' } | ||
|
|
||
| ::RSpec::Core::ConfigurationOptions.new(args).configure(configuration) | ||
|
|
||
| @example_status_persistence_file_path = configuration.example_status_persistence_file_path | ||
|
|
||
| # Load spec files in a fork to avoid polluting the parent process, | ||
| # otherwise the actual execution will return warnings for redefining constants | ||
| # and shared example groups. | ||
| @examples_to_run = within_forked_process { load_examples_to_run(configuration) } | ||
| end | ||
|
|
||
| def within_forked_process | ||
| reader, writer = IO.pipe(binmode: true) | ||
|
|
||
| fork do | ||
| reader.close | ||
| $stdout = File.new(File::NULL, 'w') | ||
|
|
||
| writer.write Marshal.dump(yield) | ||
| end | ||
|
|
||
| writer.close | ||
| Marshal.load(reader.gets(nil)) # rubocop:disable Security/MarshalLoad | ||
| end | ||
|
|
||
| def load_examples_to_run(configuration) | ||
| configuration.load_spec_files | ||
|
|
||
| # If there's an error loading spec files, exit immediately. | ||
| exit(configuration.failure_exit_code) if ::RSpec.world.wants_to_quit | ||
|
|
||
| ::RSpec.world.ordered_example_groups.flat_map(&:descendants).flat_map(&:filtered_examples).map(&:id) | ||
| end | ||
|
|
||
| class Bucket | ||
| attr_reader :examples, :runtime | ||
|
|
||
| def initialize | ||
| @examples = [] | ||
| @runtime = 0 | ||
| end | ||
|
|
||
| def add_example(example_id, runtime = 0) | ||
| @examples << example_id | ||
| @runtime += runtime | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require 'flatware/rspec/example_job_builder' | ||
|
|
||
| describe Flatware::RSpec::ExampleJobBuilder do | ||
| before do | ||
| allow(RSpec::Core::ExampleStatusPersister).to( | ||
| receive(:load_from).and_return(persisted_examples) | ||
| ) | ||
|
|
||
| allow_any_instance_of(RSpec::Core::World).to( | ||
| receive(:ordered_example_groups).and_return(ordered_example_groups) | ||
| ) | ||
| end | ||
|
|
||
| let(:persisted_examples) { [] } | ||
| let(:examples_to_run) { [] } | ||
| let(:ordered_example_groups) do | ||
| examples_to_run | ||
| .group_by { |example_id| example_id.split('[').first } | ||
| .map do |_file_name, example_ids| | ||
| double(descendants: [double(filtered_examples: example_ids.map { |id| double(id: id) })]) | ||
| end | ||
| end | ||
|
|
||
| subject do | ||
| described_class.new([], workers: 2).jobs | ||
| end | ||
|
|
||
| context 'when this run includes persisted examples' do | ||
| let(:persisted_examples) do | ||
| [ | ||
| { example_id: './fast_1_spec.rb[1]', run_time: '1 second' }, | ||
| { example_id: './fast_2_spec.rb[1]', run_time: '1 second' }, | ||
| { example_id: './fast_3_spec.rb[1]', run_time: '1 second' }, | ||
| { example_id: './slow_spec.rb[1]', run_time: '2 seconds' } | ||
| ].map { |example| example.merge status: 'passed' } | ||
| end | ||
|
|
||
| let(:examples_to_run) { %w(./fast_1_spec.rb[1] ./fast_2_spec.rb[1] ./slow_spec.rb[1]) } | ||
|
|
||
| it 'groups them into equal time blocks' do | ||
| expect(subject).to match_array( | ||
| [ | ||
| have_attributes( | ||
| id: match_array(%w[./fast_1_spec.rb[1] ./fast_2_spec.rb[1]]) | ||
| ), | ||
| have_attributes(id: match_array(%w[./slow_spec.rb[1]])) | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| context 'and this run includes examples that are not persisted' do | ||
| let(:examples_to_run) do | ||
| %w[ | ||
| ./fast_1_spec.rb[1] | ||
| ./fast_2_spec.rb[1] | ||
| ./slow_spec.rb[1] | ||
| ./new_1_spec.rb[1] | ||
| ./new_2_spec.rb[1] | ||
| ./new_3_spec.rb[1] | ||
| ] | ||
| end | ||
|
|
||
| it 'assigns the remaining files round-robin' do | ||
| expect(subject).to match_array( | ||
| [ | ||
| have_attributes(id: include('./new_1_spec.rb[1]', './new_3_spec.rb[1]')), | ||
| have_attributes(id: include('./new_2_spec.rb[1]')) | ||
| ] | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'and an example from one file takes longer than all other examples' do | ||
| let(:persisted_examples) do | ||
| [ | ||
| { example_id: './spec_1.rb[1]', run_time: '10 seconds' }, | ||
| { example_id: './spec_1.rb[2]', run_time: '1 second' }, | ||
| { example_id: './spec_1.rb[3]', run_time: '1 second' }, | ||
| { example_id: './spec_2.rb[1]', run_time: '1 second' }, | ||
| { example_id: './spec_2.rb[2]', run_time: '1 second' }, | ||
| { example_id: './spec_2.rb[3]', run_time: '1 second' } | ||
| ].map { |example| example.merge status: 'passed' } | ||
| end | ||
|
|
||
| let(:examples_to_run) do | ||
| %w(./spec_1.rb[1] ./spec_1.rb[2] ./spec_1.rb[3] ./spec_2.rb[1] ./spec_2.rb[2] ./spec_2.rb[3]) | ||
| end | ||
|
|
||
| it 'assigns that example as sole in one job' do | ||
| expect(subject).to match_array( | ||
| [ | ||
| have_attributes(id: ['./spec_1.rb[1]']), | ||
| have_attributes(id: match_array(%w[./spec_1.rb[2] ./spec_1.rb[3] ./spec_2.rb[1] ./spec_2.rb[2] | ||
| ./spec_2.rb[3]])) | ||
| ] | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
4 changes: 2 additions & 2 deletions
4
spec/flatware/rspec/job_builder_spec.rb → spec/flatware/rspec/file_job_builder_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a bit tricky - I need to access
RSpec.world.ordered_example_groupswhich only works afterconfiguration.load_spec_files, but calling that loads rails_helper (etc) which can define some constants, and then the actual execution in workers would load these files producing warnings.Would be great if there was another way to do it, but for now the least-bad idea I had was to get the list of examples from a forked process.