zev (owner)

Fork Of

Revisions

gist: 54371 Download_button fork
public
Public Clone URL: git://gist.github.com/54371.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
149
150
151
152
153
154
155
$:.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
 
 
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
== TheMigration: reverting ===================================================
-- drop_table(:people)
== TheMigration: migrating ===================================================
-- create_table(:people)
   -> 0.0062s
-- add_index(:people, :name)
   -> 0.0043s
-- add_index(:people, :sex)
   -> 0.0042s
-- add_index(:people, :birthday)
   -> 0.0052s
-- add_index(:people, :divisions)
   -> 0.0049s
== TheMigration: migrated (0.0252s) ==========================================
 
 
 
 
 
AR
                          user system total real
inserting data 15.870000 1.490000 17.360000 ( 25.338565)
finding all 0.100000 0.020000 0.120000 ( 0.139700)
finding last 0.000000 0.000000 0.000000 ( 0.000495)
find Alphonse 0.000000 0.000000 0.000000 ( 0.000620)
 
 
TC table
                          user system total real
inserting data 0.430000 0.040000 0.470000 ( 0.462957)
finding all 1.050000 0.120000 1.170000 ( 1.178739)
find last 0.000000 0.000000 0.000000 ( 0.000016)
find Alphonse 0.010000 0.000000 0.010000 ( 0.004580)
 
result2.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
== TheMigration: reverting ===================================================
-- drop_table(:people)
== TheMigration: migrating ===================================================
-- create_table(:people)
   -> 0.0575s
-- add_index(:people, :name)
   -> 0.0400s
-- add_index(:people, :sex)
   -> 0.0052s
-- add_index(:people, :birthday)
   -> 0.0044s
-- add_index(:people, :divisions)
   -> 0.0064s
== TheMigration: migrated (0.1141s) ==========================================
 
 
 
 
 
AR
                          user system total real
inserting data 15.880000 1.400000 17.280000 ( 25.141273)
finding all 0.120000 0.020000 0.140000 ( 0.163360)
finding last 0.000000 0.000000 0.000000 ( 0.000467)
find names 0.030000 0.000000 0.030000 ( 0.035074)
 
 
TC table
                          user system total real
inserting data 0.400000 0.050000 0.450000 ( 0.452233)
finding all 1.030000 0.140000 1.170000 ( 1.160435)
find last 0.000000 0.000000 0.000000 ( 0.000018)
find names 0.450000 0.000000 0.450000 ( 0.481272)