I've been using ZenTest to run all the tests in my Rails project for years and it's always been quite nippy. However, on my Mac it has suddenly started taking 3 times as long to run all the tests. We have 1219 tests and for the past year it would run all the tests in around 300 seconds on average. Now though, it's taking almost 900 seconds:
Finished in 861.3578 seconds.
1219 tests, 8167 assertions, 0 failures, 0 errors
==============================================================================
I can't think of any reason why such a slowdown would occur. I've tried updating to the latest gem version, reducing the log output from the tests and regenerating the test database, all to no avail. Can anyone suggest a way to improve the performance?
When you have eliminated the impossible, whatever remains, however improbable, must be the explanation: if it's not the gem, not the database (did you check indexes ?), not your Mac, not Rails (did you upgrade recently), could it be the code ?
I'd check git/svn/cvs logs for the few most recent changes you made, and look for anything that might e.g. be slowing down queries.
If you can't find anything right away, profile the code to see where the time is going. This will be slower than just remembering something you did change (which almost always turns out to be the explanation in this kind of situation), but might point you in the right direction.
Performance issues can be frustrating because any number of factors can have an impact. A missing index on the DB. Network latency. Low memory conditions. Don't give up, keep Tilton's Law in mind.
You are really going to do a little bit more homework here, I doubt its ZenTest:
Grab a version of your code when stuff was great and dandy a few months ago. Run all the tests, output all the test durations to a spreadsheet or something.
Grab a current version of your code base and repeat the process in 1)
If durations are the same, something about your DB configuration or machine config has changed
If all the tests are slower on average this is a hard one to diagnose, but it would seem that there is a new bit of code thats running in each test.
If a handful of new tests are really slow, fix them.
So I finally solved this. Here's how in three easy steps:
Insert OSX Leopard CD
Completely reinstall Leopard from scratch
Reinstall Ruby, MySQL etc
After doing this the tests run in under 260 seconds.
I have no idea what happened but it certainly seems to have been a MySQL issue somewhere.
Related
My RSpec examples were running respectably until I migrated to a new Mac. Now, it seems the 1st test will take 1-3 minutes. The next time the tests are run (in a different order) that same test might take only 0.2 seconds.
rspec -p will show me which examples are slow that round but it doesn't help me debug WHY.
It might be a call to an outside service or, perhaps some external volume that's timing out because it's no longer attached to this machine, but I can't think of what or where the reference might be... other teammates testing same codebase don't have this problem.
Thoughts?
Edit (adding image of profiling results)
Does this help? I have no idea what cycle 5 means...
I'm running a ruby on Rails service. Ruby 2.0 and rails 4.0 The job on heroku takes ~2x the time it does on my local machine. I would expect the processing elements to actually run faster on the big *#!# server than on my macbook air.
sorry for the lousy formatting Times are in seconds 1.00000
Herok Macbook Air delta
job_id=Job.where(id:user.id, active:true).pluck(:id)[0]___t1=0.003 _t2= 0.126_delta=0.123
{job Prep}_____________________________________t1= 0.00_t2=0.00_delta=0.00
imagedata=Jobimage.find_by(job_id: job_id, level:0)___ t1=0.349 t2=0.044 delta=0.305
image=Marshal.load(imagedata.data) _______________t1=0.374 t2=0.096 delta= 0.278_
Job processing ________________________________t1= 0.51 t2=0.328 delta=0.182
render json:answer.to_s_________________________t1=0.51 t2=0.328 delta0.182
Except for the Job -processing block, these are statements bracketed by logging statements
I'm really surprised by 4 and 6
Can anyone explain why Heroku would be getting the stuffing kicked out of it my my notebook
thanks
You're one of many (hundreds? thousands?) of people sharing that physical box, and Heroku has no incentive to make your code fast for a free instance. Your Macbook Air is doing (more or less) one thing.
I would look into the delayed_job gem. Judging by the things you have timed in your breakdown, I don't think it will apply directly to what you are trying to accomplish, but it might be useful in a different part of your project.
My 3.1.3 rails app takes quite a while to start up, and even running rails console seems to take longer than it reasonably should. For example, with my app it's 50 seconds from rails c to the command prompt. In a test fresh rails app (e.g. from rails new) it's about 5 seconds.
Needless to say, this is really annoying, particularly when trying to run tests, etc.
I've seen the links at https://stackoverflow.com/a/5652640/905282 but they're pretty involved; I was hoping for maybe something that would be at a higher level, like "oh yeah, here's how long each gem is taking up during startup".
Suggestions, or do I just need to dive into the details?
Ruby 1.9.3 fixes a performance problem in 1.9.2 when a large number of files have been loaded with require.
That post describes how the performance of including new files is O(N), getting progressively slower the more files are already loaded. Since Rails loads in a lot of files, it is a serious drag on start-up time.
I have spent hours and hours trying to configure spork so that it works for RSpec, works for Cucumber, reloads models so that it doesn't have to be restarted all the time and doesn't throw errors.
I've spent so much time researching solutions to its quirks that I might as well just have waited for the regular tests to load. Added to all of that it has the annoying characteristic that when I'm debugging I type commands into the terminal window I called Rspec from but the output gets displayed in the terminal window Spork is running in. Eesh.
I'm hugely appreciative of any piece of software that is produced for the help of others and of the spork project but just can't figure out whether it's worth labouring through further.
EDIT
YES - SPORK IS DEFINITELY WORTH THE EFFORT. After 4 days of setup I finally managed to sort out all of the issues and it's speeded up my testing incredibly. I really thoroughly recommend it.
I found out that Spork seems to work mostly OK if you follow the TDD/BDD pattern - that is, you write your test first, let it fail, and only then write the code. However, I don't always work this way - there are many situations where I need to write the code before writing the tests.
Fortunately, I found a nearly ideal solution to my testing needs - the Spin gem. It doesn't force you into into any workflow, and just works.
Give my CoreApp ago - it's a complete config of RSpec/Spork/Guard/Cucumber.
I find it's worthwhile considering it speeds up mosts test but the disadvantage then is my tests aren't engineered to be 'efficient' themselves. Some believe it's better to wait for the environment to load each time, but on my MBP it takes over 10-15 secs for the env to reload.
https://github.com/bsodmike/CoreApp
So I have tried this out on multiple computers with multiple setups (servers/apps) and I seem to consistently get Rails completing 8-15 requests per second even for doing selects on empty tables with 1 field. I think I'm doing something wrong here because I've read a lot of stats online where people are getting 60-200 with mongrel. So being down at 8 seems just awful. The first app I tested this on was a little more involved and had 2 queries in 1 controller but they were just selecting a few rows, not a big deal.
Is there some trick to this I don't realize? Ruby.exe is taking up nearly 50% of my cpu cycles but still this is pretty bad. I feel like I've tried this when messing with rails last year and have gotten something like 50 requests per second. Is it possible that routing is screwed up some how?
Any advice would be greatly appreciated. Even info as far as profiling tools go so I could at least figure out WHERE the problem is occurring.
Thanks ahead of time.
If you're on windows then that seems about right. Rails runs terribly slow on windows. Try running it on a linux box, or a mac if you have one. You could also try heroku. They have a free starter plan you can use for development.
If you must run in a windows environment you could try jruby for some extra speed.