Find your slowest-running cucumber features

Our feature set has 275 scenarios with 1175 steps, and it’s growing every day. In total it takes about 11 minutes to run the full set of features. We wanted identify the 20 slowest scenarios so that we could do some refactoring both in our code and tests to make things run a bit faster.

Add the following code to your features/support/env.rb file. All we do is catch each scenario using the Around function and grab the execution time for each scenario. Then display he results at the end of your build process.

scenario_times = {}

Around() do |scenario, block|
  start = Time.now
  block.call
  scenario_times["#{scenario.feature.file}::#{scenario.name}"] = Time.now - start
end

at_exit do
  max_scenarios = scenario_times.size > 20 ? 20 : scenario_times.size
  puts "------------- Top #{max_scenarios} slowest scenarios -------------"
  sorted_times = scenario_times.sort { |a, b| b[1] <=> a[1] }
  sorted_times[0..max_scenarios - 1].each do |key, value|
    puts "#{value.round(2)}  #{key}"
  end
end

This will give you output that looks like this:

------------- Top 20 slowest scenarios -------------
9.49   features/client_mapping.feature::Map clients with invalid upload file
8.42   features/client_mapping.feature::Map clients with valid upload file
7.35   features/indicators.feature::Show indicators after client upload
4.50   features/manage_clients.feature::Delete client
..
..
..

Hope this helps someone.

Advertisements

4 thoughts on “Find your slowest-running cucumber features

  1. In case anyone’s still finding this via Google, line 6 above will bomb if you’re using example tables. You’ll need to do something like the following to handle that case:

    if scenario.is_a? Cucumber::Ast::OutlineTable::ExampleRow
    thing = scenario.scenario_outline
    else
    thing = scenario
    end
    scenario_times[“#{thing.feature.file} #{thing.name}”] = Time.now – start

    1. Thanks, it great 🙂

      updated working example derived from above:

      if scenario.is_a? Cucumber::Core::Ast::ScenarioOutline
      thing = scenario.scenario_outline
      else
      thing = scenario
      end
      scenario_times[“#{thing.feature.file}::#{thing.name}”] = Time.now – start

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s