dbmlite3 2.0.0.pre.alpha.6 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/extras/benchmark.rb DELETED
@@ -1,172 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Low-effort benchmark comparing Lite3::DBM in various modes against
4
- # other equivalent Hash-like Ruby storage mechanisms.
5
-
6
- require 'dbmlite3'
7
- require 'yaml/dbm' unless RUBY_PLATFORM == "java"
8
- require 'fileutils'
9
- require 'optparse'
10
-
11
- COUNT = 10000
12
-
13
- Opts = proc do
14
- opt_defaults = {
15
- count: COUNT,
16
- transaction_only: true,
17
- }
18
-
19
- opts = Struct.new(*opt_defaults.keys).new(*opt_defaults.values)
20
-
21
- OptionParser.new do |opo|
22
- opo.banner = "Usage: bench_1.rb [options]"
23
-
24
- opo.on("-t", "--multi-transaction",
25
- "Don't batch Lite3 accesses in one big transaction.") {
26
- opts.transaction_only = false
27
- }
28
-
29
- opo.on("-c", "--count N", Integer, "Set test count.") { |c|
30
- opts.count = c
31
- }
32
- end.parse!
33
-
34
- next opts
35
- end.call
36
-
37
-
38
- module Tmp
39
- @root = File.join( File.dirname(__FILE__), "tmpdata")
40
- @count = 0
41
-
42
- def self.file
43
- FileUtils.mkdir(@root) unless File.directory?(@root)
44
-
45
- file = "testfile_#{@count}_#{$$}.sqlite3"
46
- @count += 1
47
-
48
- return File.join(@root, file)
49
- end
50
-
51
- def self.cleanup
52
- return unless File.directory?(@root)
53
- FileUtils.rm_rf(@root)
54
- end
55
- end
56
-
57
-
58
-
59
- def insert(count, db, offset)
60
- for n in 0 .. count
61
- db["k_#{n}"] = "#{n + offset}"
62
- end
63
- end
64
-
65
-
66
- def lookup(count, db)
67
- rnd = Random.new(69_420)
68
- sz = db.size
69
-
70
- for _ in 0 .. count * 3
71
- idx = rnd.rand(sz)
72
- v = db["k_#{idx}"]
73
- raise "Invalid value: #{v}" unless v.to_s == "#{idx + 1}"
74
- end
75
- end
76
-
77
-
78
- Times = {}
79
-
80
- def time_it(type, task, db, use_tr, &block)
81
- print "#{type} #{task} - "
82
- STDOUT.flush
83
-
84
- start = Time.now
85
-
86
- if use_tr && db.respond_to?(:transaction)
87
- db.transaction { block.call }
88
- else
89
- block.call
90
- end
91
-
92
- finish = Time.now
93
-
94
- elapsed = (finish - start).to_f
95
- Times[type] = Times.fetch(type, 0) + elapsed
96
-
97
- puts "#{elapsed.round(4)}"
98
- end
99
-
100
- def bench(count, desc, db, use_tr)
101
- time_it(desc, "insert", db, use_tr) {
102
- insert(count, db, 0)
103
- }
104
- time_it(desc, "upsert", db, use_tr) {
105
- insert(count, db, 1)
106
- }
107
- time_it(desc, "lookup", db, use_tr) {
108
- lookup(count, db)
109
- }
110
- time_it(desc, "delete_if", db, use_tr) {
111
- rnd = Random.new(69_420)
112
- db.delete_if{|k,v| rnd.rand(2) == 0}
113
- }
114
- puts
115
- end
116
-
117
-
118
-
119
- def main
120
- puts "Count = #{Opts.count}\n"
121
-
122
- Tmp.cleanup
123
-
124
- bench(Opts.count, "hash", {}, false)
125
-
126
- if RUBY_PLATFORM != "java"
127
- DBM.open(Tmp.file) {|dbm|
128
- bench(Opts.count, "DBM", dbm, false)
129
- }
130
-
131
- YAML::DBM.open(Tmp.file) {|dbm|
132
- bench(Opts.count, "YAML::DBM", dbm, false)
133
- }
134
- end
135
-
136
- Lite3::DBM.open(Tmp.file, "benchmark", :yaml) { |dbm|
137
- bench(Opts.count, "Lite3::DBM(yaml)", dbm, true)
138
- }
139
-
140
- Lite3::DBM.open(Tmp.file, "benchmark", :marshal) { |dbm|
141
- bench(Opts.count, "Lite3::DBM(marshal)", dbm, true)
142
- }
143
-
144
- Lite3::DBM.open(Tmp.file, "benchmark", :string) { |dbm|
145
- bench(Opts.count, "Lite3::DBM(string)", dbm, true)
146
- }
147
-
148
- if !Opts.transaction_only
149
- Lite3::DBM.open(Tmp.file, "benchmark", :yaml) { |dbm|
150
- bench(Opts.count, "Lite3::DBM(yaml, single-trans)", dbm, false)
151
- }
152
-
153
- Lite3::DBM.open(Tmp.file, "benchmark", :marshal) { |dbm|
154
- bench(Opts.count, "Lite3::DBM(marshal, single-trans)", dbm, false)
155
- }
156
-
157
- Lite3::DBM.open(Tmp.file, "benchmark", :string) { |dbm|
158
- bench(Opts.count, "Lite3::DBM(string, single-trans)", dbm, false)
159
- }
160
- end
161
-
162
-
163
- puts
164
- puts "Totals:"
165
- Times.each{|k,v|
166
- puts " #{k} - #{v.round(4)}"
167
- }
168
-
169
- Tmp.cleanup
170
- end
171
-
172
- main