maiha (owner)

Revisions

gist: 139623 Download_button fork
public
Public Clone URL: git://gist.github.com/139623.git
Embed All Files: show embed
bench for MongoDB vs PostgreSQL.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
require 'rubygems'
 
######################################################################
### ActiveRecord (PostgreSQL)
 
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter=>"postgresql", :database=>"jpop", :user=>"maiha")
module PG
  class Song < ActiveRecord::Base
    # Indexes: "index_songs_singer" btree (singer)
  end
end
# p PG::Song.count # => 64482
 
######################################################################
### MongoRecord (MongoDB)
 
require 'mongo'
require 'mongo_record'
MongoRecord::Base.connection = XGen::Mongo::Driver::Mongo.new.db('jpop')
module MD
  class Song < MongoRecord::Base
    collection_name :songs
    fields :code, :name, :singer, :word, :music, :intro, :tieup, :body, :created_at
    index :singer
  end
end
# p MD::Song.count # => 64482
 
######################################################################
### Bench
 
require 'rbench'
 
puts "Bench1: counting records using index"
[10,100,1000].each do |times|
  puts "#{times} times:"
  RBench.run(times) do
    report("PostgreSQL") { PG::Song.count({:conditions => {:singer => "℃-ute" }}) } # matches 19
    report("MongoDB" ) { MD::Song.count({:conditions => {:singer => "℃-ute" }}) } # matches 19
  end
  puts ""
end
 
puts "Bench2: counting records using seq scan"
[1,10,100].each do |times|
  puts "#{times} times:"
  RBench.run(times) do
    report("PostgreSQL") { PG::Song.count(:conditions => {:body => "LOVE"}) } # matches 0
    report("MongoDB" ) { MD::Song.count(:conditions => {:body => "LOVE"}) } # matches 0
  end
  puts ""
end
 
__END__
 
Bench1: counting records using index
10 times:
Results |
----------------------------
PostgreSQL 0.028 |
MongoDB 0.012 |
 
100 times:
Results |
----------------------------
PostgreSQL 0.080 |
MongoDB 0.126 |
 
1000 times:
Results |
----------------------------
PostgreSQL 0.732 |
MongoDB 1.157 |
 
Bench2: counting records using seq scan
1 times:
Results |
----------------------------
PostgreSQL 0.304 |
MongoDB 0.063 |
 
10 times:
Results |
----------------------------
PostgreSQL 1.621 |
MongoDB 0.600 |
 
100 times:
Results |
----------------------------
PostgreSQL 16.485 |
MongoDB 5.198 |