ruby include trying too many locations - ruby-on-rails

I'm helping someone diagnose some slowness with their RoR applications. I'm not that deep into ruby development, but by running an strace on the ruby process, I noticed the following oddity:
For presumably every .rb file included, the ruby process checks about 110 different locations for the file to include, with all but 1 returning ENOENT. These paths include many non-existent permutations on local directories and gem installation directories. Ironically, the 8th location attempted finds the correct path, but ruby then goes on to try another 100 invalid locations before going back and opening the file it had found at the beginning of the search.
The whole search process takes almost 30ms per included .rb file, which cumulatively means that page takes so long to load that the site is unusable.
I've posted a sample of the strace output in case it is helpful here: http://pastebin.com/t9LD89dr
So, why is it doing this, and how do I make it stop?

This is a known problem with Ruby's require... apparently they're going to fix it with Ruby 1.9.3.
So, this slowness is hard to avoid when spinning up a Ruby app.

Related

/usr/bin/ruby file just contains gibberish and the word 'ELF'; safe to delete this/is this the file I really need?

I'm trying to install the Ruby gem foreverb, to keep my server process running when SSH disconnects, and also for improved understanding of the people visiting my site.
On following this guide:
https://github.com/DAddYE/foreverb
It mentioned to write some code in /usr/bin/ruby so I dutifully opened the file and looked inside. Instead of what I expected (either a blank file or something that looks like Ruby code) I was instead greeted with this:
^?ELF^B^A^A^#^#^#^#^#^#^#^#^#^C^#>^#^A^#^#^#à^H^#^#^#^#^#^##^#^#^#^#^#^#^#^P^Q^#^#^#^#^#^#^#^#^#^##^#8^# ^##^#^[^#^Z^#^F^#^#^#^E^#^#^##^#^#^#^#^#^#^##^#^#^#^#^#^#^##^#^#^#^#^#^#^#ø^A^#^#^#^#^#^#ø^A^#^#^#^#^#^#^H^#^#^#^#^#^#^#^C^#^#^#^D^#^#^#8^B^#^#^#^#^#^#8^B^#^#^#^#^#^#8^B^#^#^#^#^#^#^\^#^#^#^#^#^#^#^\^#^#^#^#^#^#^#^A^#^#^#^#^#^#^#^A^#^#^#^E^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#¼^K^#^#^#^#^#^#¼^K^#^#^#^#^#^#^#^# ^#^#^#^#^#^A^#^#^#^F^#^#^# ^M^#^#^#^#^#^# ^M ^#^#^#^#^# ^M ^#^#^#^#^#p^B^#^#^#^#^#^#x^B^#^#^#^#^#^#^#^# ^#^#^#^#^#^B^#^#^#^F^#^#^#¸^M^#^#^#^#^#^#¸^M ^#^#^#^#^#¸^M ^#^#^#^#^#Ð^A^#^#^#^#^#^#Ð^A^#^#^#^#^#^#^H^#^#^#^#^#^#^#^D^#^#^#^D^#^#^#T^B^#^#^#^#^#^#T^B^#^#^#^#^#^#T^B^#^#^#^#^#^#D^#^#^#^#^#^#^#D^#^#^#^#^#^#^#^D^#^#^#^#^#^#^#Påtd^D^#^#^#<98>
^#^#^#^#^#^#<98>
^#^#^#^#^#^#<98>
Not entirely sure what it is, but pretty sure it shouldn't be there. Is it file corruption, as it doesn't look like what I'd generally consider to be file corruption? While I assume it's probably not doing anything useful at the moment, does the presence of semi-arbirary strings in this file represent a problem with my Ruby installation which I need to fix? Is there perhaps another location or set of locations where the right file might be, and perhaps this one isn't being used?
I notice I have a lot of problems with my server, and often find I have to randomly reinstall gems which seem to disappear from my system, usually after the SSH disconnects. Could this file be something to do with that?
It seems odd to me that if this is the location of an important Ruby system file that my Ruby installation should work at all, but it does seem to... Might it be left over from a previously broken installation? I'm only just progressing out of the beginner stages with Ruby, so any insight into what the file does, why it's important and, particularly, how I should progress would be extremely helpful. Also, any suggestions for further reading on Ruby framework eco system which might help me better understand the inner working of stuff like this would hopefully save me from having to take up people's time here with similar questions in the future, though I understand that's a little outside the usual remit for the site.

