Skip to content

Commit b7d8826

Browse files
committed
Benchmark
1 parent d8a827e commit b7d8826

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

Gemfile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ source 'https://rubygems.org'
55
# Specify your gem's dependencies in redis_single_file.gemspec
66
gemspec
77

8+
gem 'pry', '~> 0.15.2'
89
gem 'rake', '~> 13.0'
9-
1010
gem 'rspec', '~> 3.0'
11-
1211
gem 'rubocop', '~> 1.21'
1312
gem 'rubocop-rake', '~> 0.6.0'
1413
gem 'rubocop-rspec', '~> 3.4.0 '
15-
16-
gem 'pry', '~> 0.15.2'
17-
1814
gem 'mock_redis', '~> 0.49.0'
15+
gem 'benchmark', '~> 0.4.0'
16+
gem 'benchmark-ips', '~> 2.14.0'

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ GEM
88
remote: https://rubygems.org/
99
specs:
1010
ast (2.4.2)
11+
benchmark (0.4.0)
12+
benchmark-ips (2.14.0)
1113
coderay (1.1.3)
1214
connection_pool (2.5.0)
1315
diff-lcs (1.5.1)
@@ -70,6 +72,8 @@ PLATFORMS
7072
ruby
7173

7274
DEPENDENCIES
75+
benchmark (~> 0.4.0)
76+
benchmark-ips (~> 2.14.0)
7377
mock_redis (~> 0.49.0)
7478
pry (~> 0.15.2)
7579
rake (~> 13.0)

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ Redlock is not susceptible to this given the use of the multi-master deployment
143143

144144
$ bundle exec rspec
145145

146+
## Benchmark
147+
148+
$ bundle exec ruby benchmark.rb
149+
150+
```ruby
151+
ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin22]
152+
Warming up --------------------------------------
153+
synchronize 434.000 i/100ms
154+
synchronize! 434.000 i/100ms
155+
threaded (10x) 29.000 i/100ms
156+
forked (10x) 8.000 i/100ms
157+
Calculating -------------------------------------
158+
synchronize 4.329k (± 1.9%) i/s (230.98 μs/i) - 21.700k in 5.014460s
159+
synchronize! 4.352k (± 0.3%) i/s (229.79 μs/i) - 22.134k in 5.086272s
160+
threaded (10x) 249.79428.4%) i/s (4.00 ms/i) - 1.073k in 5.058461s
161+
forked (10x) 56.5883.5%) i/s (17.67 ms/i) - 288.000 in 5.097885s
162+
163+
Comparison:
164+
synchronize!: 4351.8 i/s
165+
synchronize: 4329.4 i/s - same-ish: difference falls within error
166+
threaded (10x): 249.8 i/s - 17.42x slower
167+
forked (10x): 56.6 i/s - 76.90x slower
168+
```
169+
146170
## Disclaimer
147171

148172
> [!WARNING]

benchmark.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'benchmark/ips'
4+
require 'redis_single_file'
5+
6+
scenario_1_semaphore = RedisSingleFile.new(name: :scenario_1)
7+
scenario_2_semaphore = RedisSingleFile.new(name: :scenario_2)
8+
9+
Benchmark.ips do |x|
10+
x.report('synchronize') do
11+
scenario_1_semaphore.synchronize { nil }
12+
end
13+
14+
x.report('synchronize!') do
15+
scenario_2_semaphore.synchronize! { nil }
16+
end
17+
18+
x.report('threaded (10x)') do
19+
threads = 10.times.map do
20+
Thread.new do
21+
scenario_3_semaphore = RedisSingleFile.new(name: :scenario_3)
22+
scenario_3_semaphore.synchronize { nil }
23+
end
24+
end
25+
26+
while threads.any?(&:alive?) do
27+
threads.each { _1.join(0.5) }
28+
end
29+
end
30+
31+
x.report('forked (10x)') do
32+
10.times.each do
33+
fork do
34+
scenario_4_semaphore = RedisSingleFile.new(name: :scenario_4)
35+
scenario_4_semaphore.synchronize { nil }
36+
end
37+
end
38+
39+
Process.waitall
40+
end
41+
42+
x.compare!
43+
end

0 commit comments

Comments
 (0)