jmettraux (owner)

Revisions

gist: 54409 Download_button fork
public
Public Clone URL: git://gist.github.com/54409.git
ar_vs_tctable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
$:.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
 
result0.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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