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

api keys added #168

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.openelements.benchscape.jmh.client.io.FileHandler;
import com.openelements.benchscape.jmh.client.io.RestHandler;
import com.openelements.benchscape.jmh.client.io.RestHandlerConfig;
import com.openelements.benchscape.jmh.client.jmh.BenchmarkFactory;
import com.openelements.benchscape.jmh.client.jmh.JmhExecutor;
import com.openelements.benchscape.jmh.model.BenchmarkExecution;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import org.openjdk.jmh.results.RunResult;

Expand All @@ -27,7 +29,7 @@ public class JmhRunner {

private final Path resultsPath;

private final String uploadBaseUrl;
private final RestHandlerConfig restHandlerConfig;

private final Collection<String> includes;

Expand All @@ -45,25 +47,26 @@ public JmhRunner() {
* @param writeResults whether to write the results to disc
*/
public JmhRunner(boolean uploadResults, boolean writeResults) {
this(uploadResults, writeResults, null, null, null);
this(uploadResults, writeResults, null, new RestHandlerConfig(), null);
}

/**
* Constructor.
*
* @param uploadResults whether to post the results to a server
* @param writeResults whether to write the results to disc
* @param resultsPath the path of the directory to write the results to. If null, the default directory is used
* @param uploadBaseUrl the base URL of the server to post the results to. If null, the default server is used
* @param includes the names of the benchmarks to run. If empty, all benchmarks are run
* @param uploadResults whether to post the results to a server
* @param writeResults whether to write the results to disc
* @param resultsPath the path of the directory to write the results to. If null, the default directory is
* used
* @param restHandlerConfig TODO
* @param includes the names of the benchmarks to run. If empty, all benchmarks are run
*/
public JmhRunner(boolean uploadResults, boolean writeResults, @Nullable Path resultsPath,
@Nullable String uploadBaseUrl,
@Nullable RestHandlerConfig restHandlerConfig,
@Nullable Collection<String> includes) {
this.uploadResults = uploadResults;
this.writeResults = writeResults;
this.resultsPath = resultsPath;
this.uploadBaseUrl = uploadBaseUrl;
this.restHandlerConfig = Objects.requireNonNull(restHandlerConfig, "restHandlerConfig must not be null");
if (includes == null) {
this.includes = Set.of();
} else {
Expand All @@ -87,12 +90,7 @@ public void execute() throws Exception {
final Collection<BenchmarkExecution> benchmarkExecutions = BenchmarkFactory.convert(runResults);

if (uploadResults) {
final RestHandler restHandler;
if (uploadBaseUrl == null) {
restHandler = new RestHandler();
} else {
restHandler = new RestHandler(uploadBaseUrl);
}
final RestHandler restHandler = new RestHandler(restHandlerConfig);
restHandler.post(benchmarkExecutions);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.openelements.benchscape.jmh.client;

import com.openelements.benchscape.jmh.client.io.RestHandlerConfig;
import java.nio.file.Path;
import java.util.Collection;
import java.util.concurrent.Callable;
Expand All @@ -22,17 +23,26 @@ public class JmhRunnerCommand implements Callable<Integer> {
"--path"}, description = "The path of the directory to write the results to. If null, the default directory is used")
private Path resultsPath;

@Parameters(paramLabel = "INCLUDES", description = "The names of the benchmarks to run. If empty, all benchmarks are run")
private Collection<String> includes;

@Option(names = {
"--url"}, description = "The base URL of the server to post the results to. If null, the default server is used")
private String uploadBaseUrl;

@Parameters(paramLabel = "INCLUDES", description = "The names of the benchmarks to run. If empty, all benchmarks are run")
private Collection<String> includes;
@Option(names = {
"--apiPrincipal"}, description = "TODO")
private String apiPrincipal;

@Option(names = {
"--apiKey"}, description = "TODO")
private String apiKey;

@Override
public Integer call() {
try {
new JmhRunner(uploadResults, writeResults, resultsPath, uploadBaseUrl, includes).execute();
new JmhRunner(uploadResults, writeResults, resultsPath,
new RestHandlerConfig(uploadBaseUrl, apiPrincipal, apiKey), includes).execute();
} catch (Exception e) {
e.printStackTrace();
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@
public class RestHandler {

public static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";
public static final String BENCHSCAPE_API_PRINCIPAL_HEADER_NAME = "Benchscape-Api-Principal";

public static final String BENCHSCAPE_API_KEY_HEADER_NAME = "Benchscape-Api-Key";

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

private final String apiPrincipal;

private final String apiKey;

/**
* Creates a new instance.
*
* @param baseUrl the base url of the endpoint
* @param config the config to call the endpoint
*/
public RestHandler(@NonNull String baseUrl) {
this.baseUrl = Objects.requireNonNull(baseUrl, "baseUrl must not be null");
public RestHandler(@NonNull RestHandlerConfig config) {
Objects.requireNonNull(config, "config must not be null");
this.baseUrl = config.getBaseUrl();
this.apiPrincipal = config.getApiPrincipal();
this.apiKey = config.getApiKey();
}

/**
* Creates a new instance that uses {@code http://localhost:8080} as base url for the endpoint.
*/
public RestHandler() {
this(LOCALHOST);
}

/***
* Posts the benchmark execution to the endpoint.
Expand All @@ -62,11 +66,18 @@ public void post(@NonNull BenchmarkExecution benchmarkExecution) throws Exceptio
*/
private void post(@NonNull String json) throws URISyntaxException, IOException, InterruptedException {
final BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString(json);
final HttpRequest request = HttpRequest.newBuilder()

HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(getUrl())
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE)
.POST(bodyPublisher)
.build();
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE);
if (apiPrincipal != null) {
builder = builder.header(BENCHSCAPE_API_PRINCIPAL_HEADER_NAME, apiPrincipal);
}
if (apiKey != null) {
builder = builder.header(BENCHSCAPE_API_KEY_HEADER_NAME, apiKey);
}
final HttpRequest request = builder.build();

final HttpClient httpClient = HttpClient.newBuilder().build();
final BodyHandler<Void> bodyHandler = HttpResponse.BodyHandlers.discarding();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.openelements.benchscape.jmh.client.io;

public class RestHandlerConfig {

private String baseUrl;

private String apiPrincipal;

private String apiKey;

public RestHandlerConfig() {
}

public RestHandlerConfig(String baseUrl) {
this.baseUrl = baseUrl;
}

public RestHandlerConfig(String baseUrl, String apiPrincipal, String apiKey) {
this.baseUrl = baseUrl;
this.apiPrincipal = apiPrincipal;
this.apiKey = apiKey;
}

public String getBaseUrl() {
return baseUrl;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

public String getApiPrincipal() {
return apiPrincipal;
}

public void setApiPrincipal(String apiPrincipal) {
this.apiPrincipal = apiPrincipal;
}

public String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public class JmhMojo extends AbstractMojo {
@Parameter(defaultValue = "https://backend.benchscape.cloud")
private String url;

@Parameter
private String apiPrincipal;

@Parameter
private String apiKey;

@Component
private ArtifactHandlerManager artifactHandlerManager;

Expand Down Expand Up @@ -170,6 +176,8 @@ private int executeBenchmarkRunInForkedProcess(@NonNull final List<String> class
getLog().debug("PARAM 'file'=" + file);
getLog().debug("PARAM 'upload'=" + upload);
getLog().debug("PARAM 'url'=" + url);
getLog().debug("PARAM 'apiPrincipal'=" + apiPrincipal);
getLog().debug("PARAM 'apiKey'=" + apiKey);

final List<String> command = new ArrayList<>();
command.add(javaProcess);
Expand All @@ -186,6 +194,10 @@ private int executeBenchmarkRunInForkedProcess(@NonNull final List<String> class
}
command.add("--url");
command.add(url);
command.add("--apiPrincipal");
command.add(apiPrincipal);
command.add("--apiKey");
command.add(apiKey);
getLog().debug("Running forked execution using: " + command);

final ProcessBuilder processBuilder = new ProcessBuilder(command);
Expand Down
Loading