Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 how to speed up json rendering

I have 1500 small objects to render for a webservice inside a rails 4 application. I use json as format with jbuilder for templates. I already changed the json engine to oj in the app initializer:

 require 'oj_mimic_json'
#MultiJson.use :yajl

Oj.mimic_JSON

# jbuilder json templates
Jbuilder.key_format camelize: :lower

a single rendered json object looks like this:

center: {lat: 45.962153536249, lon: 7.68207088549831}
lat: 45.962153536249
lon: 7.68207088549831
n: "Zermatt-Cervinia"
st: 80
sy: 0


Rendered json_partials/_snow_in_resort.json.jbuilder (0.5ms)
Rendered json_partials/_snow_in_resort.json.jbuilder (0.5ms)
....
Rendered resorts/find.json.jbuilder (4213.4ms)
Completed 200 OK in 4351ms (Views: 3924.3ms | ActiveRecord: 306.8ms | Solr: 0.0ms)

But still I need 150 ms for 101 kb on my localhost which is way too slow for the task I want to accomplish on the UI. What do I have to do to speed up here? Which things should I check? I appreciate help. Best, Philip

update

I optimized my active record queries down to ActiveRecord: 77.8ms , nevertheless the view rendering is still too slow

like image 585
dc10 Avatar asked Oct 24 '14 16:10

dc10


1 Answers

You can explore using HTTP Caching using Varnish. Here's a great article describing different caching techniques (Fragment Caching, HTTP Caching, etc. ) in Rails 4. It has good explanation on caching JSON responses.

Rails 4 and Caching

http://www.slatestudio.com/blog/p/caching-in-rails-4-applications

Here's another on Rails and Varnish

http://www.hward.com/scale-rails-with-varnish-http-caching-layer/

lacquer a popular gem with drop-in support for varnish caching in rails

If you want to get into little more detail on what is HTTP caching, Here's a really good writeup

https://www.mnot.net/cache_docs/

Ryan Bates has a excellent tutorial on Rails Cast but it's a Pro Episode

http://railscasts.com/episodes/321-http-caching

UPDATE

Geocoder Gem

Based on some comments below, I would suggest looking into using the Geocoder gem. It's been around for a while and is very well optimized for Points of Interest search, like what you are trying to do. It also does lot more than that.

Spatial Index If you have already tried it, and are not satisfied, can you please post some details on what kind of optimizations you are using on your database to speed up the query? You can significantly speed up POI queries by using spatial indexes on the table?

Here's a good article on spatial indexes:

http://daniel-azuma.com/articles/georails/part-6

Some performance testing ideas

You might be able to test if it is indeed the rendering that is slowing you down by coming up with a good test case. Try querying for things towards top, middle and bottom of your points table. Also for different number of response objects in your JSON, and different number of properties in your JSON object. Right now I see, that lat, lon is redundant. Try removing them and compare times for huge number of results, if it is indeed the rendering that is slowing you down, fewer the properties, faster responses you should see.

Also, if your properties, (name, st, sy etc.. ) are coming from relationships instead of same table as points, try de-normalizing your DB to see if you get faster view rendering..

like image 176
Shaunak Avatar answered Oct 21 '22 12:10

Shaunak