zombocom/derailed_benchmarks

Rails not being loaded in rake task?

Closed this issue · 6 comments

I'm using Ruby 2.1.5+Rails 4.2.0 and derailed_benchmarks 1.0.0.
I wanted to do some tests in a Rails app that uses Devise for authenticated the User class.
I tried creating a perf.rake such as the one bellow, with and without the namespace block but I always get the error bellow. It seems that rails is not being load. Guess that I'm missing something simple..?!
Any clue?

require 'bundler'
Bundler.setup

require 'derailed_benchmarks'
require 'derailed_benchmarks/tasks'

namespace :perf do
  task :rack_load do
    DERAILED_APP = Rails.application # your code here
  end
end

DerailedBenchmarks.auth.user = User.find_or_create!(email: "a@example.com")
$  bundle exec rake -f perf.rake perf:ram_over_time --trace
rake aborted!
NameError: uninitialized constant User
/Users/sergio/work/capthost/perf.rake:13:in `<top (required)>'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/rake_module.rb:28:in `load'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/rake_module.rb:28:in `load_rakefile'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:689:in `raw_load_rakefile'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:94:in `block in load_rakefile'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:93:in `load_rakefile'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:77:in `block in run'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/bin/rake:23:in `load'
/Users/sergio/work/capthost/vendor/ruby/2.1.0/bin/rake:23:in `<main>'

You don't need the rack_app block, it will never be called.

Rails isn't loaded when the rakefile gets loaded, it gets loaded once you call a task. So you're trying to use a constant before it's been defined. Unfortunately my docs in the readme are wrong. I thought I added an entry point where these things could be defined, but I don't see it in the code. Here's a short term workaround you could use

class MyAuthHelper < DerailedBenchmarks::AuthHelpers::Devise
  def user
    @user ||= begin
       User.find_or_create!(email: "a@example.com")
    end
  end
end

DerailedBenchmarks.auth = MyAuthHelper.new

thanks @schneems , the workaround works ;)

just to let you know that I had to add the method "call" to MyAuthHelper as bellow (I modified the login_as line), or else it would fail with unauthorized request in the app's controllers "before_action: authenticate_user!" from devise.

class MyAuthHelper < DerailedBenchmarks::AuthHelpers::Devise
  def user
    @user ||= begin
       User.find_or_create!(email: "a@example.com")
    end
  end

  # Logs the user in, then call the parent app
  def call(env)
    login_as(user, scope: :user)
    app.call(env)
  end

end

DerailedBenchmarks.auth = MyAuthHelper.new

well, it's not exactly the same.. I'm using

login_as(user, scope: :user)

You can now do DerailedBenchmarks.auth.user = -> { User.find_or_create!(email: "a@example.com") } on the master branch. Are you OK to close this @bitcoder?