Best Parallel_tests_ruby code snippet using ParallelTests.Test.sort_by_runtime
runner.rb
Source:runner.rb
...39 tests.map! { |t| [t, 1] }40 when :filesize41 sort_by_filesize(tests)42 when :runtime43 sort_by_runtime(tests, runtimes(tests, options), options.merge(allowed_missing: (options[:allowed_missing_percent] || 50) / 100.0))44 when nil45 # use recorded test runtime if we got enough data46 runtimes = runtimes(tests, options) rescue []47 if runtimes.size * 1.5 > tests.size48 puts "Using recorded test runtime"49 sort_by_runtime(tests, runtimes)50 else51 sort_by_filesize(tests)52 end53 else54 raise ArgumentError, "Unsupported option #{options[:group_by]}"55 end56 tests57 end58 def execute_command(cmd, process_number, num_processes, options)59 env = (options[:env] || {}).merge(60 "TEST_ENV_NUMBER" => test_env_number(process_number, options).to_s,61 "PARALLEL_TEST_GROUPS" => num_processes.to_s,62 "PARALLEL_PID_FILE" => ParallelTests.pid_file_path,63 )64 cmd = "nice #{cmd}" if options[:nice]65 cmd = "#{cmd} 2>&1" if options[:combine_stderr]66 puts cmd if options[:verbose] && !options[:serialize_stdout]67 execute_command_and_capture_output(env, cmd, options)68 end69 def execute_command_and_capture_output(env, cmd, options)70 pid = nil71 output = IO.popen(env, cmd) do |io|72 pid = io.pid73 ParallelTests.pids.add(pid)74 capture_output(io, env, options)75 end76 ParallelTests.pids.delete(pid) if pid77 exitstatus = $?.exitstatus78 seed = output[/seed (\d+)/,1]79 output = [cmd, output].join("\n") if options[:verbose] && options[:serialize_stdout]80 {:stdout => output, :exit_status => exitstatus, :command => cmd, :seed => seed}81 end82 def find_results(test_output)83 test_output.lines.map do |line|84 line.chomp!85 line.gsub!(/\e\[\d+m/, '') # remove color coding86 next unless line_is_result?(line)87 line88 end.compact89 end90 def test_env_number(process_number, options={})91 if process_number == 0 && !options[:first_is_1]92 ''93 else94 process_number + 195 end96 end97 def summarize_results(results)98 sums = sum_up_results(results)99 sums.sort.map{|word, number| "#{number} #{word}#{'s' if number != 1}" }.join(', ')100 end101 # remove old seed and add new seed102 def command_with_seed(cmd, seed)103 clean = cmd.sub(/\s--seed\s+\d+\b/, '')104 "#{clean} --seed #{seed}"105 end106 protected107 def executable108 ENV['PARALLEL_TESTS_EXECUTABLE'] || determine_executable109 end110 def determine_executable111 "ruby"112 end113 def sum_up_results(results)114 results = results.join(' ').gsub(/s\b/,'') # combine and singularize results115 counts = results.scan(/(\d+) (\w+)/)116 counts.inject(Hash.new(0)) do |sum, (number, word)|117 sum[word] += number.to_i118 sum119 end120 end121 # read output of the process and print it in chunks122 def capture_output(out, env, options={})123 result = ""124 loop do125 begin126 read = out.readpartial(1000000) # read whatever chunk we can get127 if Encoding.default_internal128 read = read.force_encoding(Encoding.default_internal)129 end130 result << read131 unless options[:serialize_stdout]132 message = read133 message = "[TEST GROUP #{env['TEST_ENV_NUMBER']}] #{message}" if options[:prefix_output_with_test_env_number]134 $stdout.print message135 $stdout.flush136 end137 end138 end rescue EOFError139 result140 end141 def sort_by_runtime(tests, runtimes, options={})142 allowed_missing = options[:allowed_missing] || 1.0143 allowed_missing = tests.size * allowed_missing144 # set know runtime for each test145 tests.sort!146 tests.map! do |test|147 allowed_missing -= 1 unless time = runtimes[test]148 if allowed_missing < 0149 log = options[:runtime_log] || runtime_log150 raise "Runtime log file '#{log}' does not contain sufficient data to sort #{tests.size} test files, please update it."151 end152 [test, time]153 end154 if options[:verbose]155 puts "Runtime found for #{tests.count(&:last)} of #{tests.size} tests"...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!