Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2176

JSON encoding of embedded documents is slow

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None
    • None

      It seems to get slower as a power of the number of levels and the number of embedded documents, unless there is some indexing or something I am missing

      require 'mongoid'
      require 'benchmark'
      require 'yajl/json_gem'
      
      ENV['MONGOID_ENV'] = 'development'
      
      Mongoid.load!("config/mongoid.yml")
      
      class Zine
        include Mongoid::Document
        field :name, type: String
      end
      
      class Band
        include Mongoid::Document
        field :name, type: String
        embeds_many :albums
      end
      
      class Album
        include Mongoid::Document
        field :name, type: String
        embedded_in :band
        embeds_many :tracks
      end
      
      class Track
        include Mongoid::Document
        field :name, type: String
        embedded_in :album
      end
      
      
      NUM_DOCS = 1000
      
      Benchmark.bm do |x|
      
        Zine.delete_all
        NUM_DOCS.times do |i|
          Zine.create!(name:"Zine #{i}")
        end
      
        x.report("One") { Zine.all.to_json }
      
        Band.delete_all
        NUM_DOCS.times do |i|
          Band.create!(name:"Band #{i}")
        end
        x.report("One") { Band.all.to_json }
      
      
        Band.delete_all
        NUM_DOCS.times do |i|
          Band.create!(name:"Band #{i}", albums: [
            { name:"Album #{i}"},
            { name:"Album #{i}"}
          ])
        end
        x.report("Two") { Band.all.to_json }
      
        Band.delete_all
          NUM_DOCS.times do |i|
            Band.create!(name:"Band #{i}", albums: [
              { name:"Album #{i}", tracks: [
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}"
              ]},
              { name:"Album #{i}", tracks: [
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}",
                name: "Track #{i}"
              ]}
      
            ])
          end
        x.report("Three") { Band.all.to_json }
      
      end
      

      results

             user     system      total        real
      One  0.210000   0.000000   0.210000 (  0.209466)
      One  0.250000   0.000000   0.250000 (  0.254118)
      Two  0.910000   0.010000   0.920000 (  0.917821)
      Three  1.680000   0.000000   1.680000 (  1.689789)
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            aubergene aubergene
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: