Class: Flaky::Commands::Stress

Inherits:
Object
  • Object
show all
Defined in:
lib/flaky/commands/stress.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec_location:, iterations: 20, seed: nil, ci_simulate: false, timeout: 600) ⇒ Stress

Returns a new instance of Stress.



8
9
10
11
12
13
14
15
# File 'lib/flaky/commands/stress.rb', line 8

def initialize(spec_location:, iterations: 20, seed: nil, ci_simulate: false, timeout: 600)
  @spec_location = spec_location
  @iterations = iterations
  @seed = seed
  @ci_simulate = ci_simulate
  @timeout = timeout
  @repo = Repository.new
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/flaky/commands/stress.rb', line 17

def execute
  env = {}
  env["FLAKY_CI_SIMULATE"] = "1" if @ci_simulate

  seeds = resolve_seeds
  passes = 0
  failures = 0
  failed_seeds = []
  start_time = Time.now

  puts "Stress testing: #{@spec_location}"
  puts "  Iterations: #{@iterations}, Seeds: #{seed_description(seeds)}, CI simulation: #{@ci_simulate}"
  puts ""

  @iterations.times do |i|
    elapsed = Time.now - start_time
    if elapsed > @timeout
      puts "\n\nTimeout reached (#{@timeout}s) after #{i} iterations."
      break
    end

    run_seed = seeds ? seeds[i % seeds.length] : rand(100_000)
    cmd = "bundle exec rspec #{@spec_location} --seed #{run_seed} --format progress 2>&1"

    output = nil
    success = nil
    IO.popen(env, cmd) do |io|
      output = io.read
      io.close
      success = $?.success?
    end

    if success
      passes += 1
      print "\e[32m.\e[0m"
    else
      failures += 1
      failed_seeds << run_seed
      print "\e[31mF\e[0m"
    end
  end

  total = passes + failures
  rate = total > 0 ? (failures.to_f / total * 100).round(1) : 0

  puts "\n\n#{total} runs: #{passes} passed, #{failures} failed"
  puts "Failure rate: #{rate}%"

  if failed_seeds.any?
    puts "Failed seeds: #{failed_seeds.join(', ')}"
    puts "\nTo reproduce a specific failure:"
    puts "  FLAKY_CI_SIMULATE=#{@ci_simulate ? '1' : '0'} bundle exec rspec #{@spec_location} --seed #{failed_seeds.first}"
  end

  @repo.insert_stress_run(
    spec_location: @spec_location, seed: @seed.is_a?(Integer) ? @seed : nil,
    iterations: @iterations, passes: passes, failures: failures,
    ci_simulation: @ci_simulate ? 1 : 0
  )

  exit(failures > 0 ? 1 : 0)
ensure
  @repo.close
end