How can I speed up Ruby/Rake task - ruby

rake --tasks takes about 18s to run. This is just the time it takes to load all the tasks, as a result any task I define will take at least this amount of time to run :
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
My Rakefile :
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
Any idea why rake takes to much times ?
Thanks

Try spring
Command line will look like:
spring rake -T
It will take more time running the first time, but subsequent runs will be very fast.

This solution worked for me: Faster rake tasks in Rails.
I had to do a little variation where I created a lib/tasks/no_rails directory and put all the Rake files which do not need Rails in there and loaded only those using the above method.

I like the solution Pratik mentions for the general case of loading rails for tasks that need it and not for those that don't, for any rake task without having to remember beforehand.
A less-invasive method to run a rake task that doesn't need rails is to use the -f rake option to tell rake to use a particular Rakefile. This way, rake won't go looking for rake tasks in all of rails.
For example, assuming your task above is in a file called Rakefile at the top level of your project and your Rakefile doesn't do anything that loads Rails like require File.expand_path('../config/application', __FILE__), you can do:
$ rake -f Rakefile spec
and it should run your spec task much faster. Try $ time rake -f Rakefile -T; I did this with a rails-independent Rakefile of mine and got:
real 0m1.543s
user 0m1.308s
sys 0m0.201s
The downside is you have to remember to specify this option every time, and not to specify it if you want to run a rake task from rails like rake db:migrate.

The entire rails environment has to be loaded, therefore even simple rake tasks such as rake --tasks take a while. Opening a console with rails console or script/console takes a similar time. You may try to hack Ruby or Rails to speed up rake, but too much optimization can be bad if you want to switch to a newer version later. Since the rails environment must be loaded, cleaning up routes may also help.

Related

Ruby: run rake task to execute ruby scripts

I have a bunch of Ruby scripts and I'd like to start them with a Rake task.
A simplified version to illustrate my issue:
export_stats.rake:
desc 'Export statistics'
task :export_stats do
puts "executing: export_stats.rb #{START_MONTH} #{END_MONTH} #{OUTPUT} #{ENVIRONMENT}"
ruby "export_stats.rb #{START_MONTH} #{END_MONTH} #{OUTPUT} #{ENVIRONMENT}"
end
rake aborted! elk-stack/export_stats.rake Don't know how to build task
'export_stats'
the export_stats.rb file is in same directory with export_stats.rake
the rake gem is installed and if I run
rake export_stats
I get an error:
rake aborted!
Don't know how to build task 'export_stats'
What am I missing?
If I understand you correctly you have a folder with some ruby scripts and you are trying to run a rake task that is located in the same folder. I assume you are not using any application framework like Rails (because you did tag the question only with "Ruby").
Do you have a Rakefile in same directory? If so does it contain a statement to load the specific files to run?
# Rakefile
#!/usr/bin/env rake
load 'export_stats.rake'
You have a typo.
The code says export_stats, and the error says exports_stats.
There's an extra s.
Read the error message carefully! ;)

Load Cucumber tests but don't execute them

I'm very new to Ruby, so, sorry if the answer is obvious.
Given
Existing project which consists exclusively of Cucumber tests (features). The project has a Gemfile, and env.rb under features/support which appends project directories to $LOAD_PATH and requires several libraries. I can run these tests by executing
bundler exec cucumber -r features
Question
I want to be able to load the support files into a REPL (say, pry), for the purposes of code inspection. In other words, I'd like to create a console -like script that loads up all the code that is used in the tests, but doesn't execute the tests. I need this for the editor that uses this kind of REPL for things like code completion, navigation, refactoring etc. Since this "application" doesn't have anything resembling an entry point, I'm at a loss as to how to create one. My efforts to require all files in the support directory so far had been unsuccessful, in particular, due to the use of World() top-level method, which I believe is defined by Cucumber.
I'm not quite sure what you're trying to accomplish but if you have say pry gem loaded in your Gemfile in test group, you should be able to just throw in a binding.pry command inside any test to inspect the code / environment.
Might look something like this:
Gemfile
group :development, :test do
gem "pry"
end
You could also use a rake task and load the environment taking the same approach.
lib/rake/sometask.rake
namespace :load do
desc "inspect code"
task :code, => :environment do
binding.pry
end
end
and run bundle exec rake load:code for example

