-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmongodb-mysql.rb
72 lines (63 loc) · 1.78 KB
/
mongodb-mysql.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
require 'rubygems'
require 'mongo'
require 'benchmark'
require 'mysql'
include Mongo
# benchmark cycle
def cycle x
x.times{ |i|
doc = {
"text" => "i am any text",
"count" => i,
"coords" => {"x" => 100, "y" => 200, "z" => i}
}
yield doc
}
end
def flush_mysql mysql
mysql.query("DROP TABLE IF EXISTS `insert_bm`")
mysql.query("DROP TABLE IF EXISTS `insert_bm_coords`")
mysql.query("CREATE TABLE `insert_bm_coords`(
`id` int unsigned not null auto_increment,
`x` int not null,
`y` int not null,
PRIMARY KEY (`id`)
)")
mysql.query("CREATE TABLE `insert_bm`(
`id` int unsigned not null auto_increment,
`text` varchar(255) not null,
`count` int not null,
`coords_id` int unsigned not null,
PRIMARY KEY (`id`)
)")
# empty init entries
mysql.query("INSERT insert_bm_coords(x,y) VALUES(0, 0)")
mysql.query("INSERT insert_bm(text, count, coords_id) VALUES('', 0, #{mysql.insert_id()})")
end
def flush_mongo mongo_cnn
mongo_cnn.collection("bmdb").drop
mongo = mongo_cnn.collection("bmdb")
mongo.insert({"stub" => "i am empty init doc"})
mongo
end
mysql = Mysql.new('localhost', 'testuser', 'testpass', 'test')
mongo_cnn = Connection.new.db("ruby_db")
# do benchmarking
Benchmark.bm {|x|
points = [1_000, 10_000, 100_000, 500_000, 1_000_000]
points.each{|c|
flush_mysql mysql
mongo = flush_mongo(mongo_cnn)
x.report("mysql #{c.to_s} :") {
cycle(c){|doc|
mysql.query("INSERT insert_bm_coords(x,y) VALUES(#{doc["coords"]["x"]}, #{doc["coords"]["y"]})")
mysql.query("INSERT insert_bm(text, count, coords_id) VALUES('#{doc["text"]}', #{doc["count"]}, #{mysql.insert_id()})")
}
}
x.report("mongo #{c.to_s} :") {
cycle(c){|doc|
mongo.insert(doc)
}
}
}
}