Created
February 21, 2018 22:11
-
-
Save gszpak/e9af789cc69718524b28d9ee9adfac16 to your computer and use it in GitHub Desktop.
Benchmarking io.lettuce.core.protocol.CommandHandler.write() method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.lettuce.core.RedisClient; | |
import io.lettuce.core.RedisURI; | |
import io.lettuce.core.api.StatefulRedisConnection; | |
import io.lettuce.core.api.async.RedisAsyncCommands; | |
import io.lettuce.core.resource.ClientResources; | |
import io.lettuce.core.resource.DefaultClientResources; | |
import io.lettuce.core.resource.EventLoopGroupProvider; | |
import io.netty.channel.EventLoopGroup; | |
import io.netty.channel.epoll.EpollEventLoopGroup; | |
import io.netty.util.concurrent.DefaultPromise; | |
import io.netty.util.concurrent.EventExecutorGroup; | |
import io.netty.util.concurrent.GlobalEventExecutor; | |
import io.netty.util.concurrent.Promise; | |
import java.time.Duration; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.CompletionStage; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.Future; | |
import java.util.concurrent.TimeUnit; | |
class SingletonEventLoopGroupProvider implements EventLoopGroupProvider { | |
static final EpollEventLoopGroup EVENT_LOOP = new EpollEventLoopGroup(1); | |
@Override | |
public <T extends EventLoopGroup> T allocate(Class<T> type) { | |
//noinspection unchecked | |
return (T) EVENT_LOOP; | |
} | |
@Override | |
public int threadPoolSize() { | |
return 1; | |
} | |
@Override | |
public io.netty.util.concurrent.Future<Boolean> release( | |
EventExecutorGroup eventLoopGroup, long quietPeriod, long timeout, TimeUnit unit) { | |
if (eventLoopGroup != EVENT_LOOP) { | |
return getCompletedPromise(false); | |
} | |
EVENT_LOOP.shutdownGracefully(quietPeriod, timeout, unit); | |
return getCompletedPromise(true); | |
} | |
@Override | |
public io.netty.util.concurrent.Future<Boolean> shutdown( | |
long quietPeriod, long timeout, TimeUnit timeUnit) { | |
EVENT_LOOP.shutdownGracefully(quietPeriod, timeout, timeUnit); | |
return getCompletedPromise(true); | |
} | |
private Promise<Boolean> getCompletedPromise(boolean success) { | |
Promise<Boolean> promise = new DefaultPromise<>(GlobalEventExecutor.INSTANCE); | |
return promise.setSuccess(success); | |
} | |
} | |
public class LettuceBenchmark { | |
private static List<CompletionStage<Map<String, String>>> sendCommands( | |
RedisAsyncCommands<String, String> commands, int numOfIterations) { | |
List<CompletionStage<Map<String, String>>> responseFutures = new ArrayList<>(); | |
for (int i = 0; i < numOfIterations; i++) { | |
responseFutures.add(commands.hgetall("key-" + i)); | |
} | |
return responseFutures; | |
} | |
public static void main(String[] args) throws ExecutionException, InterruptedException { | |
String host = args[0]; | |
int port = Integer.valueOf(args[1]); | |
int numOfIterations = Integer.valueOf(args[2]); | |
ClientResources resources = DefaultClientResources | |
.builder() | |
.eventLoopGroupProvider(new SingletonEventLoopGroupProvider()) | |
.build(); | |
RedisURI redisURI = RedisURI.Builder.redis(host, port).build(); | |
RedisClient client = RedisClient.create(resources, redisURI); | |
StatefulRedisConnection<String, String> connection = client.connect(); | |
RedisAsyncCommands<String, String> commands = connection.async(); | |
long startTime = System.nanoTime(); | |
Future<List<CompletionStage<Map<String, String>>>> future = | |
SingletonEventLoopGroupProvider.EVENT_LOOP.submit(() -> sendCommands(commands, numOfIterations)); | |
future.get(); | |
long endTime = System.nanoTime(); | |
System.out.println(String.format( | |
"Num of iterations: %d, running time: %d ms", | |
numOfIterations, | |
Duration.ofNanos(endTime - startTime).toMillis())); | |
connection.close(); | |
client.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment