Skip to content

Commit

Permalink
Add lettuce clients for benchmarking
Browse files Browse the repository at this point in the history
Signed-off-by: acarbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Oct 10, 2023
1 parent 035ef20 commit 0b4a5bf
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package javababushka.benchmarks;

import static javababushka.benchmarks.utils.Benchmarking.testClientSetGet;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javababushka.benchmarks.clients.LettuceClient;
import javababushka.benchmarks.clients.LettuceAsyncClient;
import javababushka.benchmarks.clients.LettuceClient;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand Down Expand Up @@ -43,12 +48,10 @@ public static void main(String[] args) {
System.out.println("Run JEDI pseudo-async client");
break;
case LETTUCE:
// run testClientSetGet on LETTUCE sync client
System.out.println("Run LETTUCE sync client");
testClientSetGet(LettuceClient::new, runConfiguration, false);
break;
case LETTUCE_ASYNC:
// run testClientSetGet on LETTUCE async client
System.out.println("Run LETTUCE async client");
testClientSetGet(LettuceAsyncClient::new, runConfiguration, true);
break;
case BABUSHKA:
System.out.println("Babushka not yet configured");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package javababushka.benchmarks.clients;

import java.util.concurrent.Future;

/**
* A Redis client with async capabilities
*/
public interface AsyncClient extends Client {

long DEFAULT_TIMEOUT = 1000;

Future<?> asyncSet(String key, String value);

Future<String> asyncGet(String key);

<T> T waitForResult(Future<T> future);

<T> T waitForResult(Future<T> future, long timeout);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package javababushka.benchmarks.clients;

import javababushka.benchmarks.utils.ConnectionSettings;

/**
* A Redis client interface
*/
public interface Client {
void connectToRedis();

void connectToRedis(ConnectionSettings connectionSettings);

default void closeConnection() {}

String getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package javababushka.benchmarks.clients;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javababushka.benchmarks.utils.ConnectionSettings;

/**
* A Lettuce client with async capabilities
* see: https://lettuce.io/
*/
public class LettuceAsyncClient implements AsyncClient {

RedisClient client;
RedisAsyncCommands asyncCommands;
StatefulRedisConnection<String, String> connection;

@Override
public void connectToRedis() {
connectToRedis(new ConnectionSettings("localhost", 6379, false));
}

@Override
public void connectToRedis(ConnectionSettings connectionSettings) {
client =
RedisClient.create(
String.format(
"%s://%s:%d",
connectionSettings.useSsl ? "rediss" : "redis",
connectionSettings.host,
connectionSettings.port));
connection = client.connect();
asyncCommands = connection.async();
}

@Override
public RedisFuture<?> asyncSet(String key, String value) {
return asyncCommands.set(key, value);
}

@Override
public RedisFuture<String> asyncGet(String key) {
return asyncCommands.get(key);
}

@Override
public Object waitForResult(Future future) {
return waitForResult(future, DEFAULT_TIMEOUT);
}

@Override
public Object waitForResult(Future future, long timeoutMS) {
try {
return future.get(timeoutMS, TimeUnit.MILLISECONDS);
} catch (Exception ignored) {
return null;
}
}

@Override
public void closeConnection() {
connection.close();
client.shutdown();
}

@Override
public String getName() {
return "Lettuce Async";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package javababushka.benchmarks.clients;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisStringCommands;
import javababushka.benchmarks.utils.ConnectionSettings;

/**
* A Lettuce client with sync capabilities
* see: https://lettuce.io/
*/
public class LettuceClient implements SyncClient {

RedisClient client;
RedisStringCommands syncCommands;
StatefulRedisConnection<String, String> connection;

@Override
public void connectToRedis() {
connectToRedis(new ConnectionSettings("localhost", 6379, false));
}

@Override
public void connectToRedis(ConnectionSettings connectionSettings) {
client =
RedisClient.create(
String.format(
"%s://%s:%d",
connectionSettings.useSsl ? "rediss" : "redis",
connectionSettings.host,
connectionSettings.port));
connection = client.connect();
syncCommands = connection.sync();
}

@Override
public void set(String key, String value) {
syncCommands.set(key, value);
}

@Override
public String get(String key) {
return (String) syncCommands.get(key);
}

@Override
public void closeConnection() {
connection.close();
client.shutdown();
}

@Override
public String getName() {
return "Lettuce";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package javababushka.benchmarks.clients;

/**
* A Redis client with sync capabilities
*/
public interface SyncClient extends Client {
void set(String key, String value);

String get(String key);
}
Loading

0 comments on commit 0b4a5bf

Please sign in to comment.