public

  • Download Gist
logs_mysql_vs_mongo_inserts_benchmark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
require 'rubygems'
require 'dm-core'
require 'dm-timestamps'
require 'rbench'
 
DataMapper.setup(:default, "mysql://localhost/logs_test")
DataMapper.setup(:mongo, "mongo://localhost/logs_test")
 
class MysqlLog
include DataMapper::Resource
 
property :id, Serial
property :message, String, :length => 512, :required => true
property :user_id, Integer
property :created_at, Time, :default => lambda { Time.now }
 
belongs_to :user
end
 
class MongoLog
include DataMapper::Mongo::Resource
 
def self.default_repository_name; :mongo; end
 
property :id, ObjectID, :required => false
property :message, String, :length => 512, :required => true
property :user_id, Integer
property :created_at, Time, :default => lambda { Time.now }
 
belongs_to :user, :repository => :default
end
 
class User
include DataMapper::Resource
 
property :id, Serial
property :name, String
 
has n, :mongo_logs, :repository => :mongo
has n, :mysql_logs
end
 
DataMapper.auto_migrate!
MongoLog.all.destroy!
 
user = User.create(:name => 'Foo')
 
TIMES = 1000
 
RBench.run(TIMES) do
column :times
column :mysql, :title => 'MySQL'
column :mongo, :title => "Mongo"
column :diff, :compare => [:mysql, :mongo]
 
report 'create log', TIMES do
mysql { user.mysql_logs.create(:message => 'this is a log') }
mongo { user.mongo_logs.create(:message => 'this is a log') }
end
 
report 'read logs with limit=20', TIMES do
mysql { user.mysql_logs.all(:limit => 20) }
mongo { user.mongo_logs.all(:limit => 20) }
end
 
report 'update log message', TIMES do
mysql { user.mysql_logs.first(:message => 'this is a log').update(:message=> 'this is still a log') }
mongo { user.mongo_logs.first(:message => 'this is a log').update(:message=> 'this is still a log') }
end
end
results.txt
1 2 3 4 5
| MySQL | Mongo | DIFF |
-----------------------------------------------------------------------
create log x999 | 26.824 | 1.308 | 20.51x |
read logs with limit=20 x999 | 0.215 | 0.211 | 1.02x |
update log message x999 | 27.832 | 3.919 | 7.10x |

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.