Skip to content

Instantly share code, notes, and snippets.

@nateware
Created May 24, 2010 16:20
Show Gist options
  • Save nateware/412084 to your computer and use it in GitHub Desktop.
Save nateware/412084 to your computer and use it in GitHub Desktop.
Sequel create vs insert benchmark
#!/usr/bin/env ruby
require 'sequel'
require 'logger'
require 'benchmark'
STDOUT.sync = true
DB = Sequel.mysql 'seqtest', :user => 'root', :password => 'secret'#, :logger => Logger.new(STDOUT)
Sequel::Model.plugin :schema
class User < Sequel::Model
set_schema do
primary_key :id
varchar :username
end
one_to_many :friends
one_to_many :messages
one_to_many :sent_messages, :foreign_key => :from_user_id, :class => 'Message'
end
class Friend < Sequel::Model
set_schema do
primary_key :id
integer :user_id
integer :friend_user_id
end
many_to_one :user
end
class Message < Sequel::Model
set_schema do
primary_key :id
integer :from_user_id
integer :user_id
integer :body
varchar :gift_type
integer :gift_id
bigint :gift_qty
end
many_to_one :user
end
User.create_table!
Friend.create_table!
Message.create_table!
TIMES = 10000
Benchmark.bm(30) do |bm|
bm.report("plain insert") do
TIMES.times do |n|
User.insert(:username => "user#{n}")
end
end
bm.report("find by id") do
TIMES.times do |n|
User[n+1].username
end
end
# ensure clean DB
User.delete
bm.report("create") do
TIMES.times do |n|
User.create(:username => "user#{n}")
end
end
# ensure clean DB
User.delete
bm.report("plain insert with find") do
TIMES.times do |n|
id = User.insert(:username => "user#{n}")
User[id].username
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment