diff --git a/java/benchmarks/src/main/java/javababushka/benchmarks/utils/Benchmarking.java b/java/benchmarks/src/main/java/javababushka/benchmarks/utils/Benchmarking.java index afa9cf9feb..48d16f998c 100644 --- a/java/benchmarks/src/main/java/javababushka/benchmarks/utils/Benchmarking.java +++ b/java/benchmarks/src/main/java/javababushka/benchmarks/utils/Benchmarking.java @@ -8,6 +8,7 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -23,6 +24,7 @@ public class Benchmarking { static final double PROB_GET_EXISTING_KEY = 0.8; static final int SIZE_GET_KEYSPACE = 3750000; static final int SIZE_SET_KEYSPACE = 3000000; + static final int ASYNC_OPERATION_TIMEOUT_SEC = 1; private static ChosenAction randomAction() { if (Math.random() > PROB_GET) { @@ -44,13 +46,17 @@ public static String generateKeySet() { } public interface Operation { - void go(); + void go() throws Exception; } private static Pair getLatency(Map actions) { var action = randomAction(); long before = System.nanoTime(); - actions.get(action).go(); + try { + actions.get(action).go(); + } catch (Exception e) { + // timed out - exception from Future::get + } long after = System.nanoTime(); return Pair.of(action, after - before); } @@ -182,7 +188,7 @@ public static void testClientSetGet( } // operate and calculate tik-tok Pair result = - Benchmarking.measurePerformance(client, config.dataSize, async); + measurePerformance(client, config.dataSize, async); actionResults.get(result.getLeft()).add(result.getRight()); } }); @@ -205,7 +211,7 @@ public static void testClientSetGet( } }); - Benchmarking.printResults(Benchmarking.calculateResults(actionResults), config.resultsFile); + printResults(calculateResults(actionResults), config.resultsFile); } } @@ -216,24 +222,23 @@ public static Pair measurePerformance( Client client, int dataSize, boolean async) { String value = RandomStringUtils.randomAlphanumeric(dataSize); - Map actions = new HashMap<>(); + Map actions = new HashMap<>(); actions.put( ChosenAction.GET_EXISTING, async - ? () -> ((AsyncClient) client).asyncGet(Benchmarking.generateKeySet()) - : () -> ((SyncClient) client).get(Benchmarking.generateKeySet())); + ? () -> ((AsyncClient) client).asyncGet(generateKeySet()).get(ASYNC_OPERATION_TIMEOUT_SEC, TimeUnit.SECONDS) + : () -> ((SyncClient) client).get(generateKeySet())); actions.put( ChosenAction.GET_NON_EXISTING, async - ? () -> ((AsyncClient) client).asyncGet(Benchmarking.generateKeyGet()) - : () -> ((SyncClient) client).get(Benchmarking.generateKeyGet())); + ? () -> ((AsyncClient) client).asyncGet(generateKeyGet()).get(ASYNC_OPERATION_TIMEOUT_SEC, TimeUnit.SECONDS) + : () -> ((SyncClient) client).get(generateKeyGet())); actions.put( ChosenAction.SET, async - ? () -> ((AsyncClient) client).asyncSet(Benchmarking.generateKeySet(), value) - : () -> ((SyncClient) client).set(Benchmarking.generateKeySet(), value)); + ? () -> ((AsyncClient) client).asyncSet(generateKeySet(), value).get(ASYNC_OPERATION_TIMEOUT_SEC, TimeUnit.SECONDS) + : () -> ((SyncClient) client).set(generateKeySet(), value)); - // var results = Benchmarking.calculateResults(Benchmarking.getLatencies(iterations, actions)); return getLatency(actions); } }