Skip to content

Commit

Permalink
Add getting result from async methods.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Oct 4, 2023
1 parent 6982e36 commit c6a1b9a
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -44,13 +46,17 @@ public static String generateKeySet() {
}

public interface Operation {
void go();
void go() throws Exception;
}

private static Pair<ChosenAction, Long> getLatency(Map<ChosenAction, Operation> 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);
}
Expand Down Expand Up @@ -182,7 +188,7 @@ public static void testClientSetGet(
}
// operate and calculate tik-tok
Pair<ChosenAction, Long> result =
Benchmarking.measurePerformance(client, config.dataSize, async);
measurePerformance(client, config.dataSize, async);
actionResults.get(result.getLeft()).add(result.getRight());
}
});
Expand All @@ -205,7 +211,7 @@ public static void testClientSetGet(
}
});

Benchmarking.printResults(Benchmarking.calculateResults(actionResults), config.resultsFile);
printResults(calculateResults(actionResults), config.resultsFile);
}
}

Expand All @@ -216,24 +222,23 @@ public static Pair<ChosenAction, Long> measurePerformance(
Client client, int dataSize, boolean async) {

String value = RandomStringUtils.randomAlphanumeric(dataSize);
Map<ChosenAction, Benchmarking.Operation> actions = new HashMap<>();
Map<ChosenAction, Operation> 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);
}
}

0 comments on commit c6a1b9a

Please sign in to comment.