How can I find out what prevents a rails app from loading a page?

I have a rails which seemed to be working previously but now after some changes when I go to the root page it takes it infinitely to load it, it just doesn't load it. There're nothing useful in the console either. How can I find out what prevents it from loading the main page? Is it about profiling?
Check your Rails logs, eg. development.rb. You can put logger.info, or puts statements in your environment.rb, development.rb and application.rb files to see how far Rails is getting in the boot process. You can also create a dumb initializer named 00_start_init.rb with a logger.info or puts statement to see if you're getting as far as initialization. I've found that useful before.
To really understand where you application is hanging, you need to understand the Rails initialization process. Here is the documentation for Rails version 4.2. http://guides.rubyonrails.org/v4.2/initialization.html. Similar documentation exists for every version of Rails. You can take advantage of understanding the boot sequence by placing log statement at various point in the process.
I'm assuming you're in the development environment. If so, and the console loads, it's likely not a configuration problem. It's more likely a problem with your controllers or models. If the console won't load to a prompt, then it's likely a configuration problem in application.rb, development.rb, an initializer, etc.
You mention profiling, but provide no details about it. I can't even guess what you're referring to, so the answer is "maybe?". If you can post the code changes you made since the app last loaded in the browser, that would make it much easier to help you trouble-shoot.

rails console yaml parse error - applying solutions

BACKGROUND Complete newbie to coding working thro’ RailsTutorial on a windows 7 PC– I’ve stalled on chap 4.2 with an YAML error running “rails console” from a command prompt with Ruby on Rails.
I need tips on how to interpret and use some specific answers (below) that I found posted on stackoverflow/google groups/github.
I’ve tried to meet forum criteria for questions. If any of my confusions are too basic here please let me know which & where else I might try.
The error -
~\Sites\sample_app rails console
C:/a_installers/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/psych.rb:154:in 'parse
: (<unknown>): couldn't parse YAML at line 44 column 11 (Psych::SyntaxError)
from C:/a_installers/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/psych.rb:
54:in 'parse_stream'
(40 more lines like this)
Not sure what useful context I can supply.. possibly that I used Railsinstaller and later Pik (instead of RVM) on Windows 7, that I installed ralis/ruby programs under c:\a_installers\railsInstaller and that I’ve updated PATH to include ruby bin folders.
QUESTION The advice I'm having difficulty interpreting/using is..
rails error, couldn't parse YAML suggests
a. Run the YAML code through yamlint.com
Which YAML file? All those in the error listing? I think I’m running rails console from my installed rails program area (above), there I found 60 odd files with “ *.yml “ – do I have to run all these through YAMLlint.com?
b. Manually Fix the YAML code
One example given was " fixing invalid yaml " involving splitting yaml code for a local date into lines.This was not applicable – I couldn’t find any such code in any of the 60 odd “ *.yml “ files under railsinstaller on my system
c. “load the old YAML parser (syck) with code xxx in config/boot.rb”
Two problems…
(1) I couldn’t find boot.rb in my the rails/ruby program area
(2) I’m torn between two contrary views expressed
“Psych is the new one, the one you should be using you have some invalid yaml somewhere.”
And
“wrong… ruby should use a parser that is both maintained and supports existing usage, even if that usage is further from the spec. If only there were such a thing. Until there is, they should use syck..”
In summary, not sure which files to amend, how to do that, how to check that. if it’s advisable instead to do a workaround (reverting to syck) and if so in which directory and with which files.
another source rails-yaml-config-best-practice advised
“configure parameter inside environment.rb.” use a github code called “settings logic”
Not sure how to do this..do I need to learn YAML? Tutorial hasn’t even started Ruby yet – is this
Not sure where to do this – couldn’t find environment.rb in my rails/ruby program area – it seems to be part of my application (sample_app)
In ..Settings_logic …the notes seem to bring me full circle to 1. above.
“..Note: Certain Ruby/Bundler versions include a version of the Psych YAML parser which incorrectly handles merges (the << in the example above.) If your default settings seem to be overwriting your environment-specific settings, including the following lines in your config/boot.rb file may solve the problem:
require 'yaml'
YAML::ENGINE.yamler= 'syck'
To apply this solution I still need to resolve question 1 c (2)
3 General advice - My inability to start rails console seems an absolute barrier for me to use ROR- a couple of general questions - Do practitioners use rails console for actual development or just learning? Can I learn RoR without rails console?
Many thanks for your patience, time and attention.

