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