Skip to content

Commit efe158b

Browse files
committed
Setup and enforce a reasonable rubocop config
1 parent 287276c commit efe158b

File tree

117 files changed

+1159
-1034
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1159
-1034
lines changed

.github/workflows/test.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ on:
88
branches:
99
- "*"
1010
jobs:
11+
lint:
12+
name: Rubocop
13+
timeout-minutes: 30
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Check out code
17+
uses: actions/checkout@v2
18+
- name: Set up Ruby
19+
uses: actions/setup-ruby@v1
20+
with:
21+
ruby-version: "2.4"
22+
- name: Set up Gems
23+
run: |
24+
gem update --system --no-document
25+
gem install bundler --no-document
26+
bundle install --jobs 4 --retry 3 --path=.bundle
27+
- name: Lint
28+
run: bundle exec rubocop
29+
1130
main:
1231
name: Main
1332
timeout-minutes: 30

.rubocop.yml

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
AllCops:
2+
TargetRubyVersion: 2.3
3+
4+
Layout/LineLength:
5+
Max: 120
6+
Exclude:
7+
- 'test/**/*'
8+
9+
Layout/CaseIndentation:
10+
EnforcedStyle: end
11+
12+
Lint/RescueException:
13+
Enabled: false
14+
15+
Lint/SuppressedException:
16+
Enabled: false
17+
18+
Lint/AssignmentInCondition:
19+
Enabled: false
20+
21+
Lint/UnifiedInteger:
22+
Enabled: false
23+
24+
Metrics/ClassLength:
25+
Enabled: false
26+
27+
Metrics/CyclomaticComplexity:
28+
Enabled: false
29+
30+
Metrics/AbcSize:
31+
Enabled: false
32+
33+
Metrics/BlockLength:
34+
Enabled: false
35+
36+
Metrics/MethodLength:
37+
Enabled: false
38+
39+
Metrics/ModuleLength:
40+
Enabled: false
41+
42+
Metrics/ParameterLists:
43+
Enabled: false
44+
45+
Metrics/PerceivedComplexity:
46+
Enabled: false
47+
48+
Style/PercentLiteralDelimiters:
49+
Enabled: false
50+
51+
Style/ParallelAssignment:
52+
Enabled: false
53+
54+
Style/NumericPredicate:
55+
Enabled: false
56+
57+
Style/SignalException:
58+
Exclude:
59+
- 'lib/redis/connection/synchrony.rb'
60+
61+
Style/MethodMissingSuper:
62+
Enabled: false
63+
64+
Style/StringLiterals:
65+
Enabled: false
66+
67+
Style/DoubleNegation:
68+
Enabled: false
69+
70+
Style/MultipleComparison:
71+
Enabled: false
72+
73+
Style/GuardClause:
74+
Enabled: false
75+
76+
Style/Semicolon:
77+
Enabled: false
78+
79+
Style/Documentation:
80+
Enabled: false
81+
82+
Style/FormatStringToken:
83+
Enabled: false
84+
85+
Style/FormatString:
86+
Enabled: false
87+
88+
Style/RescueStandardError:
89+
Enabled: false
90+
91+
Style/WordArray:
92+
Enabled: false
93+
94+
Lint/NonLocalExitFromIterator:
95+
Enabled: false
96+
97+
Lint/EndAlignment:
98+
EnforcedStyleAlignWith: variable
99+
100+
Layout/ElseAlignment:
101+
Enabled: false
102+
103+
Naming/HeredocDelimiterNaming:
104+
Enabled: false
105+
106+
Naming/FileName:
107+
Enabled: false
108+
109+
Naming/RescuedExceptionsVariableName:
110+
Enabled: false
111+
112+
Naming/AccessorMethodName:
113+
Exclude:
114+
- lib/redis/connection/ruby.rb

Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# frozen_string_literal: true
2+
23
source 'https://rubygems.org'
34

45
gemspec
56

6-
gem 'rake'
77
gem 'minitest'
8+
gem 'rake'
9+
gem 'rubocop', '0.81'
810

