Skip to content

Instantly share code, notes, and snippets.

@zev
Forked from jmettraux/ar_mysql_vs_tctsb.rb
Created January 29, 2009 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zev/54371 to your computer and use it in GitHub Desktop.
Save zev/54371 to your computer and use it in GitHub Desktop.
$:.unshift('~/rufus/rufus-tokyo/lib')
require 'benchmark'
require 'rubygems'
require 'faker'
require 'date'
#
# the data
#
colnames = %w{ name sex birthday divisions }
$year = (1909 .. 2009).to_a
$month = (1..12).to_a
$day = (1..28).to_a # not bothering with month diffs
def rbdate
DateTime.new($year[rand($year.size)], $month[rand($month.size)], $day[rand($day.size)])
end
def rdiv
case rand(3)
when 0
'dev'
when 1
'brd'
else
'brd,dev'
end
end
def rgen
(rand(2) == 1 ? 'male' : 'female')
end
data = [
[ 'Alphonse Armalite', 'male', DateTime.new(1972, 10, 14), 'brd,dev' ],
[ 'Brutus Beromunster', 'male', DateTime.new(1964, 07, 14), 'dev' ],
[ 'Crystel Chucknorris', 'female', DateTime.new(1980, 07, 12), 'brd' ],
[ 'Desree Dylan', 'female', DateTime.new(1954, 07, 13), 'brd,dev' ]
]
10_000.times do |i|
data << [ Faker::Name.name, rgen, rbdate, rdiv]
end
$find_name_list = []
100.times { $find_name_list << data[rand(data.size)][0] }
data.collect! { |e|
(0..colnames.length - 1).inject({}) { |h, i| h[colnames[i]] = e[i]; h }
}
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
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:database => 'test',
:encoding => 'utf8',
:pool => 30) # 2.2 ...
TheMigration.down rescue
TheMigration.up
class Person < ActiveRecord::Base; end
4.times { puts }
puts 'AR'
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 names') do
$find_name_list.each do |name|
Person.find_by_name(name)
end
end
end
#
# Tokyo Cabinet table =========================================================
#
require 'rufus/tokyo/cabinet/table'
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 names') do
$find_name_list.each do |name|
table.query { |q| q.add('name', :equals, name) }
end
end
end
# mysql Ver 14.12 Distrib 5.0.67, for apple-darwin9.0.0b5 (i686)
# ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
# 4GB RAM, MacOSX 10.5.6, 2.4 GHz Intel Core Duo
# Thu Jan 29 14:19:16 JST 2009
# 1
AR
user system total real
inserting data 0.010000 0.000000 0.010000 ( 0.012074)
finding all 0.000000 0.000000 0.000000 ( 0.000459)
finding last 0.000000 0.000000 0.000000 ( 0.000427)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000688)
TC table
user system total real
inserting data 0.000000 0.000000 0.000000 ( 0.000486)
finding all 0.000000 0.000000 0.000000 ( 0.000507)
find last 0.000000 0.000000 0.000000 ( 0.000016)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000154)
# 2
AR
user system total real
inserting data 0.010000 0.000000 0.010000 ( 0.013696)
finding all 0.000000 0.000000 0.000000 ( 0.000426)
finding last 0.000000 0.000000 0.000000 ( 0.000343)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000673)
TC table
user system total real
inserting data 0.000000 0.000000 0.000000 ( 0.000450)
finding all 0.000000 0.000000 0.000000 ( 0.000702)
find last 0.000000 0.000000 0.000000 ( 0.000016)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000149)
# questions
- weight of AR itself in this ? ...
- network vs lib...
- TC table mode is :create, :write, very naive, what about tuning the lock thing ?
- ...
== TheMigration: reverting ===================================================
-- drop_table(:people)
== TheMigration: migrating ===================================================
-- create_table(:people)
-> 0.0062s
-- add_index(:people, :name)
-> 0.0043s
-- add_index(:people, :sex)
-> 0.0042s
-- add_index(:people, :birthday)
-> 0.0052s
-- add_index(:people, :divisions)
-> 0.0049s
== TheMigration: migrated (0.0252s) ==========================================
AR
user system total real
inserting data 15.870000 1.490000 17.360000 ( 25.338565)
finding all 0.100000 0.020000 0.120000 ( 0.139700)
finding last 0.000000 0.000000 0.000000 ( 0.000495)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000620)
TC table
user system total real
inserting data 0.430000 0.040000 0.470000 ( 0.462957)
finding all 1.050000 0.120000 1.170000 ( 1.178739)
find last 0.000000 0.000000 0.000000 ( 0.000016)
find Alphonse 0.010000 0.000000 0.010000 ( 0.004580)
== TheMigration: reverting ===================================================
-- drop_table(:people)
== TheMigration: migrating ===================================================
-- create_table(:people)
-> 0.0575s
-- add_index(:people, :name)
-> 0.0400s
-- add_index(:people, :sex)
-> 0.0052s
-- add_index(:people, :birthday)
-> 0.0044s
-- add_index(:people, :divisions)
-> 0.0064s
== TheMigration: migrated (0.1141s) ==========================================
AR
user system total real
inserting data 15.880000 1.400000 17.280000 ( 25.141273)
finding all 0.120000 0.020000 0.140000 ( 0.163360)
finding last 0.000000 0.000000 0.000000 ( 0.000467)
find names 0.030000 0.000000 0.030000 ( 0.035074)
TC table
user system total real
inserting data 0.400000 0.050000 0.450000 ( 0.452233)
finding all 1.030000 0.140000 1.170000 ( 1.160435)
find last 0.000000 0.000000 0.000000 ( 0.000018)
find names 0.450000 0.000000 0.450000 ( 0.481272)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment