Skip to content

Instantly share code, notes, and snippets.

@jmettraux
Created January 29, 2009 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmettraux/54409 to your computer and use it in GitHub Desktop.
Save jmettraux/54409 to your computer and use it in GitHub Desktop.
$:.unshift('~/rufus/rufus-tokyo/lib')
require 'benchmark'
require 'rubygems'
require 'faker'
#
# the data
#
N = 1000
DATA = (0..N - 1).collect { |i|
{
'name' => Faker::Name.name,
'sex' => (i % 2) ? 'male' : 'female',
'birthday' => DateTime.new(1972, 10, 14),
'divisions' => (i % 2) ? 'brd' : 'dev'
}
}
DATA1 = DATA.collect { |e|
h = e.dup
h['birthday'] = h['birthday'].to_s
h
}
# Tokyo Cabinet tables only do strings
#
# AR ==========================================================================
#
#require_gem 'activerecord'
gem 'activerecord'; require 'active_record'
class TheMigration < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.column :name, :string
t.column :sex, :string
t.column :birthday, :timestamp
t.column :divisions, :string
end
add_index :people, :name
add_index :people, :sex
add_index :people, :birthday
add_index :people, :divisions
end
def self.down
drop_table :people
end
end
class Person < ActiveRecord::Base; end
ActiveRecord::Migration.verbose = false
def do_ar_benchmark
begin
TheMigration.down
rescue Exception => e
end
TheMigration.up
2.times { puts }
puts "AR (#{ActiveRecord::Base.connection.adapter_name})"
Benchmark.benchmark(' ' * 20 + Benchmark::Tms::CAPTION, 20) do |b|
b.report('inserting data') do
DATA.each { |h| Person.create(h) }
end
b.report('finding all') do
Person.find(:all)
end
b.report('finding last') do
Person.find(DATA.size)
end
b.report('find Alphonse') do
Person.find_by_name('Alphonse Armalite')
end
end
end
## mysql
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:database => 'test',
:encoding => 'utf8',
:pool => 30) # 2.2 ...
do_ar_benchmark
## sqlite3
FileUtils.rm_f('sqlite_test.db')
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'sqlite_test.db')
do_ar_benchmark
puts
puts 'db weight : ' + `ls -lh ./sqlite_test.db | awk '{ print $5 }'`
#
# Tokyo Cabinet table =========================================================
#
require 'rufus/tokyo/cabinet/table'
FileUtils.rm_f('test.tdb')
table = Rufus::Tokyo::Table.new('test.tdb', :create, :write)
table.clear
2.times { puts }
puts 'TC table'
Benchmark.benchmark(' ' * 20 + Benchmark::Tms::CAPTION, 20) do |b|
b.report('inserting data') do
DATA1.each_with_index { |e, i| table[i.to_s] = e }
end
b.report('finding all') do
table.query { |q| }
end
b.report('find last') do
table[DATA.size.to_s]
end
b.report('find Alphonse') do
table.query { |q| q.add('name', :equals, 'Alphonse Armalite') }
end
end
puts
puts 'db weight : ' + `ls -lh ./test.tdb | awk '{ print $5 }'`
puts
# Thu Jan 29 15:21:40 JST 2009
# 1
AR (MySQL)
user system total real
inserting data 1.940000 0.080000 2.020000 ( 2.721870)
finding all 0.010000 0.000000 0.010000 ( 0.012541)
finding last 0.000000 0.000000 0.000000 ( 0.000556)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000638)
AR (SQLite)
user system total real
inserting data 2.590000 0.630000 3.220000 ( 5.156360)
finding all 0.130000 0.000000 0.130000 ( 0.131685)
finding last 0.000000 0.000000 0.000000 ( 0.000623)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000430)
db weight : 139K
TC table
user system total real
inserting data 0.110000 0.030000 0.140000 ( 0.143995)
finding all 0.100000 0.000000 0.100000 ( 0.099476)
find last 0.000000 0.000000 0.000000 ( 0.000026)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000922)
db weight : 645K
# 2
AR (MySQL)
user system total real
inserting data 1.890000 0.080000 1.970000 ( 2.745904)
finding all 0.010000 0.000000 0.010000 ( 0.013010)
finding last 0.000000 0.000000 0.000000 ( 0.000571)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000628)
AR (SQLite)
user system total real
inserting data 2.580000 0.620000 3.200000 ( 7.623932)
finding all 0.140000 0.010000 0.150000 ( 0.134711)
finding last 0.000000 0.000000 0.000000 ( 0.000628)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000431)
db weight : 138K
TC table
user system total real
inserting data 0.110000 0.030000 0.140000 ( 0.146348)
finding all 0.100000 0.000000 0.100000 ( 0.097483)
find last 0.000000 0.000000 0.000000 ( 0.000019)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000827)
db weight : 645K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment