Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can _know_ which JSON renderer is active in my Rails 3 app?

This is a direct follow-on to this question: What is the fastest way to render json in rails?

My app does a database query and render to JSON for a JS callback. It takes at least 8 seconds for a small (1 MB) dataset, and more like 20 for a large (3.5 MB) one. This is basically going to kill my app as an idea. My users aren't going to put up with this sort of wait.

I've read about multi_json and oj and yajl, and I think I've got them installed, but none of the ways I've tried to activate the various gems in my Gemfile show any improvement in serializing time. How can I prove that I'm using one over the other, so that I compare results between them? I can't find any way of outputting (to the Rails debug log or the JS console in the browser) which library might have gotten used for the actual 'render :json => @data' call.

like image 724
David Krider Avatar asked May 14 '12 18:05

David Krider


2 Answers

Instead of fiddling with your controller, a better way is to use the Rails console, like so:

$ rails console
Loading development environment (Rails 3.2.8)
1.8.7 :001 > MultiJson.engine
 => MultiJson::Adapters::JsonGem 

You can interact directly with your Rails stack that way.

like image 98
Duncan Bayne Avatar answered Oct 10 '22 23:10

Duncan Bayne


I finally figured out I could do 'render :text => MultiJson.engine' in my controller. This yielded "MultiJson::Engines::Oj".

It confirms that I'm already using the supposedly fastest JSON library, and I may be hosed. I guess I'll try to return pure text through the controller (which takes 2 seconds compared to 8) and see how fast a routine to convert that to a hash will take...

like image 31
David Krider Avatar answered Oct 10 '22 21:10

David Krider