Skip to content

Commit

Permalink
Add ExamplesApp to test get/set/info/ping
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jan 25, 2024
1 parent 926d04e commit a7bc168
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 15 deletions.
27 changes: 13 additions & 14 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ to develop this Java wrapper.

The Java client contains the following parts:

1. A Java client (lib folder): wrapper to rust client.
2. A benchmark app: A dedicated benchmarking tool designed to evaluate and compare the performance of GLIDE for Redis and other Java clients.
1. client: A Java-wrapper around the rust-core client.
2. examples: An examples app to test the client against a Redis localhost
3. benchmark: A dedicated benchmarking tool designed to evaluate and compare the performance of GLIDE for Redis and other Java clients.
4. IntegTest: An integration test sub-project for API and E2E testing

## Installation and Setup

Expand Down Expand Up @@ -84,26 +86,23 @@ Other useful gradle developer commands:
* `./gradlew :client:test` to run client unit tests
* `./gradlew spotlessCheck` to check for codestyle issues
* `./gradlew spotlessApply` to apply codestyle recommendations
* `./gradlew :examples:run` to run client examples
* `./gradlew :benchmarks:run` to run performance benchmarks

## Basic Examples

### Standalone Redis:

```java
import glide.Client;
import glide.Client.SingleResponse;
import glide.api.RedisClient;

Client client = new Client();
RedisClient client = RedisClient.CreateClient();

SingleResponse connect = client.asyncConnectToRedis("localhost", 6379);
connect.await().isSuccess();
CompletableFuture<Void> setResponse = client.set("key", "foobar");
setResponse.get();

SingleResponse set = client.asyncSet("key", "foobar");
set.await().isSuccess();

SingleResponse get = client.asyncGet("key");
get.await().getValue() == "foobar";
CompletableFuture<String> getResponse = client.get("key");
getResponse.get() == "foobar";
```

