Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mongoid document performance
require 'benchmark'
require 'mongoid'
class FakeModel
attr_accessor :test1, :test2, :test3, :test4, :test5, :test6, :test7, :test8
def initialize
self.test1 = 1
self.test2 = 1
self.test3 = 1
self.test4 = 1
self.test5 = 1
self.test6 = 1
self.test7 = 1
self.test8 = 1
end
def + other
self.test1 += other.test1
self.test2 += other.test2
self.test3 += other.test3
self.test4 += other.test4
self.test5 += other.test5
self.test6 += other.test6
self.test7 += other.test7
self.test8 += other.test8
end
end
class MongoidFake
include Mongoid::Document
field :name, type: String
field :load, type: Float, default: 0.0
field :unhedged, type: Float, default: 0.0
field :one_x_1, type: Float, default: 0.0
field :two_x_16, type: Float, default: 0.0
field :five_x_16, type: Float, default: 0.0
field :seven_x_8, type: Float, default: 0.0
field :seven_x_16, type: Float, default: 0.0
field :seven_x_24, type: Float, default: 0.0
field :wrap, type: Float, default: 0.0
def + other
self.load += other.load
self.unhedged += other.unhedged
self.one_x_1 += other.one_x_1
self.two_x_16 += other.two_x_16
self.five_x_16 += other.five_x_16
self.seven_x_8 += other.seven_x_8
self.seven_x_16 += other.seven_x_16
self.seven_x_24 += other.seven_x_24
self.wrap += other.wrap
end
end
n = 450 * 24
fm1 = FakeModel.new
fm2 = FakeModel.new
mf1 = MongoidFake.new
mf2 = MongoidFake.new
Benchmark.bm do |x|
x.report { n.times do fm1 + fm2; end }
x.report { n.times do mf1 + mf2; end }
end
# user system total real
# 0.020000 0.000000 0.020000 ( 0.018330)
# 1.980000 0.000000 1.980000 ( 1.981296)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment