$:.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