3

I'm using Sinatra (1.3.2) with Mongoid (2.4.10). I'm noticing that it is taking a VERY long time to convert about 350 mongo documents to JSON.

I added a few benchmark wrappers just to see what is taking the most time:

get '/games' do
  content_type :text
  obj = nil
  t1 = Benchmark.measure { @games = filtered_games.entries }
  t2 = Benchmark.measure { obj = @games.as_json }
  t3 = Benchmark.measure { obj.to_json }
  "Query: #{t1}\nTo Object: #{t2}\nJSON: #{t3}"
end

(filtered_games just returns the results of a Mongoid query using parameters passed in the URL)

This is a typical response:

Query: 0.100000 0.000000 0.100000 ( 0.234351)

To Object: 3.560000 0.010000 3.570000 ( 3.569813)

JSON: 0.220000 0.000000 0.220000 ( 0.217941)

So, it looks like its taking the majority of its time just converting the Mongoid objects into a basic JSON structure (as_json) (over 3.5 seconds), NOT converting that structure into a JSON string.

The documents aren't terribly large (about 450 bytes, 15-20 fields per document).

I suppose what really confuses me is that the time it takes to perform the actual query to Mongodb, parse the response and deserialize it into Mongoid objects is MUCH faster..

Why is this? Any suggestions on how I can further optimize this? I suppose I could just use native calls to Mongo and return those results, but I'd like to be able to continue to use the scopes I've defined in Mongoid.

EDIT: I previously was not actually running the query in the first benchmark because of Mongoid lazy loading until the as_json call.

1 Answer 1

1

So, as it turns out, rolling back to a previous version of Mongoid fixed this issue. I'm guessing that this is because it pulls in an earlier version of Active Model or Active Support.

  • Mongo: 1.4.0
  • Mongoid: 2.3.5
  • Active Model: 3.1.6
  • Active Support: 3.1.6

These are the new benchmark results:

Query: 0.110000 0.010000 0.120000 ( 0.243558)

To Object: 0.200000 0.000000 0.200000 ( 0.196342)

JSON: 0.440000 0.000000 0.440000 ( 0.444311)

If I get a chance to dig down into the code, I'll try to come back and update with anything I find.

1
  • Please do, I am having the EXACT same problem, I couldnt' find anything wrong with my code as it was so garden variety, almost lost my sanity over this. Thanks for sharing
    – Nik So
    Jul 3, 2012 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.