-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c346443
commit a271f10
Showing
17 changed files
with
460 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
jmh-client/src/main/java/com/openelements/jmh/client/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
jmh-client/src/main/java/com/openelements/jmh/client/io/FileClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
jmh-client/src/main/java/com/openelements/jmh/client/io/RestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.