Skip to content

Instantly share code, notes, and snippets.

@jmettraux
Created January 29, 2009 03:17
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 jmettraux/54343 to your computer and use it in GitHub Desktop.
Save jmettraux/54343 to your computer and use it in GitHub Desktop.
$:.unshift('~/rufus/rufus-tokyo/lib')
require 'benchmark'
require 'rubygems'
#
# the data
#
colnames = %w{ name sex birthday divisions }
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' ]
].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
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('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('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
# 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 ?
- ...
# Thu Jan 29 15:06:54 JST 2009
# 1
AR (MySQL)
user system total real
inserting data 0.000000 0.000000 0.000000 ( 0.012526)
finding all 0.000000 0.000000 0.000000 ( 0.000589)
finding last 0.000000 0.000000 0.000000 ( 0.000563)
find Alphonse 0.000000 0.000000 0.000000 ( 0.001095)
AR (SQLite)
user system total real
inserting data 0.010000 0.000000 0.010000 ( 0.014630)
finding all 0.000000 0.000000 0.000000 ( 0.000989)
finding last 0.000000 0.000000 0.000000 ( 0.000619)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000624)
db weight : 7.0K
TC table
user system total real
inserting data 0.000000 0.000000 0.000000 ( 0.000582)
finding all 0.000000 0.000000 0.000000 ( 0.000651)
find last 0.000000 0.000000 0.000000 ( 0.000019)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000190)
db weight : 548K
# 2
AR (MySQL)
user system total real
inserting data 0.010000 0.000000 0.010000 ( 0.013439)
finding all 0.000000 0.000000 0.000000 ( 0.000610)
finding last 0.000000 0.000000 0.000000 ( 0.000402)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000844)
AR (SQLite)
user system total real
inserting data 0.010000 0.000000 0.010000 ( 0.015223)
finding all 0.000000 0.000000 0.000000 ( 0.000952)
finding last 0.000000 0.000000 0.000000 ( 0.000560)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000620)
db weight : 7.0K
TC table
user system total real
inserting data 0.000000 0.000000 0.000000 ( 0.000536)
finding all 0.000000 0.000000 0.000000 ( 0.000667)
find last 0.000000 0.000000 0.000000 ( 0.000021)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000176)
db weight : 548K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment