Skip to content

Commit

Permalink
Make common pool of iterations.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Sep 28, 2023
1 parent 577d7a1 commit 6982e36
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package javababushka.benchmarks;

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

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javababushka.benchmarks.jedis.JedisClient;
import javababushka.benchmarks.jedis.JedisPseudoAsyncClient;
import javababushka.benchmarks.lettuce.LettuceAsyncClient;
import javababushka.benchmarks.lettuce.LettuceClient;
import javababushka.benchmarks.utils.Benchmarking;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand Down Expand Up @@ -77,7 +75,8 @@ private static Options getOptions() {
options.addOption("f", "resultsFile", true, "Result filepath []");
options.addOption("d", "dataSize", true, "Data block size [20]");
options.addOption("C", "concurrentTasks", true, "Number of concurrent tasks [1 10 100]");
options.addOption("l", "clients", true, "one of: all|jedis|jedis_async|lettuce|lettuce_async|babushka [all]");
options.addOption(
"l", "clients", true, "one of: all|jedis|jedis_async|lettuce|lettuce_async|babushka [all]");
options.addOption("h", "host", true, "host url [localhost]");
options.addOption("p", "port", true, "port number [6379]");
options.addOption("n", "clientCount", true, "Client count [1]");
Expand Down Expand Up @@ -183,49 +182,6 @@ private static RunConfiguration verifyOptions(CommandLine line) throws ParseExce
return runConfiguration;
}

private static void testClientSetGet(
Supplier<Client> clientCreator, RunConfiguration runConfiguration, boolean async) {
System.out.printf("%n =====> %s <===== %n%n", clientCreator.get().getName());
for (int concurrentNum : runConfiguration.concurrentTasks) {
for (int clientNum : runConfiguration.clientCount) {

List<Runnable> tasks = new ArrayList<>();

for (int i = 0; i < concurrentNum; ) {
for (int j = 0; j < clientNum; j++) {
i++;
int finalI = i;
int finalJ = j;
tasks.add(
() -> {
System.out.printf(
"%n concurrent = %d/%d, client# = %d/%d%n",
finalI, concurrentNum, finalJ + 1, clientNum);
Benchmarking.printResults(
Benchmarking.measurePerformance(
clientCreator.get(), runConfiguration, async));
});
}
}
System.out.printf(
"===> concurrentNum = %d, clientNum = %d, tasks = %d%n",
concurrentNum, clientNum, tasks.size());
tasks.stream()
.map(CompletableFuture::runAsync)
.forEach(
f -> {
try {
f.get();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}

System.out.println();
}

public enum ClientName {
JEDIS("Jedis"),
JEDIS_ASYNC("Jedis async"),
Expand Down Expand Up @@ -262,22 +218,21 @@ public static class RunConfiguration {
public int port;
public int[] clientCount;
public boolean tls;
public boolean debugLogging = false;

public RunConfiguration() {
configuration = "Release";
resultsFile = Optional.empty();
dataSize = 20;
concurrentTasks = List.of(1, 10, 100);
clients = new ClientName[] {
// ClientName.BABUSHKA,
ClientName.JEDIS,
ClientName.JEDIS_ASYNC,
ClientName.LETTUCE,
ClientName.LETTUCE_ASYNC
};
concurrentTasks = List.of(10, 100);
clients =
new ClientName[] {
// ClientName.BABUSHKA,
ClientName.JEDIS, ClientName.JEDIS_ASYNC, ClientName.LETTUCE, ClientName.LETTUCE_ASYNC
};
host = "localhost";
port = 6379;
clientCount = new int[] {1};
clientCount = new int[] {1, 2};
tls = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package javababushka.benchmarks.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javababushka.benchmarks.AsyncClient;
import javababushka.benchmarks.BenchmarkingApp;
import javababushka.benchmarks.Client;
import javababushka.benchmarks.SyncClient;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.tuple.Pair;

public class Benchmarking {
static final double PROB_GET = 0.8;
Expand Down Expand Up @@ -43,28 +47,12 @@ public interface Operation {
void go();
}

public static Map<ChosenAction, ArrayList<Long>> getLatencies(
int iterations, Map<ChosenAction, Operation> actions) {
Map<ChosenAction, ArrayList<Long>> latencies = new HashMap<ChosenAction, ArrayList<Long>>();
for (ChosenAction action : actions.keySet()) {
latencies.put(action, new ArrayList<Long>());
}

for (int i = 0; i < iterations; i++) {
ChosenAction action = randomAction();
Operation op = actions.get(action);
ArrayList<Long> actionLatencies = latencies.get(action);
addLatency(op, actionLatencies);
}

return latencies;
}

private static void addLatency(Operation op, ArrayList<Long> latencies) {
private static Pair<ChosenAction, Long> getLatency(Map<ChosenAction, Operation> actions) {
var action = randomAction();
long before = System.nanoTime();
op.go();
actions.get(action).go();
long after = System.nanoTime();
latencies.add(after - before);
return Pair.of(action, after - before);
}

// Assumption: latencies is sorted in ascending order
Expand Down Expand Up @@ -114,8 +102,7 @@ public static Map<ChosenAction, LatencyResults> calculateResults(
}

public static void printResults(
Map<ChosenAction, LatencyResults> calculatedResults, Optional<FileWriter> resultsFile)
throws IOException {
Map<ChosenAction, LatencyResults> calculatedResults, Optional<FileWriter> resultsFile) {
if (resultsFile.isPresent()) {
printResults(calculatedResults, resultsFile.get());
} else {
Expand All @@ -124,16 +111,19 @@ public static void printResults(
}

public static void printResults(
Map<ChosenAction, LatencyResults> resultsMap, FileWriter resultsFile) throws IOException {
Map<ChosenAction, LatencyResults> resultsMap, FileWriter resultsFile) {
for (Map.Entry<ChosenAction, LatencyResults> entry : resultsMap.entrySet()) {
ChosenAction action = entry.getKey();
LatencyResults results = entry.getValue();

resultsFile.write("Avg. time in ms per " + action + ": " + results.avgLatency / 1000000.0);
resultsFile.write(action + " p50 latency in ms: " + results.p50Latency / 1000000.0);
resultsFile.write(action + " p90 latency in ms: " + results.p90Latency / 1000000.0);
resultsFile.write(action + " p99 latency in ms: " + results.p99Latency / 1000000.0);
resultsFile.write(action + " std dev in ms: " + results.stdDeviation / 1000000.0);
try {
resultsFile.write("Avg. time in ms per " + action + ": " + results.avgLatency / 1000000.0);
resultsFile.write(action + " p50 latency in ms: " + results.p50Latency / 1000000.0);
resultsFile.write(action + " p90 latency in ms: " + results.p90Latency / 1000000.0);
resultsFile.write(action + " p99 latency in ms: " + results.p99Latency / 1000000.0);
resultsFile.write(action + " std dev in ms: " + results.stdDeviation / 1000000.0);
} catch (Exception ignored) {
}
}
}

Expand All @@ -150,22 +140,82 @@ public static void printResults(Map<ChosenAction, LatencyResults> resultsMap) {
}
}

public static Map<ChosenAction, LatencyResults> measurePerformance(
Client client, BenchmarkingApp.RunConfiguration config, boolean async) {
client.connectToRedis(new ConnectionSettings(config.host, config.port, config.tls));

int iterations = 10000;
String value = RandomStringUtils.randomAlphanumeric(config.dataSize);

if (config.resultsFile.isPresent()) {
try {
config.resultsFile.get().write(client.getName() + " client Benchmarking: ");
} catch (Exception ignored) {
public static void testClientSetGet(
Supplier<Client> clientCreator, BenchmarkingApp.RunConfiguration config, boolean async) {
for (int concurrentNum : config.concurrentTasks) {
int iterations = Math.min(Math.max(100000, concurrentNum * 10000), 10000000);
for (int clientNum : config.clientCount) {
System.out.printf(
"%n =====> %s <===== %d clients %d concurrent %n%n",
clientCreator.get().getName(), clientNum, concurrentNum);
AtomicInteger iterationCounter = new AtomicInteger(0);
Map<ChosenAction, ArrayList<Long>> actionResults =
Map.of(
ChosenAction.GET_EXISTING, new ArrayList<>(),
ChosenAction.GET_NON_EXISTING, new ArrayList<>(),
ChosenAction.SET, new ArrayList<>());
List<Runnable> tasks = new ArrayList<>();

for (int i = 0; i < concurrentNum; ) {
for (int j = 0; j < clientNum; j++) {
i++;
int finalI = i;
int finalJ = j;
var client = clientCreator.get();
client.connectToRedis(new ConnectionSettings(config.host, config.port, config.tls));
tasks.add(
() -> {
if (config.debugLogging) {
System.out.printf(
"%n concurrent = %d/%d, client# = %d/%d%n",
finalI, concurrentNum, finalJ + 1, clientNum);
}
while (true) {
int iterationIncrement = iterationCounter.incrementAndGet();
if (iterationIncrement >= iterations) {
break;
}
if (config.debugLogging) {
System.out.printf(
"> iteration = %d/%d, client# = %d/%d%n",
iterationIncrement + 1, iterations, finalJ + 1, clientNum);
}
// operate and calculate tik-tok
Pair<ChosenAction, Long> result =
Benchmarking.measurePerformance(client, config.dataSize, async);
actionResults.get(result.getLeft()).add(result.getRight());
}
});
}
}
if (config.debugLogging) {
System.out.printf("%s client Benchmarking: %n", clientCreator.get().getName());
System.out.printf(
"===> concurrentNum = %d, clientNum = %d, tasks = %d%n",
concurrentNum, clientNum, tasks.size());
}
tasks.stream()
.map(CompletableFuture::runAsync)
.forEach(
f -> {
try {
f.get();
} catch (Exception e) {
e.printStackTrace();
}
});

Benchmarking.printResults(Benchmarking.calculateResults(actionResults), config.resultsFile);
}
} else {
System.out.printf("%s client Benchmarking: %n", client.getName());
}

System.out.println();
}

public static Pair<ChosenAction, Long> measurePerformance(
Client client, int dataSize, boolean async) {

String value = RandomStringUtils.randomAlphanumeric(dataSize);
Map<ChosenAction, Benchmarking.Operation> actions = new HashMap<>();
actions.put(
ChosenAction.GET_EXISTING,
Expand All @@ -183,8 +233,7 @@ public static Map<ChosenAction, LatencyResults> measurePerformance(
? () -> ((AsyncClient) client).asyncSet(Benchmarking.generateKeySet(), value)
: () -> ((SyncClient) client).set(Benchmarking.generateKeySet(), value));

var results = Benchmarking.calculateResults(Benchmarking.getLatencies(iterations, actions));
client.closeConnection();
return results;
// var results = Benchmarking.calculateResults(Benchmarking.getLatencies(iterations, actions));
return getLatency(actions);
}
}

0 comments on commit 6982e36

Please sign in to comment.