forked from petergoldstein/dalli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.rb
283 lines (252 loc) · 7.46 KB
/
bench.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# frozen_string_literal: true
require 'ruby-prof'
require "benchmark"
require 'dalli'
def profile(&block)
return yield unless ENV["PROFILE"]
prof = RubyProf::Profile.new
result = prof.profile(&block)
rep = RubyProf::GraphHtmlPrinter.new(result)
rep.print(File.new("profile.html", "w"), min_percent: 1)
end
puts "Testing #{Dalli::VERSION} with #{RUBY_DESCRIPTION}"
class Bencher
def initialize
# We'll use a simple @value to try to avoid spending time in Marshal,
# which is a constant penalty that both clients have to pay
@started = {}
@value = []
@marshalled = Marshal.dump(@value)
@port = 23417
@servers = ["127.0.0.1:#{@port}", "localhost:#{@port}"]
@key1 = "Short"
@key2 = "Sym1-2-3::45" * 8
@key3 = "Long" * 40
@key4 = "Medium" * 8
# 5 and 6 are only used for multiget miss test
@key5 = "Medium2" * 8
@key6 = "Long3" * 40
@counter = "counter"
end
def call
memcached(@port) do
profile do
Benchmark.bm(37) do |x|
n = 2500
@m = Dalli::Client.new(@servers)
x.report("set:plain:dalli") do
n.times do
@m.set @key1, @marshalled, 0, raw: true
@m.set @key2, @marshalled, 0, raw: true
@m.set @key3, @marshalled, 0, raw: true
@m.set @key1, @marshalled, 0, raw: true
@m.set @key2, @marshalled, 0, raw: true
@m.set @key3, @marshalled, 0, raw: true
end
end
@m = Dalli::Client.new(@servers)
x.report("setq:plain:dalli") do
@m.multi do
n.times do
@m.set @key1, @marshalled, 0, raw: true
@m.set @key2, @marshalled, 0, raw: true
@m.set @key3, @marshalled, 0, raw: true
@m.set @key1, @marshalled, 0, raw: true
@m.set @key2, @marshalled, 0, raw: true
@m.set @key3, @marshalled, 0, raw: true
end
end
end
@m = Dalli::Client.new(@servers)
x.report("set:ruby:dalli") do
n.times do
@m.set @key1, @value
@m.set @key2, @value
@m.set @key3, @value
@m.set @key1, @value
@m.set @key2, @value
@m.set @key3, @value
end
end
@m = Dalli::Client.new(@servers)
x.report("get:plain:dalli") do
n.times do
@m.get @key1, raw: true
@m.get @key2, raw: true
@m.get @key3, raw: true
@m.get @key1, raw: true
@m.get @key2, raw: true
@m.get @key3, raw: true
end
end
@m = Dalli::Client.new(@servers)
x.report("get:ruby:dalli") do
n.times do
@m.get @key1
@m.get @key2
@m.get @key3
@m.get @key1
@m.get @key2
@m.get @key3
end
end
@m = Dalli::Client.new(@servers)
x.report("multiget:ruby:dalli") do
n.times do
# We don't use the keys array because splat is slow
@m.get_multi @key1, @key2, @key3, @key4, @key5, @key6
end
end
@m = Dalli::Client.new(@servers)
x.report("missing:ruby:dalli") do
n.times do
begin @m.delete @key1; rescue; end
begin @m.get @key1; rescue; end
begin @m.delete @key2; rescue; end
begin @m.get @key2; rescue; end
begin @m.delete @key3; rescue; end
begin @m.get @key3; rescue; end
end
end
@m = Dalli::Client.new(@servers)
x.report("mixed:ruby:dalli") do
n.times do
@m.set @key1, @value
@m.set @key2, @value
@m.set @key3, @value
@m.get @key1
@m.get @key2
@m.get @key3
@m.set @key1, @value
@m.get @key1
@m.set @key2, @value
@m.get @key2
@m.set @key3, @value
@m.get @key3
end
end
@m = Dalli::Client.new(@servers)
x.report("mixedq:ruby:dalli") do
@m.multi do
n.times do
@m.set @key1, @value
@m.set @key2, @value
@m.set @key3, @value
@m.get @key1
@m.get @key2
@m.get @key3
@m.set @key1, @value
@m.get @key1
@m.set @key2, @value
@m.replace @key2, @value
@m.delete @key3
@m.add @key3, @value
@m.get @key2
@m.set @key3, @value
@m.get @key3
end
end
end
@m = Dalli::Client.new(@servers)
x.report("incr:ruby:dalli") do
counter = "foocount"
n.times do
@m.incr counter, 1, 0, 1
end
n.times do
@m.decr counter, 1
end
result = @m.incr(counter, 0)
raise result if result != 0
end
end
end
end
end
def memcached(port, args = "", client_options = {})
dc = start_and_flush_with_retry(port, args, client_options)
yield dc, port if block_given?
memcached_kill(port)
end
def memcached_kill(port)
pid = @started.delete(port)
if pid
begin
Process.kill("TERM", pid)
Process.wait(pid)
rescue Errno::ECHILD, Errno::ESRCH => e
puts e.inspect
end
end
end
def start_and_flush_with_retry(port, args = "", client_options = {})
dc = nil
retry_count = 0
while dc.nil?
begin
dc = start_and_flush(port, args, client_options, (retry_count == 0))
rescue => e
@started[port] = nil
retry_count += 1
raise e if retry_count >= 3
end
end
dc
end
def start_and_flush(port, args = "", client_options = {}, flush = true)
memcached_server(port, args)
dc = if port.to_i == 0
# unix socket
Dalli::Client.new(port, client_options)
else
Dalli::Client.new(["localhost:#{port}", "127.0.0.1:#{port}"], client_options)
end
dc.flush_all if flush
dc
end
def memcached_server(port, args = "")
Memcached.path ||= find_memcached
if port.to_i == 0
# unix socket
port_socket_arg = "-s"
begin
File.delete(port)
rescue Errno::ENOENT
end
else
port_socket_arg = "-p"
port = port.to_i
end
cmd = "#{Memcached.path}memcached #{args} #{port_socket_arg} #{port}"
@started[port] ||= begin
pid = IO.popen(cmd).pid
at_exit do
Process.kill("TERM", pid)
Process.wait(pid)
rescue Errno::ECHILD, Errno::ESRCH
end
wait_time = 0.1
sleep wait_time
pid
end
end
def find_memcached
output = `memcached -h | head -1`.strip
if output && output =~ /^memcached (\d.\d.\d+)/ && $1 > "1.4"
return (puts "Found #{output} in PATH"; "")
end
PATHS.each do |path|
output = `memcached -h | head -1`.strip
if output && output =~ /^memcached (\d\.\d\.\d+)/ && $1 > "1.4"
return (puts "Found #{output} in #{path}"; path)
end
end
raise Errno::ENOENT, "Unable to find memcached 1.4+ locally"
end
end
module Memcached
class << self
attr_accessor :path
end
end
Bencher.new.call