Rails load error (no such file to load) after many requests

I'm using Apache2 and Rails to host a pretty simple API that uses this: https://github.com/geocommons/geocoder
The other "load error (no such file to load)" problems I've seen online seem to happen at all times and typically in relation to gem files, but my API will get through 1000+ requests before it has this problem. The file it is unable to load is a controller in my appname/app/controller directory.
After restarting apache the API will work for roughly 1000 more requests.
Error from log file:
Started GET "/api_uri?[parameters]"
for 127.0.0.1 at [timestamp]
LoadError (no such file to load --
[dir]/app/controllers/controller_name.rb
The stack trace says something very similar, I can post it if necessary.
I've made sure all of my ruby files require "rubygems", I've made sure my path includes GEM_PATH, GEM_HOME and RUBYOPT in both my environment and my .bashrc file.
Really baffling to me since it appears to work fine at first. Any ideas?
Update: A memory leak! The Ruby process slowly climbs to ~10.5% memory usage as the requests continues, and then requests cut out. Hm
The problem here was that the Geocoder gem was not closing transactions or database connections. Fixed the code and things are now working as expected.

rails ruby-prof and benchmark testing

I'm running Rails 2.2.2. I've read a few articles about ruby-prof and profiling a rails app. And I'm confused as to how things are really working.
I was originally using this tutorial
http://snippets.aktagon.com/snippets/255-How-to-profile-your-Rails-and-Ruby-applications-with-ruby-prof
to profile my app, and it works. This involves writing your own profile environment and running > RAILS_ENV=profiling ./script/performance/request -n 100 profiling/homepage.rb
So here's my confusion. Somehow, this runs ruby-prof and opens up all the stats etc, but I can't see anywhere where ruby-prof is ever actually called.
So then I read further, and it seems 2.2.2 has benchmarking/profiling built in. So I write a test file in the performance section like so
require 'test_helper'
require 'performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionController::PerformanceTest
def test_worksheet
get '/reduction/worksheet'
end
end
and run
rake test:profile
Is this equivalent to what I was doing above, but just now it's integrated into the whole rails framework?
My next question is this. The original script ouput a flat file and html file, but I couldn't figure out how to also get a tree file automatically to open up with KCacheGrind, or in my case MacCallGrind. Can I add formats into my script call?
Edit: running the scripts through rake test seem to actually produce a tree file, great. Mac CallGrind however seems to hang when trying to parse it. Anyone know other tools for viewing these tree files?
Turns out, it's exactly the same, I must have been reading an old tutorial. All the benchmarking and profiling is now in Rails which is awesome. And it outputs txt,html and tree files, also awesome. Now if Only MacCallGrind would work, there's no way I'm installing the 4gb of KDE packages needed just to view a tree file.
Brad, you should contact the author of MacCallGrind and see if there's an update, or whether he/she has a fix for your files.
Oh that's right, that's me. Please email at richard at symbol aggmedia.net, as there is an update about to come out, and I'd love to have Ruby/Rails support available.

Resources