911
# Using jruby-openssl 0.10.0, we get NPEs in jruby tests: https://github.com/redis/redis-rb/issues/756
1012
platform :jruby do

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'bundler/gem_tasks'
34
require 'rake/testtask'
45
Rake::TestTask.new :test do |t|

benchmarking/cluster.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
HOST = '127.0.0.1'
1313
STANDALONE_PORT = 6381
1414
CLUSTER_PORT = 7000
15-
N = (ARGV.first || 100000).to_i
15+
N = (ARGV.first || 100_000).to_i
1616

1717
rn = Redis.new(host: HOST, port: STANDALONE_PORT)
1818
rc = Redis.new(host: HOST, port: CLUSTER_PORT)

benchmarking/cluster_slot.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
require 'redis'
44
require 'benchmark'
55

6-
N = (ARGV.first || 100000).to_i
6+
N = (ARGV.first || 100_000).to_i
77

88
available_slots = {
99
"127.0.0.1:7000" => [0..5460],
1010
"127.0.0.1:7003" => [0..5460],
11-
"127.0.0.1:7001" => [5461..10922],
12-
"127.0.0.1:7004" => [5461..10922],
13-
"127.0.0.1:7002" => [10923..16383],
14-
"127.0.0.1:7005" => [10923..16383]
11+
"127.0.0.1:7001" => [5461..10_922],
12+
"127.0.0.1:7004" => [5461..10_922],
13+
"127.0.0.1:7002" => [10_923..16_383],
14+
"127.0.0.1:7005" => [10_923..16_383]
1515
}
1616

