Skip to content

Instantly share code, notes, and snippets.

@nateware
Created April 15, 2010 00:37
Show Gist options
  • Save nateware/366548 to your computer and use it in GitHub Desktop.
Save nateware/366548 to your computer and use it in GitHub Desktop.
Sequel vs Friendly benchmark
#!/usr/bin/env ruby
$:.unshift File.expand_path "#{File.dirname(__FILE__)}/../friendly/lib"
require 'mysqlplus'
require 'friendly'
require "rbench"
require 'uuid4r'
# Choose how many times you want to repeat each benchmark.
# This can be overridden on specific reports, if needed.
TIMES = 5000
database = {:adapter => 'mysql', :username => 'root', :password => 'ngenpd1t', :database => 'friendly_dev'}
Friendly.configure database
Sequel.connect database
Sequel::Model.plugin :schema
class Dog
include Friendly::Document
attribute :name, String
attribute :age, Integer
indexes :name
indexes :age
end
class Cat < Sequel::Model
set_schema do
primary_key :added_id
column :uuid, 'binary(16)', :unique => true, :null => false
String :name
Integer :age
index :name
index :age
end
def before_create
super
self.uuid = UUID4R.uuid(1, :bin)
end
end
# http://stackoverflow.com/questions/88311/how-best-to-generate-a-random-string-in-ruby
def random_string(maxlen=50)
(0..rand(maxlen)).map{65.+(rand(25)).chr}.join
end
puts "Recreating DB tables..."
Friendly.create_tables!
Cat.create_table!
# A relatively simple benchmark:
RBench.run(TIMES) do
column :sequel, :title => 'Sequel'
column :friend, :title => 'Friendly'
sequels = []; si = -1
friends = []; fi = -1
report "inserting #{TIMES} records" do
sequel { sequels << Cat.create(:age => rand(2000), :name => random_string) }
friend { friends << Dog.create(:age => rand(2000), :name => random_string) }
end
report "retrieving first record where :name" do
sequel { Cat.where(:name => sequels[si+=1].name).first }
friend { Dog.scope(:name => friends[fi+=1].name).first }
end
report "retrieving all records where :age" do
sequel { Cat.where(:age => rand(2000)).all }
friend { Dog.scope(:age => rand(2000)).all }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment