discourse-redis 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.travis.yml +59 -0
- data/.travis/Gemfile +11 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +349 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +328 -0
- data/Rakefile +87 -0
- data/benchmarking/logging.rb +71 -0
- data/benchmarking/pipeline.rb +51 -0
- data/benchmarking/speed.rb +21 -0
- data/benchmarking/suite.rb +24 -0
- data/benchmarking/worker.rb +71 -0
- data/examples/basic.rb +15 -0
- data/examples/consistency.rb +114 -0
- data/examples/dist_redis.rb +43 -0
- data/examples/incr-decr.rb +17 -0
- data/examples/list.rb +26 -0
- data/examples/pubsub.rb +37 -0
- data/examples/sentinel.rb +41 -0
- data/examples/sentinel/start +49 -0
- data/examples/sets.rb +36 -0
- data/examples/unicorn/config.ru +3 -0
- data/examples/unicorn/unicorn.rb +20 -0
- data/lib/redis.rb +2731 -0
- data/lib/redis/client.rb +575 -0
- data/lib/redis/connection.rb +9 -0
- data/lib/redis/connection/command_helper.rb +44 -0
- data/lib/redis/connection/hiredis.rb +64 -0
- data/lib/redis/connection/registry.rb +12 -0
- data/lib/redis/connection/ruby.rb +322 -0
- data/lib/redis/connection/synchrony.rb +124 -0
- data/lib/redis/distributed.rb +873 -0
- data/lib/redis/errors.rb +40 -0
- data/lib/redis/hash_ring.rb +132 -0
- data/lib/redis/pipeline.rb +141 -0
- data/lib/redis/subscribe.rb +83 -0
- data/lib/redis/version.rb +3 -0
- data/redis.gemspec +34 -0
- data/test/bitpos_test.rb +69 -0
- data/test/blocking_commands_test.rb +42 -0
- data/test/command_map_test.rb +30 -0
- data/test/commands_on_hashes_test.rb +21 -0
- data/test/commands_on_hyper_log_log_test.rb +21 -0
- data/test/commands_on_lists_test.rb +20 -0
- data/test/commands_on_sets_test.rb +77 -0
- data/test/commands_on_sorted_sets_test.rb +137 -0
- data/test/commands_on_strings_test.rb +101 -0
- data/test/commands_on_value_types_test.rb +133 -0
- data/test/connection_handling_test.rb +250 -0
- data/test/distributed_blocking_commands_test.rb +46 -0
- data/test/distributed_commands_on_hashes_test.rb +10 -0
- data/test/distributed_commands_on_hyper_log_log_test.rb +33 -0
- data/test/distributed_commands_on_lists_test.rb +22 -0
- data/test/distributed_commands_on_sets_test.rb +83 -0
- data/test/distributed_commands_on_sorted_sets_test.rb +18 -0
- data/test/distributed_commands_on_strings_test.rb +59 -0
- data/test/distributed_commands_on_value_types_test.rb +95 -0
- data/test/distributed_commands_requiring_clustering_test.rb +164 -0
- data/test/distributed_connection_handling_test.rb +23 -0
- data/test/distributed_internals_test.rb +79 -0
- data/test/distributed_key_tags_test.rb +52 -0
- data/test/distributed_persistence_control_commands_test.rb +26 -0
- data/test/distributed_publish_subscribe_test.rb +92 -0
- data/test/distributed_remote_server_control_commands_test.rb +66 -0
- data/test/distributed_scripting_test.rb +102 -0
- data/test/distributed_sorting_test.rb +20 -0
- data/test/distributed_test.rb +58 -0
- data/test/distributed_transactions_test.rb +32 -0
- data/test/encoding_test.rb +18 -0
- data/test/error_replies_test.rb +59 -0
- data/test/fork_safety_test.rb +65 -0
- data/test/helper.rb +232 -0
- data/test/helper_test.rb +24 -0
- data/test/internals_test.rb +437 -0
- data/test/lint/blocking_commands.rb +150 -0
- data/test/lint/hashes.rb +162 -0
- data/test/lint/hyper_log_log.rb +60 -0
- data/test/lint/lists.rb +143 -0
- data/test/lint/sets.rb +125 -0
- data/test/lint/sorted_sets.rb +316 -0
- data/test/lint/strings.rb +260 -0
- data/test/lint/value_types.rb +122 -0
- data/test/persistence_control_commands_test.rb +26 -0
- data/test/pipelining_commands_test.rb +242 -0
- data/test/publish_subscribe_test.rb +254 -0
- data/test/remote_server_control_commands_test.rb +118 -0
- data/test/scanning_test.rb +413 -0
- data/test/scripting_test.rb +78 -0
- data/test/sentinel_command_test.rb +80 -0
- data/test/sentinel_test.rb +255 -0
- data/test/sorting_test.rb +59 -0
- data/test/support/connection/hiredis.rb +1 -0
- data/test/support/connection/ruby.rb +1 -0
- data/test/support/connection/synchrony.rb +17 -0
- data/test/support/redis_mock.rb +119 -0
- data/test/support/wire/synchrony.rb +24 -0
- data/test/support/wire/thread.rb +5 -0
- data/test/synchrony_driver.rb +88 -0
- data/test/test.conf.erb +9 -0
- data/test/thread_safety_test.rb +32 -0
- data/test/transactions_test.rb +264 -0
- data/test/unknown_commands_test.rb +14 -0
- data/test/url_param_test.rb +138 -0
- metadata +182 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ezra Zygmuntowicz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,328 @@
|
|
1
|
+
# redis-rb [![Build Status][travis-image]][travis-link] [![Inline docs][inchpages-image]][inchpages-link]
|
2
|
+
|
3
|
+
[travis-image]: https://secure.travis-ci.org/redis/redis-rb.png?branch=master
|
4
|
+
[travis-link]: http://travis-ci.org/redis/redis-rb
|
5
|
+
[travis-home]: http://travis-ci.org/
|
6
|
+
[inchpages-image]: http://inch-ci.org/github/redis/redis-rb.png
|
7
|
+
[inchpages-link]: http://inch-ci.org/github/redis/redis-rb
|
8
|
+
|
9
|
+
A Ruby client library for [Redis][redis-home].
|
10
|
+
|
11
|
+
[redis-home]: http://redis.io
|
12
|
+
|
13
|
+
A Ruby client that tries to match Redis' API one-to-one, while still
|
14
|
+
providing an idiomatic interface. It features thread-safety, client-side
|
15
|
+
sharding, pipelining, and an obsession for performance.
|
16
|
+
|
17
|
+
## Upgrading from 2.x to 3.0
|
18
|
+
|
19
|
+
Please refer to the [CHANGELOG][changelog-3.0.0] for a summary of the
|
20
|
+
most important changes, as well as a full list of changes.
|
21
|
+
|
22
|
+
[changelog-3.0.0]: https://github.com/redis/redis-rb/blob/master/CHANGELOG.md#300
|
23
|
+
|
24
|
+
## Getting started
|
25
|
+
|
26
|
+
To install **redis-rb**, run the following command:
|
27
|
+
|
28
|
+
```
|
29
|
+
gem install redis
|
30
|
+
```
|
31
|
+
|
32
|
+
Or if you are using **bundler**, add
|
33
|
+
|
34
|
+
```
|
35
|
+
gem 'redis', '~>3.2'
|
36
|
+
```
|
37
|
+
|
38
|
+
to your `Gemfile`, and run `bundle install`
|
39
|
+
|
40
|
+
As of version 2.0 this client only targets Redis version 2.0 and higher.
|
41
|
+
You can use an older version of this client if you need to interface
|
42
|
+
with a Redis instance older than 2.0, but this is no longer supported.
|
43
|
+
|
44
|
+
You can connect to Redis by instantiating the `Redis` class:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require "redis"
|
48
|
+
|
49
|
+
redis = Redis.new
|
50
|
+
```
|
51
|
+
|
52
|
+
This assumes Redis was started with a default configuration, and is
|
53
|
+
listening on `localhost`, port 6379. If you need to connect to a remote
|
54
|
+
server or a different port, try:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
redis = Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
|
58
|
+
```
|
59
|
+
|
60
|
+
You can also specify connection options as a [`redis://` URL][redis-url]:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
redis = Redis.new(:url => "redis://:p4ssw0rd@10.0.1.1:6380/15")
|
64
|
+
```
|
65
|
+
|
66
|
+
[redis-url]: http://www.iana.org/assignments/uri-schemes/prov/redis
|
67
|
+
|
68
|
+
By default, the client will try to read the `REDIS_URL` environment variable
|
69
|
+
and use that as URL to connect to. The above statement is therefore equivalent
|
70
|
+
to setting this environment variable and calling `Redis.new` without arguments.
|
71
|
+
|
72
|
+
To connect to Redis listening on a Unix socket, try:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
redis = Redis.new(:path => "/tmp/redis.sock")
|
76
|
+
```
|
77
|
+
|
78
|
+
To connect to a password protected Redis instance, use:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
redis = Redis.new(:password => "mysecret")
|
82
|
+
```
|
83
|
+
|
84
|
+
The Redis class exports methods that are named identical to the commands
|
85
|
+
they execute. The arguments these methods accept are often identical to
|
86
|
+
the arguments specified on the [Redis website][redis-commands]. For
|
87
|
+
instance, the `SET` and `GET` commands can be called like this:
|
88
|
+
|
89
|
+
[redis-commands]: http://redis.io/commands
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
redis.set("mykey", "hello world")
|
93
|
+
# => "OK"
|
94
|
+
|
95
|
+
redis.get("mykey")
|
96
|
+
# => "hello world"
|
97
|
+
```
|
98
|
+
|
99
|
+
All commands, their arguments and return values are documented, and
|
100
|
+
available on [rdoc.info][rdoc].
|
101
|
+
|
102
|
+
[rdoc]: http://rdoc.info/github/redis/redis-rb/
|
103
|
+
|
104
|
+
## Sentinel support
|
105
|
+
|
106
|
+
The client is able to perform automatic failovers by using [Redis
|
107
|
+
Sentinel](http://redis.io/topics/sentinel). Make sure to run Redis 2.8+
|
108
|
+
if you want to use this feature.
|
109
|
+
|
110
|
+
To connect using Sentinel, use:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
SENTINELS = [{:host => "127.0.0.1", :port => 26380},
|
114
|
+
{:host => "127.0.0.1", :port => 26381}]
|
115
|
+
|
116
|
+
redis = Redis.new(:url => "redis://mymaster", :sentinels => SENTINELS, :role => :master)
|
117
|
+
```
|
118
|
+
|
119
|
+
* The master name identifies a group of Redis instances composed of a master
|
120
|
+
and one or more slaves (`mymaster` in the example).
|
121
|
+
|
122
|
+
* It is possible to optionally provide a role. The allowed roles are `master`
|
123
|
+
and `slave`. When the role is `slave`, the client will try to connect to a
|
124
|
+
random slave of the specified master. If a role is not specified, the client
|
125
|
+
will connect to the master.
|
126
|
+
|
127
|
+
* When using the Sentinel support you need to specify a list of sentinels to
|
128
|
+
connect to. The list does not need to enumerate all your Sentinel instances,
|
129
|
+
but a few so that if one is down the client will try the next one. The client
|
130
|
+
is able to remember the last Sentinel that was able to reply correctly and will
|
131
|
+
use it for the next requests.
|
132
|
+
|
133
|
+
## Storing objects
|
134
|
+
|
135
|
+
Redis only stores strings as values. If you want to store an object, you
|
136
|
+
can use a serialization mechanism such as JSON:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
require "json"
|
140
|
+
|
141
|
+
redis.set "foo", [1, 2, 3].to_json
|
142
|
+
# => OK
|
143
|
+
|
144
|
+
JSON.parse(redis.get("foo"))
|
145
|
+
# => [1, 2, 3]
|
146
|
+
```
|
147
|
+
|
148
|
+
## Pipelining
|
149
|
+
|
150
|
+
When multiple commands are executed sequentially, but are not dependent,
|
151
|
+
the calls can be *pipelined*. This means that the client doesn't wait
|
152
|
+
for reply of the first command before sending the next command. The
|
153
|
+
advantage is that multiple commands are sent at once, resulting in
|
154
|
+
faster overall execution.
|
155
|
+
|
156
|
+
The client can be instructed to pipeline commands by using the
|
157
|
+
`#pipelined` method. After the block is executed, the client sends all
|
158
|
+
commands to Redis and gathers their replies. These replies are returned
|
159
|
+
by the `#pipelined` method.
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
redis.pipelined do
|
163
|
+
redis.set "foo", "bar"
|
164
|
+
redis.incr "baz"
|
165
|
+
end
|
166
|
+
# => ["OK", 1]
|
167
|
+
```
|
168
|
+
|
169
|
+
### Executing commands atomically
|
170
|
+
|
171
|
+
You can use `MULTI/EXEC` to run a number of commands in an atomic
|
172
|
+
fashion. This is similar to executing a pipeline, but the commands are
|
173
|
+
preceded by a call to `MULTI`, and followed by a call to `EXEC`. Like
|
174
|
+
the regular pipeline, the replies to the commands are returned by the
|
175
|
+
`#multi` method.
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
redis.multi do
|
179
|
+
redis.set "foo", "bar"
|
180
|
+
redis.incr "baz"
|
181
|
+
end
|
182
|
+
# => ["OK", 1]
|
183
|
+
```
|
184
|
+
|
185
|
+
### Futures
|
186
|
+
|
187
|
+
Replies to commands in a pipeline can be accessed via the *futures* they
|
188
|
+
emit (since redis-rb 3.0). All calls inside a pipeline block return a
|
189
|
+
`Future` object, which responds to the `#value` method. When the
|
190
|
+
pipeline has successfully executed, all futures are assigned their
|
191
|
+
respective replies and can be used.
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
redis.pipelined do
|
195
|
+
@set = redis.set "foo", "bar"
|
196
|
+
@incr = redis.incr "baz"
|
197
|
+
end
|
198
|
+
|
199
|
+
@set.value
|
200
|
+
# => "OK"
|
201
|
+
|
202
|
+
@incr.value
|
203
|
+
# => 1
|
204
|
+
```
|
205
|
+
|
206
|
+
## Error Handling
|
207
|
+
|
208
|
+
In general, if something goes wrong you'll get an exception. For example, if
|
209
|
+
it can't connect to the server a `Redis::CannotConnectError` error will be raised.
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
begin
|
213
|
+
redis.ping
|
214
|
+
rescue Exception => e
|
215
|
+
e.inspect
|
216
|
+
# => #<Redis::CannotConnectError: Timed out connecting to Redis on 10.0.1.1:6380>
|
217
|
+
|
218
|
+
e.message
|
219
|
+
# => Timed out connecting to Redis on 10.0.1.1:6380
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
See lib/redis/errors.rb for information about what exceptions are possible.
|
224
|
+
|
225
|
+
|
226
|
+
## Expert-Mode Options
|
227
|
+
|
228
|
+
- `inherit_socket: true`: disable safety check that prevents a forked child
|
229
|
+
from sharing a socket with its parent; this is potentially useful in order to mitigate connection churn when:
|
230
|
+
- many short-lived forked children of one process need to talk
|
231
|
+
to redis, AND
|
232
|
+
- your own code prevents the parent process from using the redis
|
233
|
+
connection while a child is alive
|
234
|
+
|
235
|
+
Improper use of `inherit_socket` will result in corrupted and/or incorrect
|
236
|
+
responses.
|
237
|
+
|
238
|
+
## Alternate drivers
|
239
|
+
|
240
|
+
By default, redis-rb uses Ruby's socket library to talk with Redis.
|
241
|
+
To use an alternative connection driver it should be specified as option
|
242
|
+
when instantiating the client object. These instructions are only valid
|
243
|
+
for **redis-rb 3.0**. For instructions on how to use alternate drivers from
|
244
|
+
**redis-rb 2.2**, please refer to an [older README][readme-2.2.2].
|
245
|
+
|
246
|
+
[readme-2.2.2]: https://github.com/redis/redis-rb/blob/v2.2.2/README.md
|
247
|
+
|
248
|
+
### hiredis
|
249
|
+
|
250
|
+
The hiredis driver uses the connection facility of hiredis-rb. In turn,
|
251
|
+
hiredis-rb is a binding to the official hiredis client library. It
|
252
|
+
optimizes for speed, at the cost of portability. Because it is a C
|
253
|
+
extension, JRuby is not supported (by default).
|
254
|
+
|
255
|
+
It is best to use hiredis when you have large replies (for example:
|
256
|
+
`LRANGE`, `SMEMBERS`, `ZRANGE`, etc.) and/or use big pipelines.
|
257
|
+
|
258
|
+
In your Gemfile, include hiredis:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
gem "redis", "~> 3.0.1"
|
262
|
+
gem "hiredis", "~> 0.4.5"
|
263
|
+
```
|
264
|
+
|
265
|
+
When instantiating the client object, specify hiredis:
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
redis = Redis.new(:driver => :hiredis)
|
269
|
+
```
|
270
|
+
|
271
|
+
### synchrony
|
272
|
+
|
273
|
+
The synchrony driver adds support for [em-synchrony][em-synchrony].
|
274
|
+
This makes redis-rb work with EventMachine's asynchronous I/O, while not
|
275
|
+
changing the exposed API. The hiredis gem needs to be available as
|
276
|
+
well, because the synchrony driver uses hiredis for parsing the Redis
|
277
|
+
protocol.
|
278
|
+
|
279
|
+
[em-synchrony]: https://github.com/igrigorik/em-synchrony
|
280
|
+
|
281
|
+
In your Gemfile, include em-synchrony and hiredis:
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
gem "redis", "~> 3.0.1"
|
285
|
+
gem "hiredis", "~> 0.4.5"
|
286
|
+
gem "em-synchrony"
|
287
|
+
```
|
288
|
+
|
289
|
+
When instantiating the client object, specify synchrony:
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
redis = Redis.new(:driver => :synchrony)
|
293
|
+
```
|
294
|
+
|
295
|
+
## Testing
|
296
|
+
|
297
|
+
This library is tested using [Travis][travis-home], where it is tested
|
298
|
+
against the following interpreters and drivers:
|
299
|
+
|
300
|
+
* MRI 1.8.7 (drivers: ruby, hiredis)
|
301
|
+
* MRI 1.9.2 (drivers: ruby, hiredis, synchrony)
|
302
|
+
* MRI 1.9.3 (drivers: ruby, hiredis, synchrony)
|
303
|
+
* MRI 2.0.0 (drivers: ruby, hiredis, synchrony)
|
304
|
+
* JRuby 1.7 (1.8 mode) (drivers: ruby)
|
305
|
+
* JRuby 1.7 (1.9 mode) (drivers: ruby)
|
306
|
+
|
307
|
+
## Contributors
|
308
|
+
|
309
|
+
(ordered chronologically with more than 5 commits, see `git shortlog -sn` for
|
310
|
+
all contributors)
|
311
|
+
|
312
|
+
* Ezra Zygmuntowicz
|
313
|
+
* Taylor Weibley
|
314
|
+
* Matthew Clark
|
315
|
+
* Brian McKinney
|
316
|
+
* Luca Guidi
|
317
|
+
* Salvatore Sanfilippo
|
318
|
+
* Chris Wanstrath
|
319
|
+
* Damian Janowski
|
320
|
+
* Michel Martens
|
321
|
+
* Nick Quaranto
|
322
|
+
* Pieter Noordhuis
|
323
|
+
* Ilya Grigorik
|
324
|
+
|
325
|
+
## Contributing
|
326
|
+
|
327
|
+
[Fork the project](https://github.com/redis/redis-rb) and send pull
|
328
|
+
requests. You can also ask for help at `#redis-rb` on Freenode.
|
data/Rakefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require "rake/testtask"
|
2
|
+
|
3
|
+
ENV["REDIS_BRANCH"] ||= "unstable"
|
4
|
+
|
5
|
+
REDIS_DIR = File.expand_path(File.join("..", "test"), __FILE__)
|
6
|
+
REDIS_CNF = File.join(REDIS_DIR, "test.conf")
|
7
|
+
REDIS_CNF_TEMPLATE = File.join(REDIS_DIR, "test.conf.erb")
|
8
|
+
REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid")
|
9
|
+
REDIS_LOG = File.join(REDIS_DIR, "db", "redis.log")
|
10
|
+
REDIS_SOCKET = File.join(REDIS_DIR, "db", "redis.sock")
|
11
|
+
BINARY = "tmp/redis-#{ENV["REDIS_BRANCH"]}/src/redis-server"
|
12
|
+
|
13
|
+
task :default => :run
|
14
|
+
|
15
|
+
desc "Run tests and manage server start/stop"
|
16
|
+
task :run => [:start, :test, :stop]
|
17
|
+
|
18
|
+
desc "Start the Redis server"
|
19
|
+
task :start => [BINARY, REDIS_CNF] do
|
20
|
+
sh "#{BINARY} --version"
|
21
|
+
|
22
|
+
redis_running = \
|
23
|
+
begin
|
24
|
+
File.exists?(REDIS_PID) && Process.kill(0, File.read(REDIS_PID).to_i)
|
25
|
+
rescue Errno::ESRCH
|
26
|
+
FileUtils.rm REDIS_PID
|
27
|
+
false
|
28
|
+
end
|
29
|
+
|
30
|
+
unless redis_running
|
31
|
+
unless system("#{BINARY} #{REDIS_CNF}")
|
32
|
+
abort "could not start redis-server"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
at_exit do
|
37
|
+
Rake::Task["stop"].invoke
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "Stop the Redis server"
|
42
|
+
task :stop do
|
43
|
+
if File.exists?(REDIS_PID)
|
44
|
+
Process.kill "INT", File.read(REDIS_PID).to_i
|
45
|
+
FileUtils.rm REDIS_PID
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "Clean up testing artifacts"
|
50
|
+
task :clean do
|
51
|
+
FileUtils.rm_f(BINARY)
|
52
|
+
FileUtils.rm_f(REDIS_CNF)
|
53
|
+
end
|
54
|
+
|
55
|
+
file BINARY do
|
56
|
+
branch = ENV.fetch("REDIS_BRANCH")
|
57
|
+
|
58
|
+
sh <<-SH
|
59
|
+
mkdir -p tmp;
|
60
|
+
cd tmp;
|
61
|
+
rm -rf redis-#{branch};
|
62
|
+
wget https://github.com/antirez/redis/archive/#{branch}.tar.gz -O #{branch}.tar.gz;
|
63
|
+
tar xf #{branch}.tar.gz;
|
64
|
+
cd redis-#{branch};
|
65
|
+
make
|
66
|
+
SH
|
67
|
+
end
|
68
|
+
|
69
|
+
file REDIS_CNF => [REDIS_CNF_TEMPLATE, __FILE__] do |t|
|
70
|
+
require 'erb'
|
71
|
+
|
72
|
+
erb = t.prerequisites[0]
|
73
|
+
template = File.read(erb)
|
74
|
+
|
75
|
+
File.open(REDIS_CNF, 'w') do |file|
|
76
|
+
file.puts "\# This file was auto-generated at #{Time.now}",
|
77
|
+
"\# from (#{erb})",
|
78
|
+
"\#"
|
79
|
+
conf = ERB.new(template).result
|
80
|
+
file << conf
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
Rake::TestTask.new do |t|
|
85
|
+
t.options = "-v" if $VERBOSE
|
86
|
+
t.test_files = FileList["test/*_test.rb"]
|
87
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Run with
|
2
|
+
#
|
3
|
+
# $ ruby -Ilib benchmarking/logging.rb
|
4
|
+
#
|
5
|
+
|
6
|
+
begin
|
7
|
+
require "bench"
|
8
|
+
rescue LoadError
|
9
|
+
$stderr.puts "`gem install bench` and try again."
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
require "redis"
|
14
|
+
require "logger"
|
15
|
+
|
16
|
+
def log(level, namespace = nil)
|
17
|
+
logger = (namespace || Kernel).const_get(:Logger).new("/dev/null")
|
18
|
+
logger.level = (namespace || Logger).const_get(level)
|
19
|
+
logger
|
20
|
+
end
|
21
|
+
|
22
|
+
def stress(redis)
|
23
|
+
redis.flushdb
|
24
|
+
|
25
|
+
n = (ARGV.shift || 2000).to_i
|
26
|
+
|
27
|
+
n.times do |i|
|
28
|
+
key = "foo:#{i}"
|
29
|
+
redis.set key, i
|
30
|
+
redis.get key
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
default = Redis.new
|
35
|
+
|
36
|
+
logging_redises = [
|
37
|
+
Redis.new(:logger => log(:DEBUG)),
|
38
|
+
Redis.new(:logger => log(:INFO)),
|
39
|
+
]
|
40
|
+
|
41
|
+
begin
|
42
|
+
require "log4r"
|
43
|
+
|
44
|
+
logging_redises += [
|
45
|
+
Redis.new(:logger => log(:DEBUG, Log4r)),
|
46
|
+
Redis.new(:logger => log(:INFO, Log4r)),
|
47
|
+
]
|
48
|
+
rescue LoadError
|
49
|
+
$stderr.puts "Log4r not installed. `gem install log4r` if you want to compare it against Ruby's Logger (spoiler: it's much faster)."
|
50
|
+
end
|
51
|
+
|
52
|
+
benchmark "Default options (no logger)" do
|
53
|
+
stress(default)
|
54
|
+
end
|
55
|
+
|
56
|
+
logging_redises.each do |redis|
|
57
|
+
logger = redis.client.logger
|
58
|
+
|
59
|
+
case logger
|
60
|
+
when Logger
|
61
|
+
level = Logger::SEV_LABEL[logger.level]
|
62
|
+
when Log4r::Logger
|
63
|
+
level = logger.levels[logger.level]
|
64
|
+
end
|
65
|
+
|
66
|
+
benchmark "#{logger.class} on #{level}" do
|
67
|
+
stress(redis)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
run 10
|