1717
node_flags = {
@@ -33,4 +33,4 @@
3333

3434
puts GC.stat(:total_allocated_objects) - allocs
3535
end
36-
end
36+
end

benchmarking/logging.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
# Run with
34
#
45
# $ ruby -Ilib benchmarking/logging.rb
@@ -7,7 +8,7 @@
78
begin
89
require "bench"
910
rescue LoadError
10-
$stderr.puts "`gem install bench` and try again."
11+
warn "`gem install bench` and try again."
1112
exit 1
1213
end
1314

@@ -35,19 +36,20 @@ def stress(redis)
3536
default = Redis.new
3637

3738
logging_redises = [
38-
Redis.new(:logger => log(:DEBUG)),
39-
Redis.new(:logger => log(:INFO)),
39+
Redis.new(logger: log(:DEBUG)),
40+
Redis.new(logger: log(:INFO))
4041
]
4142

4243
begin
4344
require "log4r"
4445

4546
logging_redises += [
46-
Redis.new(:logger => log(:DEBUG, Log4r)),
47-
Redis.new(:logger => log(:INFO, Log4r)),
47+
Redis.new(logger: log(:DEBUG, Log4r)),
48+
Redis.new(logger: log(:INFO, Log4r))
4849
]
4950
rescue LoadError
50-
$stderr.puts "Log4r not installed. `gem install log4r` if you want to compare it against Ruby's Logger (spoiler: it's much faster)."
51+
warn "Log4r not installed. `gem install log4r` if you want to compare it against Ruby's " \
52+
"Logger (spoiler: it's much faster)."
5153
end
5254

5355
benchmark "Default options (no logger)" do

benchmarking/pipeline.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# frozen_string_literal: true
2+
23
require "benchmark"
34

4-
$:.push File.join(File.dirname(__FILE__), 'lib')
5+
$LOAD_PATH.push File.join(File.dirname(__FILE__), 'lib')
56

67
require 'redis'
78

8-
ITERATIONS = 10000
9+
ITERATIONS = 10_000
910

1011
@r = Redis.new
1112

benchmarking/speed.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
# Run with
34
#
45
# $ ruby -Ilib benchmarking/speed.rb
@@ -8,7 +9,7 @@
89
require "redis"
910

1011
r = Redis.new
11-
n = (ARGV.shift || 20000).to_i
12+
n = (ARGV.shift || 20_000).to_i
1213

1314
elapsed = Benchmark.realtime do
1415
# n sets, n gets

benchmarking/suite.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'fileutils'
34

45
def run_in_background(command)
@@ -7,17 +8,17 @@ def run_in_background(command)
78

89
def with_all_segments(&block)
910
0.upto(9) do |segment_number|
10-
block_size = 100000
11+
block_size = 100_000
1112
start_index = segment_number * block_size
1213
end_index = start_index + block_size - 1
1314
block.call(start_index, end_index)
1415
end
1516
end
1617

17-
#with_all_segments do |start_index, end_index|
18+
# with_all_segments do |start_index, end_index|
1819
# puts "Initializing keys from #{start_index} to #{end_index}"
1920
# system "ruby worker.rb initialize #{start_index} #{end_index} 0"
20-
#end
21+
# end
2122

2223
with_all_segments do |start_index, end_index|
2324
run_in_background "ruby worker.rb write #{start_index} #{end_index} 10"

benchmarking/worker.rb

+33-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# frozen_string_literal: true
2+
23
BENCHMARK_ROOT = File.dirname(__FILE__)
34
REDIS_ROOT = File.join(BENCHMARK_ROOT, "..", "lib")
45

5-
$: << REDIS_ROOT
6+
$LOAD_PATH << REDIS_ROOT
67
require 'redis'
78
require 'benchmark'
89

@@ -16,7 +17,7 @@ def shift_from_argv
1617
value = ARGV.shift
1718
unless value
1819
show_usage
19-
exit -1
20+
exit(-1)
2021
end
2122
value
2223
end
@@ -25,48 +26,48 @@ def shift_from_argv
2526
start_index = shift_from_argv.to_i
2627
end_index = shift_from_argv.to_i
2728
sleep_msec = shift_from_argv.to_i
28-
sleep_duration = sleep_msec/1000.0
29+
sleep_duration = sleep_msec / 1000.0
2930

3031
redis = Redis.new
3132

3233
case operation
33-
when :initialize
34+
when :initialize
3435

35-
start_index.upto(end_index) do |i|
36-
redis[i] = 0
37-
end
36+
start_index.upto(end_index) do |i|
37+
redis[i] = 0
38+
end
3839

39-
when :clear
40+
when :clear
4041

41-
start_index.upto(end_index) do |i|
42-
redis.delete(i)
43-
end
42+
start_index.upto(end_index) do |i|
43+
redis.delete(i)
44+
end
4445

45-
when :read, :write
46+
when :read, :write
4647

47-
puts "Starting to #{operation} at segment #{end_index + 1}"
48+
puts "Starting to #{operation} at segment #{end_index + 1}"
4849

49-
loop do
50-
t1 = Time.now
51-
start_index.upto(end_index) do |i|
52-
case operation
53-
when :read
54-
redis.get(i)
55-
when :write
56-
redis.incr(i)
57-
else
58-
raise "Unknown operation: #{operation}"
59-
end
60-
sleep sleep_duration
50+
loop do
51+
t1 = Time.now
52+
start_index.upto(end_index) do |i|
53+
case operation
54+
when :read
55+
redis.get(i)
56+
when :write
57+
redis.incr(i)
58+
else
59+
raise "Unknown operation: #{operation}"
6160
end
62-
t2 = Time.now
63-
64-
requests_processed = end_index - start_index
65-
time = t2 - t1
66-
puts "#{t2.strftime("%H:%M")} [segment #{end_index + 1}] : Processed #{requests_processed} requests in #{time} seconds - #{(requests_processed/time).round} requests/sec"
61+
sleep sleep_duration
6762
end
63+
t2 = Time.now
64+
65+
requests_processed = end_index - start_index
66+
time = t2 - t1
67+
puts "#{t2.strftime('%H:%M')} [segment #{end_index + 1}] : Processed #{requests_processed} requests " \
68+
"in #{time} seconds - #{(requests_processed / time).round} requests/sec"
69+
end
6870

6971
else
70-
raise "Unknown operation: #{operation}"
72+
raise "Unknown operation: #{operation}"
7173
end
72-

0 commit comments

Comments
 (0)