jmettraux (owner)

Forks

Revisions

gist: 54343 Download_button fork
public
Public Clone URL: git://gist.github.com/54343.git
ar_mysql_vs_tctsb.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
$:.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
 
 
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
# 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 ?
- ...
 
 
 
result1.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
# 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