First experiment running nb5 in process invoker

This commit is contained in:
MikeYaacoubStax 2023-02-01 11:00:11 -05:00
parent 82abce9cb4
commit 0638c71f6f
3 changed files with 203 additions and 4 deletions

View File

@ -23,25 +23,43 @@ import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
import java.io.IOException;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
public class ExampleContainers {
private final String java = Optional.ofNullable(System.getenv(
"JAVA_HOME")).map(v -> v + "/bin/java").orElse("java");
private final static String JARNAME = "../nb5/target/nb5.jar";
// private static GenericContainer cass= new CassandraContainer("cassandra").withExposedPorts(9042);
public static GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:3.0.6"))
public static GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:latest"))
.withExposedPorts(6379);
@BeforeAll
public static void initContainer() {
redis.start();
redis.start();
}
@BeforeEach
public void setUp() {
System.out.println("foo");
System.out.println("setup");
}
@Test
public void testSimplePutAndGet() {
System.out.println("foo");
ProcessInvoker invoker = new ProcessInvoker();
invoker.setLogDir("logs/test");
ProcessResult result = invoker.run("test-workloads", 30,
"java", "-jar", JARNAME, "--list-workloads"
);
assertThat(result.exception).isNull();
String stdout = String.join("\n", result.getStdoutData());
System.out.println(stdout);
System.out.println("end");
}
}

View File

@ -0,0 +1,113 @@
/*
* Copyright (c) 2022 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.nb5.proof;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.util.concurrent.TimeUnit;
public class ProcessInvoker {
private static final Logger logger = LogManager.getLogger(ProcessInvoker.class);
private File runDirectory = new File(".");
private File logDirectory = new File(".");
public ProcessInvoker() {
}
/**
* Run the process with a specified timeout and alias.
*
* @param alias the name of the process for logging and result gathering
* @param timeoutSeconds the number of seconds to wait for the process to return
* @param cmdline the command line
* @return a ProcessResult
*/
public ProcessResult run(String alias, int timeoutSeconds, String... cmdline) {
ProcessResult result = new ProcessResult();
result.stdout = getStdOutFile(alias);
result.stderr = getStdErrFile(alias);
long startNanosTime = 0;
Process process = null;
ProcessBuilder pb = new ProcessBuilder(cmdline);
pb.redirectError(result.stderr);
pb.redirectOutput(result.stdout);
startNanosTime = System.nanoTime();
try {
result.cmdDir = new File(".").getCanonicalPath();
process = pb.start();
var handle = process.toHandle();
boolean terminated = process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
if (!terminated) {
process.destroyForcibly().waitFor();
result.exception = new RuntimeException("timed out waiting for process, so it was shutdown forcibly.");
}
} catch (Exception e) {
if (process != null) {
logger.debug("Exception received, with exit value: " + process.exitValue());
}
result.exception = e;
} finally {
result.startNanosTime = startNanosTime;
if (startNanosTime != 0) {
long endNanosTime = System.nanoTime();
result.durationNanos = endNanosTime - startNanosTime;
}
if (process != null) {
result.exitStatus = process.exitValue();
} else {
result.exitStatus = 255;
}
}
return result;
}
public ProcessInvoker setLogDir(String logDir) {
logDirectory = new File(logDir);
return this;
}
public ProcessInvoker setRunDir(String runDir) {
runDirectory = new File(runDir);
return this;
}
public File getStdOutFile(String alias) {
return mkdirsFor(new File(logDirectory.getPath() + File.separator + alias + ".stdout"));
}
public File getStdErrFile(String alias) {
return mkdirsFor(new File(logDirectory.getPath() + File.separator + alias + ".stderr"));
}
private File mkdirsFor(File file) {
if (!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
throw new RuntimeException("Could not create directories for " + file);
}
}
return file;
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2022 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.nb5.proof;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
public class ProcessResult {
public int exitStatus = -1;
public File stderr;
public File stdout;
public double durationNanos;
public Exception exception;
public static int NORMAL_EXIT = 0;
public long startNanosTime;
public String cmdDir;
public boolean isNormal() {
return exception==null && exitStatus == NORMAL_EXIT;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("--- process result\nstatus:%d", exitStatus));
sb.append(String.format(" time:%.3fs", (durationNanos/1000000000.0)));
sb.append(String.format(" dir:%s",cmdDir));
sb.append("\n--- stdout:\n");
sb.append(getStdoutData().stream().collect(Collectors.joining("\n")));
sb.append("\n--- stderr:\n");
sb.append(getStderrData().stream().collect(Collectors.joining("\n","","\n")));
sb.append("---\n");
return sb.toString();
}
public List<String> getStdoutData() {
try {
return Files.readAllLines(stdout.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public List<String> getStderrData() {
try {
return Files.readAllLines(stderr.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}