memcached 0.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,371 @@
1
+
2
+ HERE = File.dirname(__FILE__)
3
+ $LOAD_PATH << "#{HERE}/../../lib/"
4
+
5
+ require 'benchmark'
6
+ require 'rubygems'
7
+
8
+ begin
9
+ require 'memory'
10
+ rescue LoadError
11
+ end
12
+
13
+ if ARGV.join !~ /--with/
14
+ ARGV << "--with-memcached" << "--with-memcache-client" << "--with-caffeine"
15
+ end
16
+
17
+ ARGV.grep(/--with-(\w+)/) do
18
+ begin
19
+ puts "Loading #{$1}"
20
+ require $1
21
+ rescue LoadError
22
+ puts "Failed"
23
+ end
24
+ end
25
+
26
+ # We'll use a simple @value to try to avoid spending time in Marshal,
27
+ # which is a constant penalty that both clients have to pay
28
+ @value = []
29
+ @marshalled = Marshal.dump(@value)
30
+
31
+ @opts = [
32
+ ['127.0.0.1:43042', '127.0.0.1:43043'],
33
+ {
34
+ :buffer_requests => false,
35
+ :no_block => false,
36
+ :namespace => "benchmark_namespace"
37
+ }
38
+ ]
39
+ @key1 = "Short"
40
+ @key2 = "Sym1-2-3::45"*8
41
+ @key3 = "Long"*40
42
+ @key4 = "Medium"*8
43
+
44
+ def restart_servers
45
+ system("ruby #{HERE}/../setup.rb")
46
+ sleep(1)
47
+ end
48
+
49
+ restart_servers
50
+
51
+ Benchmark.bm(31) do |x|
52
+
53
+ n = 2500
54
+
55
+ if defined? Memcached
56
+ @m = Memcached.new(
57
+ @opts[0],
58
+ @opts[1].merge(:no_block => true, :buffer_requests => true)
59
+ )
60
+ x.report("set:plain:noblock:memcached") do
61
+ n.times do
62
+ @m.set @key1, @marshalled, 0, false
63
+ @m.set @key2, @marshalled, 0, false
64
+ @m.set @key3, @marshalled, 0, false
65
+ @m.set @key1, @marshalled, 0, false
66
+ @m.set @key2, @marshalled, 0, false
67
+ @m.set @key3, @marshalled, 0, false
68
+ end
69
+ end
70
+ @m = Memcached.new(*@opts)
71
+ x.report("set:plain:memcached") do
72
+ n.times do
73
+ @m.set @key1, @marshalled, 0, false
74
+ @m.set @key2, @marshalled, 0, false
75
+ @m.set @key3, @marshalled, 0, false
76
+ @m.set @key1, @marshalled, 0, false
77
+ @m.set @key2, @marshalled, 0, false
78
+ @m.set @key3, @marshalled, 0, false
79
+ end
80
+ end # if false
81
+ end
82
+ # Not supported by Caffeine
83
+ if defined? MemCache
84
+ @m = MemCache.new(*@opts)
85
+ x.report("set:plain:memcache-client") do
86
+ n.times do
87
+ @m.set @key1, @marshalled, 0, true
88
+ @m.set @key2, @marshalled, 0, true
89
+ @m.set @key3, @marshalled, 0, true
90
+ @m.set @key1, @marshalled, 0, true
91
+ @m.set @key2, @marshalled, 0, true
92
+ @m.set @key3, @marshalled, 0, true
93
+ end
94
+ end
95
+ end
96
+
97
+ # restart_servers
98
+
99
+
100
+ if defined? Memcached
101
+ @m = Memcached.new(
102
+ @opts[0],
103
+ @opts[1].merge(:no_block => true, :buffer_requests => true)
104
+ )
105
+ x.report("set:ruby:noblock:memcached") do
106
+ n.times do
107
+ @m.set @key1, @value
108
+ @m.set @key2, @value
109
+ @m.set @key3, @value
110
+ @m.set @key1, @value
111
+ @m.set @key2, @value
112
+ @m.set @key3, @value
113
+ end
114
+ end
115
+ @m = Memcached.new(*@opts)
116
+ x.report("set:ruby:memcached") do
117
+ n.times do
118
+ @m.set @key1, @value
119
+ @m.set @key2, @value
120
+ @m.set @key3, @value
121
+ @m.set @key1, @value
122
+ @m.set @key2, @value
123
+ @m.set @key3, @value
124
+ end
125
+ end # if false
126
+ end
127
+ if defined? Caffeine
128
+ @m = Caffeine::MemCache.new(@opts[1]); @m.servers = @opts[0]
129
+ x.report("set:ruby:caffeine") do
130
+ n.times do
131
+ @m.set @key1, @value
132
+ @m.set @key2, @value
133
+ @m.set @key3, @value
134
+ @m.set @key1, @value
135
+ @m.set @key2, @value
136
+ @m.set @key3, @value
137
+ end
138
+ end
139
+ end
140
+ if defined? MemCache
141
+ @m = MemCache.new(*@opts)
142
+ x.report("set:ruby:memcache-client") do
143
+ n.times do
144
+ @m.set @key1, @value
145
+ @m.set @key2, @value
146
+ @m.set @key3, @value
147
+ @m.set @key1, @value
148
+ @m.set @key2, @value
149
+ @m.set @key3, @value
150
+ end
151
+ end
152
+ end
153
+
154
+ if defined? Memcached
155
+ @m = Memcached.new(*@opts)
156
+ x.report("get:plain:memcached") do
157
+ n.times do
158
+ @m.get @key1, false
159
+ @m.get @key2, false
160
+ @m.get @key3, false
161
+ @m.get @key1, false
162
+ @m.get @key2, false
163
+ @m.get @key3, false
164
+ end
165
+ end
166
+ end
167
+ # Not supported by Caffeine
168
+ if defined? MemCache
169
+ @m = MemCache.new(*@opts)
170
+ x.report("get:plain:memcache-client") do
171
+ n.times do
172
+ @m.get @key1, true
173
+ @m.get @key2, true
174
+ @m.get @key3, true
175
+ @m.get @key1, true
176
+ @m.get @key2, true
177
+ @m.get @key3, true
178
+ end
179
+ end
180
+ end
181
+
182
+ if defined? Memcached
183
+ @m = Memcached.new(*@opts)
184
+ x.report("get:ruby:memcached") do
185
+ n.times do
186
+ @m.get @key1
187
+ @m.get @key2
188
+ @m.get @key3
189
+ @m.get @key1
190
+ @m.get @key2
191
+ @m.get @key3
192
+ end
193
+ end
194
+ end
195
+ if defined? Caffeine
196
+ @m = Caffeine::MemCache.new(@opts[1]); @m.servers = @opts[0]
197
+ x.report("get:ruby:caffeine") do
198
+ n.times do
199
+ @m.get @key1
200
+ @m.get @key2
201
+ @m.get @key3
202
+ @m.get @key1
203
+ @m.get @key2
204
+ @m.get @key3
205
+ end
206
+ end
207
+ end
208
+ if defined? MemCache
209
+ @m = MemCache.new(*@opts)
210
+ x.report("get:ruby:memcache-client") do
211
+ n.times do
212
+ @m.get @key1
213
+ @m.get @key2
214
+ @m.get @key3
215
+ @m.get @key1
216
+ @m.get @key2
217
+ @m.get @key3
218
+ end
219
+ end
220
+ end
221
+
222
+ # restart_servers
223
+
224
+ if defined? Memcached
225
+ @m = Memcached.new(*@opts)
226
+ x.report("missing:ruby:memcached") do
227
+ n.times do
228
+ begin @m.delete @key1; rescue Memcached::NotFound; end
229
+ begin @m.get @key1; rescue Memcached::NotFound; end
230
+ begin @m.delete @key2; rescue Memcached::NotFound; end
231
+ begin @m.get @key2; rescue Memcached::NotFound; end
232
+ begin @m.delete @key3; rescue Memcached::NotFound; end
233
+ begin @m.get @key3; rescue Memcached::NotFound; end
234
+ end
235
+ end
236
+ end
237
+ if defined? Caffeine
238
+ @m = Caffeine::MemCache.new(@opts[1]); @m.servers = @opts[0]
239
+ x.report("missing:ruby:caffeine") do
240
+ n.times do
241
+ begin @m.delete @key1; rescue; end
242
+ begin @m.get @key1; rescue; end
243
+ begin @m.delete @key2; rescue; end
244
+ begin @m.get @key2; rescue; end
245
+ begin @m.delete @key3; rescue; end
246
+ begin @m.get @key3; rescue; end
247
+ end
248
+ end
249
+ end
250
+ if defined? MemCache
251
+ @m = MemCache.new(*@opts)
252
+ x.report("missing:ruby:memcache-client") do
253
+ n.times do
254
+ begin @m.delete @key1; rescue; end
255
+ begin @m.get @key1; rescue; end
256
+ begin @m.delete @key2; rescue; end
257
+ begin @m.get @key2; rescue; end
258
+ begin @m.delete @key3; rescue; end
259
+ begin @m.get @key3; rescue; end
260
+ end
261
+ end
262
+ end
263
+
264
+ # restart_servers
265
+
266
+ if defined? Memcached
267
+ @m = Memcached.new(
268
+ @opts[0],
269
+ @opts[1].merge(:no_block => true, :buffer_requests => true)
270
+ )
271
+ x.report("mixed:ruby:noblock:memcached") do
272
+ n.times do
273
+ @m.set @key1, @value
274
+ @m.set @key2, @value
275
+ @m.set @key3, @value
276
+ @m.get @key1
277
+ @m.get @key2
278
+ @m.get @key3
279
+ @m.set @key1, @value
280
+ @m.get @key1
281
+ @m.set @key2, @value
282
+ @m.get @key2
283
+ @m.set @key3, @value
284
+ @m.get @key3
285
+ end
286
+ end
287
+ @m = Memcached.new(*@opts)
288
+ x.report("mixed:ruby:memcached") do
289
+ n.times do
290
+ @m.set @key1, @value
291
+ @m.set @key2, @value
292
+ @m.set @key3, @value
293
+ @m.get @key1
294
+ @m.get @key2
295
+ @m.get @key3
296
+ @m.set @key1, @value
297
+ @m.get @key1
298
+ @m.set @key2, @value
299
+ @m.get @key2
300
+ @m.set @key3, @value
301
+ @m.get @key3
302
+ end
303
+ end # if false
304
+ end
305
+ if defined? Caffeine
306
+ @m = Caffeine::MemCache.new(@opts[1]); @m.servers = @opts[0]
307
+ x.report("mixed:ruby:caffeine") do
308
+ n.times do
309
+ @m.set @key1, @value
310
+ @m.set @key2, @value
311
+ @m.set @key3, @value
312
+ @m.get @key1
313
+ @m.get @key2
314
+ @m.get @key3
315
+ @m.set @key1, @value
316
+ @m.get @key1
317
+ @m.set @key2, @value
318
+ @m.get @key2
319
+ @m.set @key3, @value
320
+ @m.get @key3
321
+ end
322
+ end
323
+ end
324
+ if defined? MemCache
325
+ @m = MemCache.new(*@opts)
326
+ x.report("mixed:ruby:memcache-client") do
327
+ n.times do
328
+ @m.set @key1, @value
329
+ @m.set @key2, @value
330
+ @m.set @key3, @value
331
+ @m.get @key1
332
+ @m.get @key2
333
+ @m.get @key3
334
+ @m.set @key1, @value
335
+ @m.get @key1
336
+ @m.set @key2, @value
337
+ @m.get @key2
338
+ @m.set @key3, @value
339
+ @m.get @key3
340
+ end
341
+ end
342
+ end
343
+
344
+ # restart_servers
345
+
346
+ if defined? Memcached
347
+ unless ARGV.include? "--no-hash"
348
+ n = 10000
349
+ Memcached::HASH_VALUES.each do |mode,|
350
+ @m = Memcached.new(@opts[0], @opts[1].merge(:hash => mode))
351
+ x.report("hash:#{mode}:memcached") do
352
+ n.times do
353
+ @m.set @key1, @marshalled, 0, false
354
+ @m.get @key1, false
355
+ @m.set @key2, @marshalled, 0, false
356
+ @m.get @key2, false
357
+ @m.set @key3, @marshalled, 0, false
358
+ @m.get @key3, false
359
+ end
360
+ end
361
+ end
362
+ end
363
+
364
+ end
365
+ end
366
+
367
+ if Process.respond_to? :memory
368
+ Process.memory.each do |key, value|
369
+ puts "#{key}: #{value/1024.0}M"
370
+ end
371
+ end
@@ -0,0 +1,14 @@
1
+
2
+ HERE = File.dirname(__FILE__)
3
+ $LOAD_PATH << "#{HERE}/../../lib/"
4
+
5
+ require 'rubygems'
6
+ require 'memcached'
7
+ require 'ruby-prof'
8
+
9
+ result = RubyProf.profile do
10
+ load "#{HERE}/valgrind.rb"
11
+ end
12
+
13
+ printer = RubyProf::GraphPrinter.new(result)
14
+ printer.print(STDOUT, 0)
@@ -0,0 +1,133 @@
1
+
2
+ HERE = File.dirname(__FILE__)
3
+ $LOAD_PATH << "#{HERE}/../../lib/"
4
+
5
+ require 'memcached'
6
+ require 'rubygems'
7
+
8
+ class Worker
9
+ def initialize(method_name, iterations)
10
+ @method = method_name || 'mixed'
11
+ @i = (iterations || 1000).to_i
12
+
13
+ @key1 = "key1-"*8
14
+ @key2 = "key2-"*8
15
+
16
+ @value = []
17
+ @marshalled = Marshal.dump(@value)
18
+
19
+ @opts = [
20
+ ['127.0.0.1:43042', '127.0.0.1:43043'],
21
+ {
22
+ :buffer_requests => false,
23
+ :no_block => false,
24
+ :namespace => "benchmark_namespace"
25
+ }
26
+ ]
27
+ system("ruby #{HERE}/../setup.rb")
28
+ sleep(1)
29
+ @cache = Memcached.new(*@opts)
30
+
31
+ @cache.set @key1, @value
32
+ end
33
+
34
+ def work
35
+ case @method
36
+ when "set"
37
+ @i.times do
38
+ @cache.set @key1, @value
39
+ end
40
+ when "get"
41
+ @i.times do
42
+ @cache.get @key1
43
+ end
44
+ when "delete"
45
+ @i.times do
46
+ @cache.set @key1, @value
47
+ @cache.delete @key1
48
+ end
49
+ when "delete-miss"
50
+ @i.times do
51
+ @cache.delete @key1
52
+ end
53
+ when "get-miss"
54
+ @i.times do
55
+ begin
56
+ @cache.get @key2
57
+ rescue Memcached::NotFound
58
+ end
59
+ end
60
+ when "get-increasing"
61
+ one_k = "x"*1024
62
+ @i.times do |i|
63
+ @cache.set @key1, one_k*(i+1), 0, false
64
+ @cache.get @key1, false
65
+ end
66
+ when "get-miss-increasing"
67
+ @i.times do |i|
68
+ @cache.delete @key2 rescue nil
69
+ begin
70
+ @cache.get @key2
71
+ rescue Memcached::NotFound
72
+ end
73
+ end
74
+ when "add"
75
+ @i.times do
76
+ begin
77
+ @cache.delete @key1
78
+ rescue
79
+ end
80
+ @cache.add @key1, @value
81
+ end
82
+ when "add-present"
83
+ @cache.set @key1, @value
84
+ @i.times do
85
+ begin
86
+ @cache.add @key1, @value
87
+ rescue Memcached::NotStored
88
+ end
89
+ end
90
+ when "mixed"
91
+ @i.times do
92
+ @cache.set @key1, @value
93
+ @cache.get @key1
94
+ end
95
+ when "stats"
96
+ @i.times do
97
+ @cache.stats
98
+ end
99
+ when "multiget"
100
+ @i.times do
101
+ @cache.get([@key1, @key2])
102
+ end
103
+ when "clone"
104
+ @i.times do
105
+ cache = @cache.clone
106
+ cache.destroy(false)
107
+ end
108
+ when "clone-nodestroy"
109
+ @i.times do
110
+ @cache.clone
111
+ end
112
+ when "servers"
113
+ @i.times do
114
+ @cache.servers
115
+ end
116
+ else
117
+ raise "No such method"
118
+ end
119
+
120
+ @cache.destroy
121
+ end
122
+
123
+ end
124
+
125
+ Worker.new(ENV['METHOD'], ENV['LOOPS']).work
126
+
127
+ begin
128
+ require 'memory'
129
+ Process.memory.each do |key, value|
130
+ puts "#{key}: #{value/1024.0}M"
131
+ end
132
+ rescue LoadError
133
+ end