Skip to content

Commit 7a9c314

Browse files
authored
test: add benchmark test (#76)
1 parent ae90499 commit 7a9c314

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

.github/workflows/test.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,32 @@ jobs:
189189
run: bundle exec rake test_cluster_scale
190190
- name: Stop containers
191191
run: docker compose -f $DOCKER_COMPOSE_FILE down
192+
benchmark:
193+
name: Benchmark
194+
timeout-minutes: 15
195+
strategy:
196+
fail-fast: false
197+
runs-on: ubuntu-latest
198+
env:
199+
REDIS_VERSION: '7.0.1'
200+
DOCKER_COMPOSE_FILE: 'docker-compose.yaml'
201+
steps:
202+
- name: Check out code
203+
uses: actions/checkout@v3
204+
- name: Set up Ruby
205+
uses: ruby/setup-ruby@v1
206+
with:
207+
ruby-version: '3.1'
208+
bundler-cache: true
209+
- name: Pull Docker images
210+
run: docker pull redis:$REDIS_VERSION
211+
- name: Run containers
212+
run: docker compose -f $DOCKER_COMPOSE_FILE up -d
213+
- name: Wait for Redis cluster to be ready
214+
run: bundle exec rake wait
215+
- name: Print containers
216+
run: docker compose -f $DOCKER_COMPOSE_FILE ps
217+
- name: Run minitest
218+
run: bundle exec rake bench
219+
- name: Stop containers
220+
run: docker compose -f $DOCKER_COMPOSE_FILE down

Rakefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ SLUGGISH_TEST_TYPES.each do |type|
2626
end
2727
end
2828

29+
Rake::TestTask.new(:bench) do |t|
30+
t.libs << :lib
31+
t.libs << :test
32+
t.options = '-v'
33+
t.warning = false
34+
t.test_files = ARGV.size > 1 ? ARGV[1..] : Dir['test/**/bench_*.rb']
35+
end
36+
2937
desc 'Wait for cluster to be ready'
3038
task :wait do
3139
$LOAD_PATH.unshift(File.expand_path('test', __dir__))

test/bench_command.rb

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# frozen_string_literal: true
2+
3+
require 'benchmark_helper'
4+
5+
class BenchCommand
6+
module Mixin
7+
def setup
8+
@client = new_test_client
9+
@client.call('FLUSHDB')
10+
wait_for_replication
11+
end
12+
13+
def teardown
14+
@client.call('FLUSHDB')
15+
wait_for_replication
16+
@client&.close
17+
end
18+
19+
def bench_ping
20+
assert_performance_linear do |n|
21+
n.times do
22+
@client.call('PING')
23+
end
24+
end
25+
end
26+
27+
def bench_set
28+
assert_performance_linear do |n|
29+
n.times do |i|
30+
@client.call('SET', "key#{i}", i)
31+
end
32+
end
33+
end
34+
35+
def bench_get
36+
assert_performance_linear do |n|
37+
n.times do |i|
38+
@client.call('GET', "key#{i}")
39+
end
40+
end
41+
end
42+
43+
def bench_pipeline_set
44+
assert_performance_linear do |n|
45+
n.times do |i|
46+
@client.pipelined do |pi|
47+
pi.call('SET', "key#{i}", i)
48+
end
49+
end
50+
end
51+
end
52+
53+
def bench_pipeline_get
54+
assert_performance_linear do |n|
55+
n.times do |i|
56+
@client.pipelined do |pi|
57+
pi.call('GET', "key#{i}")
58+
end
59+
end
60+
end
61+
end
62+
63+
private
64+
65+
def wait_for_replication
66+
client_side_timeout = TEST_TIMEOUT_SEC + 1.0
67+
server_side_timeout = (TEST_TIMEOUT_SEC * 1000).to_i
68+
@client.blocking_call(client_side_timeout, 'WAIT', TEST_REPLICA_SIZE, server_side_timeout)
69+
end
70+
end
71+
72+
class PrimaryOnly < BenchmarkWrapper
73+
include Mixin
74+
75+
private
76+
77+
def new_test_client
78+
config = ::RedisClient::ClusterConfig.new(
79+
nodes: TEST_NODE_URIS,
80+
fixed_hostname: TEST_FIXED_HOSTNAME,
81+
**TEST_GENERIC_OPTIONS
82+
)
83+
::RedisClient::Cluster.new(config)
84+
end
85+
end
86+
87+
class ScaleRead < BenchmarkWrapper
88+
include Mixin
89+
90+
private
91+
92+
def new_test_client
93+
config = ::RedisClient::ClusterConfig.new(
94+
nodes: TEST_NODE_URIS,
95+
replica: true,
96+
fixed_hostname: TEST_FIXED_HOSTNAME,
97+
**TEST_GENERIC_OPTIONS
98+
)
99+
::RedisClient::Cluster.new(config)
100+
end
101+
end
102+
103+
class Pooled < BenchmarkWrapper
104+
include Mixin
105+
106+
private
107+
108+
def new_test_client
109+
config = ::RedisClient::ClusterConfig.new(
110+
nodes: TEST_NODE_URIS,
111+
fixed_hostname: TEST_FIXED_HOSTNAME,
112+
**TEST_GENERIC_OPTIONS
113+
)
114+
::RedisClient::Cluster.new(config, pool: { timeout: TEST_TIMEOUT_SEC, size: 2 })
115+
end
116+
end
117+
end

test/benchmark_helper.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
# @see https://ruby-doc.org/stdlib-3.0.1/libdoc/minitest/rdoc/Minitest/Benchmark.html
4+
5+
require 'minitest/benchmark'
6+
require 'minitest/autorun'
7+
require 'redis_client'
8+
require 'redis_cluster_client'
9+
require 'testing_constants'
10+
11+
case ENV.fetch('REDIS_CONNECTION_DRIVER', 'ruby')
12+
when 'hiredis' then require 'hiredis-client'
13+
end
14+
15+
class BenchmarkWrapper < Minitest::Benchmark; end

0 commit comments

Comments
 (0)