Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarking application. No test clients yet. #629

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
import javababushka.benchmarks.BenchmarkingApp;
import javababushka.benchmarks.clients.AsyncClient;
Expand Down Expand Up @@ -46,11 +47,12 @@ public interface Operation {
void go() throws InterruptedException, ExecutionException;
}

public static Pair<ChosenAction, Long> measurePerformance(Map<ChosenAction, Operation> actions) {
public static Pair<ChosenAction, Long> measurePerformance(
Client client, Map<ChosenAction, Function<Client, Operation>> actions) {
var action = randomAction();
shachlanAmazon marked this conversation as resolved.
Show resolved Hide resolved
long before = System.nanoTime();
try {
actions.get(action).go();
actions.get(action).apply(client).go();
} catch (ExecutionException e) {
throw new RuntimeException("Client error", e);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -215,11 +217,12 @@ private static CompletableFuture<Map<ChosenAction, ArrayList<Long>>> createTask(
boolean debugLogging) {
return CompletableFuture.supplyAsync(
() -> {
Map<ChosenAction, ArrayList<Long>> taskActionResults =
var taskActionResults =
Map.of(
ChosenAction.GET_EXISTING, new ArrayList<>(),
ChosenAction.GET_NON_EXISTING, new ArrayList<>(),
ChosenAction.SET, new ArrayList<>());
ChosenAction.GET_EXISTING, new ArrayList<Long>(),
ChosenAction.GET_NON_EXISTING, new ArrayList<Long>(),
ChosenAction.SET, new ArrayList<Long>());
var actions = getActionMap(dataSize, async);
int iterationIncrement = iterationCounter.getAndIncrement();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you increment this outside of the loop? wouldn't it make more sense to just increment it at the beginning of the while loop instead of before the loop AND inside the loop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d3f707f.

int clientIndex = iterationIncrement % clients.size();

Expand All @@ -235,9 +238,8 @@ private static CompletableFuture<Map<ChosenAction, ArrayList<Long>>> createTask(
iterationIncrement + 1, iterations, clientIndex + 1, clientCount);
}

var actions = getActionMap(clients.get(clientIndex), dataSize, async);
// operate and calculate tik-tok
Pair<ChosenAction, Long> result = measurePerformance(actions);
Pair<ChosenAction, Long> result = measurePerformance(clients.get(clientIndex), actions);
if (result != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would this be null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! It is never happen now.
Fixed in d3f707f.

taskActionResults.get(result.getLeft()).add(result.getRight());
}
Expand All @@ -249,26 +251,37 @@ private static CompletableFuture<Map<ChosenAction, ArrayList<Long>>> createTask(
});
}

public static Map<ChosenAction, Operation> getActionMap(
Client client, int dataSize, boolean async) {
public static Map<ChosenAction, Function<Client, Operation>> getActionMap(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: instead of a function returning a function, Operation can just take a Client as an argument, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh!
Fixed in d3f707f.

int dataSize, boolean async) {

String value = "0".repeat(dataSize);
Map<ChosenAction, Operation> actions = new HashMap<>();
actions.put(
return Map.of(
ChosenAction.GET_EXISTING,
async
? () -> ((AsyncClient) client).asyncGet(generateKeySet()).get()
: () -> ((SyncClient) client).get(generateKeySet()));
actions.put(
client ->
() -> {
if (async) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: why drop the ternary operator syntax?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java does not accept this in void expressions. I was also disappointed.
We ignore results of get/set, Operation interface defines a void function.

((AsyncClient) client).asyncGet(generateKeySet()).get();
} else {
((SyncClient) client).get(generateKeySet());
}
},
ChosenAction.GET_NON_EXISTING,
async
? () -> ((AsyncClient) client).asyncGet(generateKeyGet()).get()
: () -> ((SyncClient) client).get(generateKeyGet()));
actions.put(
client ->
() -> {
if (async) {
((AsyncClient) client).asyncGet(generateKeyGet()).get();
} else {
((SyncClient) client).get(generateKeyGet());
}
},
ChosenAction.SET,
async
? () -> ((AsyncClient) client).asyncSet(generateKeySet(), value).get()
: () -> ((SyncClient) client).set(generateKeySet(), value));
return actions;
client ->
() -> {
if (async) {
((AsyncClient) client).asyncSet(generateKeySet(), value).get();
} else {
((SyncClient) client).set(generateKeySet(), value);
}
});
}
}