Skip to content

Commit

Permalink
Client API
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikebbers committed Sep 11, 2023
1 parent c346443 commit a271f10
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 259 deletions.
73 changes: 37 additions & 36 deletions jmh-client/src/main/java/com/openelements/jmh/client/JmhRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.openelements.jmh.common.BenchmarkExecution;
import com.openelements.jmh.client.factory.BenchmarkFactory;
import com.openelements.jmh.client.jmh.BenchmarkFactory;
import com.openelements.jmh.client.json.BenchmarkJsonFactory;
import com.openelements.jmh.common.BenchmarkExecution;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
Expand All @@ -18,45 +18,46 @@
import org.openjdk.jmh.runner.options.OptionsBuilder;


@Deprecated
public class JmhRunner {

private final Set<Class> classes;
private final Set<Class> classes;

public JmhRunner() {
this.classes = new HashSet<>();
}
public JmhRunner() {
this.classes = new HashSet<>();
}

public void addBenchmarkClass(final Class cls) {
classes.add(cls);
}
public void addBenchmarkClass(final Class cls) {
classes.add(cls);
}

public void run() throws RunnerException {
final OptionsBuilder optionsBuilder = new OptionsBuilder();
classes.forEach(cls -> optionsBuilder.getIncludes().add(cls.getName()));
final Runner runner = new Runner(optionsBuilder.build());
Collection<RunResult> run = runner.run();
public void run() throws RunnerException {
final OptionsBuilder optionsBuilder = new OptionsBuilder();
classes.forEach(cls -> optionsBuilder.getIncludes().add(cls.getName()));
final Runner runner = new Runner(optionsBuilder.build());
Collection<RunResult> run = runner.run();

final Gson gson = new GsonBuilder().create();
String jsonString = gson.toJson(run);
try (final FileWriter fileWriter = new FileWriter("raw-jmh-data.json")) {
fileWriter.write(jsonString);
} catch (final IOException e) {
e.printStackTrace();
}

Set<BenchmarkExecution> results = run.stream()
.map(BenchmarkFactory::convert)
.collect(Collectors.toSet());

results.stream().forEach(benchmark -> {
final String json = BenchmarkJsonFactory.toJson(benchmark);
final String filename = UUID.randomUUID().toString() + ".json";
try (final FileWriter fileWriter = new FileWriter(filename)) {
fileWriter.write(json);
} catch (final IOException e) {
e.printStackTrace();
}
});

final Gson gson = new GsonBuilder().create();
String jsonString = gson.toJson(run);
try (final FileWriter fileWriter = new FileWriter("raw-jmh-data.json")) {
fileWriter.write(jsonString);
} catch (final IOException e) {
e.printStackTrace();
}

Set<BenchmarkExecution> results = run.stream()
.map(BenchmarkFactory::convert)
.collect(Collectors.toSet());

results.stream().forEach(benchmark -> {
final String json = BenchmarkJsonFactory.toJson(benchmark);
final String filename = UUID.randomUUID().toString() + ".json";
try (final FileWriter fileWriter = new FileWriter(filename)) {
fileWriter.write(json);
} catch (final IOException e) {
e.printStackTrace();
}
});

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.openelements.jmh.client;

import com.openelements.jmh.client.io.RestClient;
import com.openelements.jmh.client.jmh.BenchmarkFactory;
import com.openelements.jmh.common.BenchmarkExecution;
import com.openelements.jmh.client.factory.BenchmarkFactory;
import com.openelements.jmh.client.json.BenchmarkJsonFactory;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -17,47 +13,37 @@
import org.openjdk.jmh.runner.options.OptionsBuilder;


@Deprecated
public class JmhUploader {

private final Set<Class> classes;

public JmhUploader() {
this.classes = new HashSet<>();
}

public void addBenchmarkClass(final Class cls) {
classes.add(cls);
}

public void run() throws RunnerException {
final OptionsBuilder optionsBuilder = new OptionsBuilder();
classes.forEach(cls -> optionsBuilder.getIncludes().add(cls.getName()));
final Runner runner = new Runner(optionsBuilder.build());
Collection<RunResult> run = runner.run();

Set<BenchmarkExecution> results = run.stream()
.map(result -> BenchmarkFactory.convert(result))
.collect(Collectors.toSet());

results.stream().forEach(benchmark -> {
final String json = BenchmarkJsonFactory.toJson(benchmark);

try {
final HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://localhost:8080/benchmark"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();

final HttpResponse<Void> response = HttpClient
.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.discarding());
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
});

}
private final Set<Class> classes;

public JmhUploader() {
this.classes = new HashSet<>();
}

public void addBenchmarkClass(final Class cls) {
classes.add(cls);
}

public void run() throws RunnerException {
final OptionsBuilder optionsBuilder = new OptionsBuilder();
classes.forEach(cls -> optionsBuilder.getIncludes().add(cls.getName()));
final Runner runner = new Runner(optionsBuilder.build());
Collection<RunResult> run = runner.run();

final Set<BenchmarkExecution> results = run.stream()
.map(result -> BenchmarkFactory.convert(result))
.collect(Collectors.toSet());

final RestClient restClient = new RestClient("http://localhost:8080");
results.stream().forEach(benchmark -> {
try {
restClient.post(benchmark);
} catch (Exception e) {
e.printStackTrace();
}
});

}
}
24 changes: 24 additions & 0 deletions jmh-client/src/main/java/com/openelements/jmh/client/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.openelements.jmh.client;

import com.openelements.jmh.client.io.FileClient;
import com.openelements.jmh.client.io.RestClient;
import com.openelements.jmh.client.jmh.BenchmarkFactory;
import com.openelements.jmh.client.jmh.JmhExecutor;

public class Main {

public static void main(String[] args) throws Exception {
final RestClient restClient = new RestClient();
final FileClient fileClient = new FileClient("target/benchmark/");
JmhExecutor.executeAll().stream()
.map(BenchmarkFactory::convert)
.forEach(benchmarkExecution -> {
try {
fileClient.write(benchmarkExecution);
restClient.post(benchmarkExecution);
} catch (final Exception e) {
throw new RuntimeException("Error in posting result", e);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.openelements.jmh.client.io;

import com.openelements.jmh.client.json.BenchmarkJsonFactory;
import com.openelements.jmh.common.BenchmarkExecution;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Objects;

public class FileClient {

private final Path directory;

public FileClient(@NonNull final String directoryPath) {
this(Path.of(directoryPath));
}

public FileClient(@NonNull final Path directory) {
this.directory = Objects.requireNonNull(directory, "directory must not be null");
}

public void write(@NonNull final Collection<BenchmarkExecution> benchmarkExecutions) throws Exception {
Objects.requireNonNull(benchmarkExecutions, "benchmarkExecutions must not be null");
for (final BenchmarkExecution benchmarkExecution : benchmarkExecutions) {
write(benchmarkExecution);
}
}

public void write(@NonNull final BenchmarkExecution benchmarkExecution) throws Exception {
Objects.requireNonNull(benchmarkExecution, "benchmarkExecution must not be null");
directory.toFile().mkdirs();
final File file = new File(directory.toFile(), benchmarkExecution.benchmarkName() + ".json");
final String json = BenchmarkJsonFactory.toJson(benchmarkExecution);
Files.deleteIfExists(file.toPath());
Files.writeString(file.toPath(), json, StandardCharsets.UTF_8, StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.openelements.jmh.client.io;

import com.openelements.jmh.client.json.BenchmarkJsonFactory;
import com.openelements.jmh.common.BenchmarkExecution;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.util.Objects;

public class RestClient {

public static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";
public static final String CONTENT_TYPE_HEADER_VALUE = "application/json";
public static final String LOCALHOST = "http://localhost:8080";
private final String baseUrl;

public RestClient(@NonNull String baseUrl) {
this.baseUrl = Objects.requireNonNull(baseUrl, "baseUrl must not be null");
}

public RestClient() {
this(LOCALHOST);
}

public void post(@NonNull BenchmarkExecution benchmarkExecution) throws Exception {
final String json = BenchmarkJsonFactory.toJson(benchmarkExecution);
post(json);
}

private void post(@NonNull String json) throws Exception {
final BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString(json);
final HttpRequest request = HttpRequest.newBuilder()
.uri(getUrl())
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE)
.POST(bodyPublisher)
.build();

final HttpClient httpClient = HttpClient.newBuilder().build();
final BodyHandler<Void> bodyHandler = HttpResponse.BodyHandlers.discarding();
final HttpResponse<Void> response = httpClient.send(request, bodyHandler);
System.out.println(response);
}

@NonNull
private URI getUrl() throws URISyntaxException {
if (baseUrl.endsWith("/")) {
return new URI(baseUrl + "api/benchmark");
} else {
return new URI(baseUrl + "/api/benchmark");
}
}

}
Loading

0 comments on commit a271f10

Please sign in to comment.