### Benchmarks
Expand All @@ -115,11 +114,11 @@ You can run benchmarks using `./gradlew run`. You can set arguments using the ar
./gradlew run --args="-resultsFile=output -dataSize \"100 1000\" -concurrentTasks \"10 100\" -clients all -host localhost -port 6279 -clientCount \"1 5\" -tls"
```

The following arguments are accepted:
The following arguments are accepted:
* `resultsFile`: the results output file
* `concurrentTasks`: Number of concurrent tasks
* `clients`: one of: all|jedis|lettuce|glide
* `clientCount`: Client count
* `host`: redis server host url
* `port`: redis server port number
* `tls`: redis TLS configured
* `tls`: redis TLS configured
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void connectToRedis(ConnectionSettings connectionSettings) {
.withPort(connectionSettings.port)
.withSsl(connectionSettings.useSsl)
.build();
if (connectionSettings.clusterMode) {
if (!connectionSettings.clusterMode) {
client = RedisClient.create(uri);
connection = ((RedisClient) client).connect();
asyncCommands = ((StatefulRedisConnection<String, String>) connection).async();
Expand Down
23 changes: 23 additions & 0 deletions java/examples/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
implementation project(':client')

implementation 'redis.clients:jedis:4.4.3'
implementation 'io.lettuce:lettuce-core:6.2.6.RELEASE'
implementation 'commons-cli:commons-cli:1.5.0'
}

application {
// Define the main class for the application.
mainClass = 'glide.examples.ExamplesApp'
applicationDefaultJvmArgs = ['-Djava.library.path=../target/release']
}
96 changes: 96 additions & 0 deletions java/examples/src/main/java/glide/examples/ExamplesApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package glide.examples;

import glide.api.RedisClient;
import glide.examples.clients.GlideClient;
import glide.examples.clients.JedisClient;
import glide.examples.clients.LettuceAsyncClient;
import java.util.concurrent.ExecutionException;
import redis.clients.jedis.commands.JedisCommands;

public class ExamplesApp {

// main application entrypoint
public static void main(String[] args) {

// runJedisExamples();
// runLettuceExamples();
runGlideExamples();
}

private static void runJedisExamples() {
ConnectionSettings settings = new ConnectionSettings("localhost", 6379, false, false);

System.out.println("Connecting to Redis using Jedis-client");
JedisCommands jedisCommands = JedisClient.connectToRedis(settings);
System.out.println("Jedis SET(myKey, myValue): " + jedisCommands.set("myKey", "myValue"));
System.out.println("Jedis GET(myKey): " + jedisCommands.get("myKey"));
System.out.println("Jedis GET(invalid): " + jedisCommands.get("invalid"));
}

private static void runLettuceExamples() {

ConnectionSettings settings = new ConnectionSettings("localhost", 6379, false, false);

System.out.println("Connecting to Redis using Lettuce-client");
LettuceAsyncClient lettuceAsyncClient = new LettuceAsyncClient(settings);

try {
System.out.println(
"Lettuce SET(myKey, myValue): "
+ lettuceAsyncClient.asyncCommands.set("myKey", "myValue").get());
System.out.println(
"Lettuce GET(myKey): " + lettuceAsyncClient.asyncCommands.get("myKey").get());
System.out.println(
"Lettuce GET(invalid): " + lettuceAsyncClient.asyncCommands.get("invalid").get());

} catch (InterruptedException | ExecutionException e) {
System.out.println("Lettuce example failed with an exception: ");
e.printStackTrace();
}
}

private static void runGlideExamples() {
ConnectionSettings settings = new ConnectionSettings("localhost", 6379, false, false);

try {
RedisClient client = GlideClient.connectToGlide(settings);

System.out.println("Glide PING: " + client.ping().get());
System.out.println("Glide PING(custom): " + client.ping("found you!").get());

// panic
// System.out.println("Glide INFO(): " + client.info().get());

System.out.println("Glide SET(myKey, myValue): " + client.set("myKey", "myValue").get());
System.out.println("Glide GET(myKey): " + client.get("myKey").get());
System.out.println("Glide SET(myKey, yourValue): " + client.set("myKey", "yourValue").get());
System.out.println("Glide GET(myKey): " + client.get("myKey").get());
System.out.println("Glide GET(invalid): " + client.get("invalid").get());

Object customGetMyKey = client.customCommand(new String[] {"get", "myKey"}).get();
System.out.println("Glide CUSTOM_COMMAND(get, myKey): " + customGetMyKey);

Object customGetInvalid = client.customCommand(new String[] {"get", "invalid"}).get();
System.out.println("Glide CUSTOM_COMMAND(get, invalid): " + customGetInvalid);

} catch (ExecutionException | InterruptedException e) {
System.out.println("Glide example failed with an exception: ");
e.printStackTrace();
}
}

/** Redis-client settings */
public static class ConnectionSettings {
public final String host;
public final int port;
public final boolean useSsl;
public final boolean clusterMode;

public ConnectionSettings(String host, int port, boolean useSsl, boolean clusterMode) {
this.host = host;
this.port = port;
this.useSsl = useSsl;
this.clusterMode = clusterMode;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package glide.examples.clients;

import glide.api.RedisClient;
import glide.api.models.configuration.NodeAddress;
import glide.api.models.configuration.RedisClientConfiguration;
import glide.examples.ExamplesApp;
import java.util.concurrent.ExecutionException;

/** Connect to Jedis client. See: https://github.com/redis/jedis */
public class GlideClient {
public static RedisClient connectToGlide(ExamplesApp.ConnectionSettings connectionSettings)
throws ExecutionException, InterruptedException {
if (connectionSettings.clusterMode) {
throw new RuntimeException("Not implemented");
}
RedisClientConfiguration config =
RedisClientConfiguration.builder()
.address(
NodeAddress.builder()
.host(connectionSettings.host)
.port(connectionSettings.port)
.build())
.useTLS(connectionSettings.useSsl)
.build();
return RedisClient.CreateClient(config).get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package glide.examples.clients;

import glide.examples.ExamplesApp;
import java.util.Set;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.commands.JedisCommands;

/** Connect to Jedis client. See: https://github.com/redis/jedis */
public class JedisClient {

public static JedisCommands connectToRedis(ExamplesApp.ConnectionSettings connectionSettings) {
JedisCommands jedis;
if (connectionSettings.clusterMode) {
jedis =
new JedisCluster(
Set.of(new HostAndPort(connectionSettings.host, connectionSettings.port)),
DefaultJedisClientConfig.builder().ssl(connectionSettings.useSsl).build());
} else {
try (JedisPool pool =
new JedisPool(
connectionSettings.host, connectionSettings.port, connectionSettings.useSsl)) {
jedis = pool.getResource();
}
}
return jedis;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package glide.examples.clients;

import glide.examples.ExamplesApp;
import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulConnection;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisStringAsyncCommands;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import java.time.Duration;

/** Connect to lettuce client - see: https://lettuce.io/ */
public class LettuceAsyncClient {
static final int ASYNC_OPERATION_TIMEOUT_SEC = 1;

private AbstractRedisClient client;
public RedisStringAsyncCommands<String, String> asyncCommands;
private StatefulConnection<String, String> connection;

public LettuceAsyncClient(ExamplesApp.ConnectionSettings connectionSettings) {
RedisURI uri =
RedisURI.builder()
.withHost(connectionSettings.host)
.withPort(connectionSettings.port)
.withSsl(connectionSettings.useSsl)
.build();
if (!connectionSettings.clusterMode) {
client = RedisClient.create(uri);
connection = ((RedisClient) client).connect();
asyncCommands = ((StatefulRedisConnection<String, String>) connection).async();
} else {
client = RedisClusterClient.create(uri);
connection = ((RedisClusterClient) client).connect();
asyncCommands = ((StatefulRedisClusterConnection<String, String>) connection).async();
}
connection.setTimeout(Duration.ofSeconds(ASYNC_OPERATION_TIMEOUT_SEC));
}

public void closeConnection() {
connection.close();
client.shutdown();
}
}
1 change: 1 addition & 0 deletions java/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ rootProject.name = 'glide'

include 'client'
include 'integTest'
include 'examples'
include 'benchmarks'

0 comments on commit a7bc168

Please sign in to comment.