travis.ci keeps on failing when running "bundle exec rake"

I am trying to get automated testing sorted out with Travis.ci. However, at the moment the build keeps on failing when trying to execute bundle exec rake.
This is what I see...
$ bundle exec rake
rake aborted!
Don't know how to build task 'default'
/home/travis/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/travis/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
(See full trace by running task with --trace)
The command "bundle exec rake" exited with 1.
Done. Your build exited with 1.
My unit tests are in the test folder in the main directory and is named test_np_search.rb. I understand that I am somehow supposed to point travis to this location in order to run the unit tests but I have no idea how to do this.
I have read the ruby related documentation on travis.ci a number of times and have looked online for tutorials, however I have been unable to get this to work.
The whole github repository in question is here: https://github.com/IsmailM/NeuroPeptideSearch
The Travis.CI link is here: https://travis-ci.org/IsmailM/NeuroPeptideSearch
I have been trying to get this sorted for over a week now with any success and so would be highly grateful if anyone could help me.
Many Thank
if you want to execute bundle exec rake on travis, you will have to make sure, that it runs on your local machine!
if you call rake without providing a task-name, it will assume that you want to run the default-task.
if you want to run your minitest testsuite as a default-task, you have to do this:
require "rake/testtask"
Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
end
task default: :test

how to set profiles using parallel_tests gem

How do I specify a cucumber profile when using the parallel tests gem?
If I were to launch cucumber from the command line (using Jruby) I would do
Jruby -S cucumber -p profile_name
and using rake tasks I would set the tasks profile t.profle=myprofile and then execute using rake mytask
so when using parallel_tests I launch the tests using the built in rake tasks associated with the gem : rake parallel:features is there any way to pass in t.profile argument into the rake task on execution?
What i would like to do is something along the lines of
rake parallel:features[4] t.profile=myprofile
This may or may not be useful, but in the cucumber.yml, you can just add a parallel: profile and it will get picked up automatically. You can specify one on command line but I didn't bother trying to figure it out because I don't really have the need to switch profiles.

How do I create a global rakefile on Windows 7?

I want to be able to create a rakefile on Windows 7 that I can call with the rake -g <taskname> or rake --system <taskname> command. Where do I save the rakefile? I've tried creating a Rake directory under my user directory (c:\users\me\rake), but when I call rake -g hello_world, rake errors out saying it doesn't know how to build the :hello_world task, which makes me think it just can't see the rakefile. Here's what my global rakefile looks like:
require 'rake'
desc "a hello world task"
task :hello_world do
puts "hello from your global rakefile"
end
Figured it out - turns out Rake expects global rakefiles to end in a ".rake" extension. I changed the example above to this:
require 'rake'
desc "a hello world task"
task :hello_world do
puts "hello from {__FILE__}"
end
And saved it as "testing.rake" in my c:\users\me\rake\ directory. I opened up a command prompt to a random directory and ran "rake -g hello_world" and got this output:
C:\Windows\system32>rake -g hello_world
(in C:/Windows/system32)
hello from C:/Users/me/Rake/testing.rake
Rake looks for Rakefile in the current directory unless specified explicitly as in:
rake --rakefile C:\users\me\rake\[rakefile]
Rake on Windows looks for $HOME/Rake/*.rake if the target cannot be found locally. If HOME is not set, then it will use c:\Users\me\Rake as the other responders have indicated.
But, if you happen to have HOME set (as I do, operating in a mixed Cygwin/Windows 7 world), now you know where rake will be expecting to find your global rake files.
Btw, I don't find it necessary to pass the -g flag; Rake seems happy to find the global definitions on its own. I think the only case in which -g is warranted is if you need to be absolutely sure there is no rake task of the same name locally.

Resources