Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahoward/984235 to your computer and use it in GitHub Desktop.
Save ahoward/984235 to your computer and use it in GitHub Desktop.
mongo-should-be-bloody-faster.rb
## setup
#
require 'rubygems'
require 'benchmark'
require 'uuidtools'
require 'active_record'
require 'mongoid'
## setup a sequel connection
#
require 'sequel'
S = Sequel.connect('postgres://@localhost')
require 'sequel_pg'
## make sure the table is setup
#
S.drop_table(:bench) if S.table_exists?(:bench)
S.create_table(:bench) do
primary_key :id
String :value, :unique => true, :null => false
end
## setup an ar class
#
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :database => ENV['USER'])
class A < ActiveRecord::Base
set_table_name :bench
end
## setup mongoid class
#
class M
include Mongoid::Document
field(:value)
index(:value, :unique => true)
end
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('bench')
end
M.create_indexes
## setup the mongo raw collection
#
collection = Mongo::Connection.new['bench']['bench']
## bench
#
$n = (ARGV.shift || 1000).to_i
def values() Array.new($n).map{ UUIDTools::UUID.timestamp_create.to_s } end
Benchmark.bm do |bm|
s = S[:bench]
bm.report 'raw sequel' do
values.each do |value|
id = s.insert(:value => value)
s.where(:id => id).first
end
end
bm.report 'ar model' do
values.each do |value|
a = A.create!(:value => value)
A.where(:id => a.id).first
end
end
bm.report 'mongid model' do
values.each do |value|
m = M.create!(:value => value)
M.where(:id => m.id).first
end
end
bm.report 'raw mongo' do
values.each do |value|
id = collection.insert(:value => value)
collection.find(:_id => id).first
end
end
end
__END__
========================================================
n = 1000
user system total real
raw sequel 0.590000 0.080000 0.670000 ( 1.497045)
ar model 1.190000 0.090000 1.280000 ( 1.931152)
mongid model 0.810000 0.040000 0.850000 ( 2.544655)
raw mongo 0.370000 0.030000 0.400000 ( 0.505453)
user system total real
raw sequel 0.590000 0.090000 0.690000 ( 2.595548)
ar model 1.230000 0.080000 1.310000 ( 2.095908)
mongid model 0.830000 0.040000 0.870000 ( 3.033356)
raw mongo 0.380000 0.030000 0.410000 ( 0.461516)
user system total real
raw sequel 0.580000 0.080000 0.670000 ( 1.312917)
ar model 1.210000 0.090000 1.300000 ( 1.891217)
mongid model 0.860000 0.040000 0.900000 ( 3.586757)
raw mongo 0.370000 0.030000 0.400000 ( 0.455874)
user system total real
raw sequel 0.590000 0.090000 0.680000 ( 1.330041)
ar model 1.220000 0.080000 1.300000 ( 1.908693)
mongid model 0.840000 0.050000 0.890000 ( 3.959392)
raw mongo 0.370000 0.030000 0.400000 ( 0.474385)
user system total real
raw sequel 0.580000 0.080000 0.660000 ( 1.418006)
ar model 1.220000 0.090000 1.310000 ( 1.980597)
mongid model 0.860000 0.040000 0.900000 ( 4.427167)
raw mongo 0.370000 0.030000 0.400000 ( 0.450478)
user system total real
raw sequel 0.620000 0.090000 0.710000 ( 1.708009)
ar model 1.340000 0.100000 1.440000 ( 2.183434)
mongid model 0.910000 0.040000 0.950000 ( 4.940917)
raw mongo 0.380000 0.030000 0.410000 ( 0.486670)
user system total real
raw sequel 0.610000 0.100000 0.710000 ( 2.126445)
ar model 1.250000 0.080000 1.330000 ( 3.386526)
mongid model 0.880000 0.050000 0.930000 ( 5.249455)
raw mongo 0.380000 0.030000 0.410000 ( 0.526936)
user system total real
raw sequel 0.600000 0.090000 0.690000 ( 1.687000)
ar model 1.230000 0.080000 1.310000 ( 2.499392)
mongid model 0.890000 0.050000 0.940000 ( 5.839483)
raw mongo 0.400000 0.040000 0.440000 ( 0.537875)
========================================================
n = 10_000
user system total real
raw sequel 6.690000 0.660000 7.350000 ( 22.036142)
ar model 12.620000 0.840000 13.460000 ( 21.399599)
mongid model 11.110000 0.500000 11.610000 ( 88.837410)
raw mongo 3.690000 0.320000 4.010000 ( 4.620775)
user system total real
raw sequel 6.590000 0.650000 7.250000 ( 19.691586)
ar model 12.340000 0.810000 13.150000 ( 22.792966)
mongid model 11.680000 0.490000 12.170000 (130.087106)
raw mongo 3.740000 0.320000 4.060000 ( 4.730355)
@paul
Copy link

paul commented May 21, 2011

 $ ruby scary-pg-mongoid-sequel-ar-benchmark.rb 1000
      user     system      total        real
raw sequel  0.430000   0.080000   0.510000 (  1.081222)
ar model  1.220000   0.090000   1.310000 (  1.881141)
mongid model  0.620000   0.040000   0.660000 (  1.135799)
raw mongo  0.320000   0.030000   0.350000 (  0.435519)

 $ ruby scary-pg-mongoid-sequel-ar-benchmark.rb 10000
      user     system      total        real
raw sequel  4.190000   0.510000   4.700000 ( 10.545542)
ar model 10.510000   0.800000  11.310000 ( 17.019659)
mongid model  6.950000   0.480000   7.430000 ( 40.954382)
raw mongo  3.400000   0.370000   3.770000 (  4.477398)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment