Topic: Performance tests with Ruby-Prof and fixture questions

I am using some articles I found on using ruby-prof and functional tests. My goal is to work out my application and find areas to improve performance.
However, I am not terribly familiar with rails testing and fixtures. What I expect to be happening is not happening.

In my case I expect that the data in my users.yml fixture will be inserted into the database and be available to User.find(:xyz) methods. However, my assertion always fails because the User.find_by_remember_token("test_remember_token") always returns nil. So I assume that means my fixture data is never making it into the database, which I thought the 'fixtures: users' statement should do.

Can someone please let me know where my understanding is wrong?

Thanks in advance.

Here is relevant users.yml content:

dave:
    id: 1
    username: dave
    email: dave@example.com   
    remember_token: "test_remember_token"

Here is my functional test source: DashboardControllerTest:
require File.dirname(__FILE__) + '/../profile_test_helper'

class DashboardControllerTest < Test::Unit::TestCase
  include RubyProf::Test
 
  fixtures :users

  def setup
    @controller = DashboardController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new   
  end

  def test_get_index
    user = User.find_by_remember_token("test_remember_token")
    assert_not_nil user, 'no user with remember_token'   
  end
end

Re: Performance tests with Ruby-Prof and fixture questions

Did you ever figure this out?  I seem to see similar behavior right now.

Additionally, if I use the access method for the fixtures ( users(:dave) in your case ) I get an error message: undefined method '[]' for nil:NilClass which occurs in fixtures.rb:882.


When I look at the code for ruby_prof/test.rb it appears to try and call setup during run_test.  However, if I add a little debug to fixtures.rb I find that setup_fixtures is never getting called (this initilizes @fixture_cache my nil error above).
 

    def run_test
      begin
        setup
        yield
      rescue ::Test::Unit::AssertionFailedError => e
        add_failure(e.message, e.backtrace)
      rescue StandardError, ScriptError
        add_error($!)
      ensure
        begin
          teardown
        rescue ::Test::Unit::AssertionFailedError => e
          add_failure(e.message, e.backtrace)
        rescue StandardError, ScriptError
          add_error($!)
        end
      end
    end

As a hack I tried to force ruby_prof/test.rb to call setup_fixtures in the run_warm method of
    def run_warmup
      print "\n#{self.class.name}##{method_name}"
      setup_fixtures # Hack - added this line to force fixtures to load

      run_test do
        bench = Benchmark.realtime do
          __send__(@method_name)
        end
        puts " (%.2fs warmup)" % bench
      end
    end


My profiled unit tests now find fixture data.  If anyone can explain why I needed the hack I'd appreciate it.

Last edited by gwinklosky (2009-05-08 11:46:53)

Re: Performance tests with Ruby-Prof and fixture questions

I have this problem too, tried your solution and the fixtures get loaded. However it also prints "WARNING:  there is already a transaction in progress".

Have created an issue on GitHub.

Thanks for posting your hack. This was a real headscratcher for me.