Skip to content

Instantly share code, notes, and snippets.

@polarnik
Last active September 11, 2024 11:29
Show Gist options
  • Save polarnik/a4ff9881330fe2a8cb49eb44936f6191 to your computer and use it in GitHub Desktop.
Save polarnik/a4ff9881330fe2a8cb49eb44936f6191 to your computer and use it in GitHub Desktop.
import org.slf4j.Logger;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.output.WaitingConsumer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
{
private String getScriptPathInProject() {
String pathToK6 = System.getProperty("project.projectDir") + "/src/main/k6";
return pathToK6;
}
private String getHarDumpPath() {
String pathToK6 = System.getProperty("project.projectDir") + "/src/main/python/mitmproxy/har_dump.py";
return pathToK6;
}
private String getScriptPathInDocker() {
return "/tmp/src";
}
private String getPathToResult() {
String pathToResult = System.getProperty("project.buildDir") + "/k6/" + getTestName();
return pathToResult;
}
private String getPathToHar() {
String pathToResult = System.getProperty("project.buildDir") + "/mitmproxy/" + getTestName();
return pathToResult;
}
private String getSummaryJsonPath() {
return getPathToResult() + "/summary.json";
}
private String getSummaryJsonDubugPath() {
return getPathToResult() + "/debug.json";
}
private String getSummaryTxtPath() {
return getPathToResult() + "/summary.txt";
}
private GenericContainer getProxy(Network network, Logger logger) {
createDirectoryAndGrandAccess(getPathToHar());
GenericContainer proxy = new GenericContainer(DockerImageName.parse("mitmproxy/mitmproxy:9.0.1"))
.withNetworkAliases("mitmproxy")
.withNetwork(network)
.withFileSystemBind(getHarDumpPath(), "/har_dump.py", BindMode.READ_ONLY)
.withFileSystemBind(getPathToHar(), "/tmp/results", BindMode.READ_WRITE)
.withCommand("/usr/local/bin/mitmdump -s /har_dump.py --set hardump=/tmp/results/dump.har");
Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);
proxy.withLogConsumer(logConsumer);
return proxy;
}
private GenericContainer getRedis(Network network, Logger logger) {
GenericContainer redis = new GenericContainer(DockerImageName.parse("redis:7.0.11"))
.withNetworkAliases("redis")
.withNetwork(network);
return redis;
}
private void createDirectoryAndGrandAccess(String path) {
File directory = new File(path);
if (!directory.exists()) {
if (directory.mkdirs()) {
log.info("Directory was created {}", path);
} else {
log.error("Directory was not created {}", path);
}
}
directory.setReadable(true, false);
directory.setWritable(true, false);
directory.setExecutable(true, false);
log.info("Directory status. Exist: {}, canExecute: {}, canRead: {}, canWrite: {}",
directory.isDirectory(),
directory.canExecute(),
directory.canRead(),
directory.canWrite()
);
}
private GenericContainer getk6(Network network, Logger logger) {
String pathToConfig = System.getProperty("project.projectDir") + "/config";
createDirectoryAndGrandAccess(getPathToResult());
var config = ConfigFactory.create(TestConfig.class);
GenericContainer container = new GenericContainer(DockerImageName.parse(config.k6Image()))
.withEnv("K6_INFLUXDB_USERNAME", config.K6_INFLUXDB_USERNAME())
.withEnv("K6_INFLUXDB_PASSWORD", config.K6_INFLUXDB_PASSWORD())
.withEnv("K6_INFLUXDB_PUSH_INTERVAL", config.K6_INFLUXDB_PUSH_INTERVAL())
.withEnv("K6_NO_USAGE_REPORT", "true")
.withEnv("BUILD_ID", config.BUILD_ID())
.withEnv("JOB_NAME", config.JOB_NAME())
.withEnv("NODE_NAME", config.NODE_NAME())
.withEnv("BUILD_URL", config.BUILD_URL())
.withEnv("SERVER_VERSION", config.SERVER_VERSION())
.withEnv("TEST_CONFIG", getTEST_CONFIG())
.withEnv("TEST_ENVIRONMENT", getTEST_ENVIRONMENT())
.withEnv("TEST_SCIM_GROUP_SIZE", String.valueOf(getTEST_SCIM_GROUP_SIZE()))
.withEnv("TEST_NAME", getTestName())
.withEnv("K6_ITERATIONS", String.valueOf(getK6_ITERATIONS()))
.withEnv("K6_RPS", String.valueOf(getK6_RPS()))
.withEnv("K6_VUS", String.valueOf(getK6_VUS()))
.withEnv("K6_THROW", "true")
.withEnv("TARGET_DURATION", String.valueOf(getTARGET_DURATION()))
.withEnv("K6_SUMMARY_EXPORT", "summary.json")
.withFileSystemBind(getScriptPathInProject(), getScriptPathInDocker(), BindMode.READ_ONLY)
.withFileSystemBind(pathToConfig, "/tmp/config", BindMode.READ_ONLY)
.withFileSystemBind(getPathToResult(), "/tmp/results", BindMode.READ_WRITE)
.withWorkingDirectory("/tmp/results");
if(config.K6_INFLUXDB_ADDRESS().isEmpty()) {
container.withCommand("run" +
" " + getScriptPathInDocker() + "/" + getK6_SCENARIO_PATH());
} else {
container.withCommand("run" +
" --out csv=/tmp/results/metrics.csv" +
" --out influxdb=" + config.K6_INFLUXDB_ADDRESS().trim() +
" " + getScriptPathInDocker() + "/" + getK6_SCENARIO_PATH());
}
if (envs != null && !envs.isEmpty()) {
for (Map.Entry<String, String> envVal : envs.entrySet()) {
container.withEnv(envVal.getKey(), envVal.getValue());
}
}
log.info("scriptPath: {}", getScriptPathInProject());
log.info("scenario: {}", getK6_SCENARIO_PATH());
log.info("/tmp/config {}", pathToConfig);
log.info("/tmp/results {}", getPathToResult());
log.info("SCIM SIZE", TEST_SCIM_GROUP_SIZE);
if (config.USE_PROXY() && network != null) {
container
.withNetwork(network)
.withEnv("NO_PROXY", "raw.githubusercontent.com,jslib.k6.io")
.withEnv("HTTP_PROXY", "mitmproxy:8080")
.withEnv("HTTPS_PROXY", "mitmproxy:8080")
;
}
if (getK6_DURATION_MINUTE() > 0) {
container.withEnv("K6_DURATION", String.valueOf(getK6_DURATION_MINUTE()) + "m");
}
Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);
container.withLogConsumer(logConsumer);
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment