Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:18:38+00:00 2026-06-01T04:18:38+00:00

I’m trying to speed up a large RSpec project’s tests. In addition to using

  • 0

I’m trying to speed up a large RSpec project’s tests. In addition to using RSpec’s --profile option I wanted to get the longest running test files [1] printed out.

In my spec_helper.rb I dump the classes being tested and total time to a file, however as we have spec/model and spec/request directories I’d really like to be able to print the current test’s filename and not just the class name (described_class), so that the user can disambiguate between model/foo_spec.rb and request/foo_spec.rb when optimizing.

In a before block in the spec/spec_helper.rb, how can I get the current test file’s filename?

My (heavily trimmed) spec_helper looks like this:

config.before :all do
  @start_time = Time.now
end

config.after :all do |test|
  timings.push({ :name => test.described_class,
                 :file => 'I do not know how to get this',
                 :duration_in_seconds => (Time.now - @start_time) })
end

config.after :suite do
  timing_logfile_name = 'log/rspec_file_times.log'
  timing_logfile = "#{File.dirname(__FILE__)}/../#{timing_logfile_name}"
  file = File.open(timing_logfile, 'w')
  timings.sort_by{ |timing| timing[:duration_in_seconds].to_f }.reverse!.each do |timing|
    file.write( sprintf("%-25.25s    % 9.3f seconds\n",
                        timing[:name], timing[:duration_in_seconds]) )
  end
  file.close
  tell_if_verbose("Overall test times are logged in '#{timing_logfile_name}'")
end

This doesn’t seem to be available in the curretn RSpec meta-data, but I’m hoping someone more familiar with the internals can think of a way to expose it. Thanks,
Dave

[1] Often a file with, say, 100 examples in it yields more speed up than a single example from –profile – when that large file’s before :each / before :all blocks are targetted, obviously even a ms saved is multiplied up by the number of tests in the file. Using this technique in addition to --profile helped me a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:18:40+00:00Added an answer on June 1, 2026 at 4:18 am

    As long as you’re just using this for profiling your tests to identify which files need to be improved, you should be able to toss this into your spec_helper.rb file (and remove it afterwards). I fully understand that this is not pretty/clean/elegant/acceptible in production environments and I disavow that I ever wrote it 🙂

    config.before(:each) do |example|
      path = example.metadata[:example_group][:file_path]
      curr_path = config.instance_variable_get(:@curr_file_path)
      if (curr_path.nil? || path != curr_path)
        config.instance_variable_set(:@curr_file_path, path)
        puts path
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.