Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. ahoward revised this gist May 21, 2011. 1 changed file with 112 additions and 108 deletions.
    220 changes: 112 additions & 108 deletions scary-pg-mongoid-sequel-ar-benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -1,160 +1,164 @@
    # following is a little benchmark looking a real world example of inserting a
    # bunch of records into pg and mongo using a few methods available in ruby.
    # this is not meant to be an apples to apples benchmark, rather it's trying to
    # see how the various dbs might perform in a real world situation. i know
    # mongo does not fsync to disk. i know it's not fair using a transaction with
    # the rdbms, but these conditions will exist in real world usage and it's that
    # which i'm interested in.
    #
    # here is a few runs
    #
    # n = 10_000 (with transactions)
    #
    # user system total real
    # pg 0.840000 0.330000 1.170000 ( 3.265902)
    # sequel 6.350000 0.540000 6.890000 ( 10.261432)
    # ar model 11.860000 0.380000 12.240000 ( 15.038289)
    # mongid model 9.400000 0.520000 9.920000 ( 35.926766)
    # mongo 5.920000 0.410000 6.330000 ( 7.749044)
    #
    #
    # n = 10_000 (*without* transactions - code was modified)
    #
    # user system total real
    # pg 0.900000 0.440000 1.350000 ( 7.976228)
    # sequel 6.150000 0.560000 6.710000 ( 13.553662)
    # ar model 11.500000 0.790000 12.290000 ( 18.490117)
    # mongid model 9.330000 0.530000 9.860000 ( 35.956647)
    # mongo 5.710000 0.410000 6.120000 ( 7.577185)
    #
    #
    # n = 100_000 (with transactions)
    #
    # user system total real
    # pg 8.450000 2.780000 11.230000 ( 37.551105)
    # sequel 69.880000 5.170000 75.050000 (103.346337)
    # ar model233.250000 3.860000 237.110000 (265.600197)
    # mongid model145.180000 5.910000 151.090000 (2443.426801)
    # mongo 51.490000 3.920000 55.410000 ( 70.370120)
    #
    #
    #
    # summary of results:
    #
    # 1) raw pg is almost as fast(er) as raw mongo and a *lot* more durable.
    # 2) ar has gotten much, much better.
    #
    #


    ## setup
    #
    require 'rubygems'
    require 'ostruct'
    require 'benchmark'
    require 'uuidtools'
    require 'active_record'
    require 'mongoid'

    ## cli options
    #
    STDOUT.sync = true
    $n = (ARGV.shift || 1000).to_i

    ## config
    #
    C =
    OpenStruct.new(
    'db' => (ENV['USER'] || 'bench'),
    'table' => 'bench'
    )

    ## setup a sequel connection
    #
    require 'sequel'
    S = Sequel.connect('postgres://@localhost')
    S = Sequel.connect("postgres://localhost/#{ C.db }")
    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'])
    ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :database => C.db)

    class A < ActiveRecord::Base
    set_table_name :bench
    set_table_name C.table
    end

    ## setup mongoid class
    #
    Mongoid.configure do |config|
    config.master = Mongo::Connection.new.db(C.db)
    end

    class M
    include Mongoid::Document
    self.collection_name = C.table
    field(:value)
    index(:value, :unique => true)
    end
    Mongoid.configure do |config|
    config.master = Mongo::Connection.new.db('bench')
    A.delete_all

    ## make sure the rdbms table is setup and indexed
    #
    S.drop_table(C.table) if S.table_exists?(C.table)
    S.create_table(C.table) do
    primary_key(:id)
    String(:value, :unique => true, :null => false)
    end

    ## make sure the mongo collection is setup and indexed
    #
    M.delete_all
    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]
    sequel = S[:bench]
    mongo = M.collection.db.collection(C.table)
    pg = A.connection.raw_connection

    bm.report 'raw sequel' do
    bm.report 'pg' do
    pg.exec('begin transaction')
    values.each do |value|
    id = s.insert(:value => value)
    s.where(:id => id).first
    res = pg.exec('insert into bench values(default, $1) returning id', [value])
    id = res.getvalue(0,0)
    res = pg.exec('select * from bench where id=$1', [id])
    end
    pg.exec('commit')
    end

    bm.report 'sequel' do
    S.transaction do
    values.each do |value|
    id = sequel.insert(:value => value)
    sequel.where(:id => id).first
    end
    end
    end

    bm.report 'ar model' do
    values.each do |value|
    a = A.create!(:value => value)
    A.where(:id => a.id).first
    A.transaction do
    values.each do |value|
    r = A.create!(:value => value)
    A.where(:id => r.id).first
    end
    end
    end

    bm.report 'mongid model' do
    values.each do |value|
    m = M.create!(:value => value)
    M.where(:id => m.id).first
    r = M.safely.create!(:value => value)
    M.where(:id => r.id).first
    end
    end

    bm.report 'raw mongo' do
    bm.report 'mongo' do
    values.each do |value|
    id = collection.insert(:value => value)
    collection.find(:_id => id).first
    id = mongo.insert({:value => value}, {:safe => true})
    mongo.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)
  2. ahoward created this gist May 21, 2011.
    160 changes: 160 additions & 0 deletions scary-pg-mongoid-sequel-ar-benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,160 @@
    ## 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)