From 3e6feed747a3f08c5eaa8ec93783a7c78f499796 Mon Sep 17 00:00:00 2001 From: Andrew Carbonetto Date: Fri, 24 Nov 2023 12:35:48 -0800 Subject: [PATCH] Add pool and pooled Jedis clients for comparison Signed-off-by: Andrew Carbonetto --- .../benchmarks/BenchmarkingApp.java | 14 +++- .../benchmarks/clients/jedis/JedisClient.java | 26 +++---- .../clients/jedis/JedisPoolClient.java | 68 +++++++++++++++++++ .../clients/jedis/JedisPooledClient.java | 59 ++++++++++++++++ 4 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPoolClient.java create mode 100644 java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPooledClient.java diff --git a/java/benchmarks/src/main/java/javababushka/benchmarks/BenchmarkingApp.java b/java/benchmarks/src/main/java/javababushka/benchmarks/BenchmarkingApp.java index 8b576b0b74..80b87923e3 100644 --- a/java/benchmarks/src/main/java/javababushka/benchmarks/BenchmarkingApp.java +++ b/java/benchmarks/src/main/java/javababushka/benchmarks/BenchmarkingApp.java @@ -7,6 +7,8 @@ import java.util.stream.Stream; import javababushka.benchmarks.clients.babushka.JniNettyClient; import javababushka.benchmarks.clients.jedis.JedisClient; +import javababushka.benchmarks.clients.jedis.JedisPoolClient; +import javababushka.benchmarks.clients.jedis.JedisPooledClient; import javababushka.benchmarks.clients.lettuce.LettuceAsyncClient; import javababushka.benchmarks.clients.lettuce.LettuceAsyncClusterClient; import org.apache.commons.cli.CommandLine; @@ -50,6 +52,14 @@ public static void main(String[] args) { // run testClientSetGet on JEDIS sync client testClientSetGet(JedisClient::new, runConfiguration, false); break; + case JEDIS_POOL: + // run testClientSetGet on JEDIS sync client + testClientSetGet(JedisPoolClient::new, runConfiguration, false); + break; + case JEDIS_POOLED: + // run testClientSetGet on JEDIS sync client + testClientSetGet(JedisPooledClient::new, runConfiguration, false); + break; case LETTUCE: if (runConfiguration.clusterModeEnabled) { testClientSetGet(LettuceAsyncClusterClient::new, runConfiguration, true); @@ -194,7 +204,9 @@ private static int[] parseIntListOption(String line) throws ParseException { } public enum ClientName { - JEDIS("Jedis"), + JEDIS("Jedis Single Resource Client"), + JEDIS_POOL("Jedis Single Pool Client"), + JEDIS_POOLED("Jedis Pooled Client"), LETTUCE("Lettuce"), BABUSHKA("Babushka"), ALL("All"), diff --git a/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisClient.java b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisClient.java index 0b0268721d..961b55ef69 100644 --- a/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisClient.java +++ b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisClient.java @@ -8,8 +8,7 @@ /** A Jedis client with sync capabilities. See: https://github.com/redis/jedis */ public class JedisClient implements SyncClient { - // protected Jedis jedisResource; - protected JedisPool pool; + protected Jedis jedisResource; // protected JedisPooled pooledConnection; @Override @@ -31,38 +30,29 @@ public void connectToRedis() { public void connectToRedis(ConnectionSettings connectionSettings) { assert connectionSettings.clusterMode == false : "JedisClient does not support clusterMode: use JedisClusterClient instead"; - pool = + JedisPool pool = new JedisPool(connectionSettings.host, connectionSettings.port, connectionSettings.useSsl); // check if the pool is properly connected - try (Jedis jedis = pool.getResource()) { - assert jedis.isConnected() : "failed to connect to jedis"; - } + jedisResource = pool.getResource(); + assert jedisResource.isConnected() : "failed to connect to jedis"; } public String info() { - try (Jedis jedis = pool.getResource()) { - return jedis.info(); - } + return jedisResource.info(); } public String info(String section) { - try (Jedis jedis = pool.getResource()) { - return jedis.info(section); - } + return jedisResource.info(section); } @Override public void set(String key, String value) { - try (Jedis jedis = pool.getResource()) { - jedis.set(key, value); - } + jedisResource.set(key, value); } @Override public String get(String key) { - try (Jedis jedis = pool.getResource()) { - return jedis.get(key); - } + return jedisResource.get(key); } } diff --git a/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPoolClient.java b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPoolClient.java new file mode 100644 index 0000000000..85149383e9 --- /dev/null +++ b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPoolClient.java @@ -0,0 +1,68 @@ +package javababushka.benchmarks.clients.jedis; + +import javababushka.benchmarks.clients.SyncClient; +import javababushka.benchmarks.utils.ConnectionSettings; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +/** A Jedis client with sync capabilities. See: https://github.com/redis/jedis */ +public class JedisPoolClient implements SyncClient { + + // protected Jedis jedisResource; + protected JedisPool pool; + + // protected JedisPooled pooledConnection; + @Override + public void closeConnection() { + // nothing to do + } + + @Override + public String getName() { + return "Jedis"; + } + + @Override + public void connectToRedis() { + connectToRedis(DEFAULT_CONNECTION_STRING); + } + + @Override + public void connectToRedis(ConnectionSettings connectionSettings) { + assert connectionSettings.clusterMode == false + : "JedisClient does not support clusterMode: use JedisClusterClient instead"; + pool = + new JedisPool(connectionSettings.host, connectionSettings.port, connectionSettings.useSsl); + + // check if the pool is properly connected + try (Jedis jedis = pool.getResource()) { + assert jedis.isConnected() : "failed to connect to jedis"; + } + } + + public String info() { + try (Jedis jedis = pool.getResource()) { + return jedis.info(); + } + } + + public String info(String section) { + try (Jedis jedis = pool.getResource()) { + return jedis.info(section); + } + } + + @Override + public void set(String key, String value) { + try (Jedis jedis = pool.getResource()) { + jedis.set(key, value); + } + } + + @Override + public String get(String key) { + try (Jedis jedis = pool.getResource()) { + return jedis.get(key); + } + } +} diff --git a/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPooledClient.java b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPooledClient.java new file mode 100644 index 0000000000..3f62c34f4d --- /dev/null +++ b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/jedis/JedisPooledClient.java @@ -0,0 +1,59 @@ +package javababushka.benchmarks.clients.jedis; + +import javababushka.benchmarks.clients.SyncClient; +import javababushka.benchmarks.utils.ConnectionSettings; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPooled; + +/** A Jedis client with sync capabilities. See: https://github.com/redis/jedis */ +public class JedisPooledClient implements SyncClient { + + // protected Jedis jedisResource; + protected JedisPooled pool; + + // protected JedisPooled pooledConnection; + @Override + public void closeConnection() { + // nothing to do + } + + @Override + public String getName() { + return "Jedis"; + } + + @Override + public void connectToRedis() { + connectToRedis(DEFAULT_CONNECTION_STRING); + } + + @Override + public void connectToRedis(ConnectionSettings connectionSettings) { + assert connectionSettings.clusterMode == false + : "JedisClient does not support clusterMode: use JedisClusterClient instead"; + pool = + new JedisPooled(connectionSettings.host, connectionSettings.port, connectionSettings.useSsl); + + // check if the pool is properly connected + assert pool.getPool().getResource().isConnected() : "failed to connect to jedis"; + } + + public String info() { + return "N/A"; + } + + public String info(String section) { + return "N/A"; + } + + @Override + public void set(String key, String value) { + pool.set(key, value); + } + + @Override + public String get(String key) { + return pool.get(key); + } +}