|
| 1 | +package com.emorozov.datasciencelab; |
| 2 | + |
| 3 | +import com.emorozov.datasciencelab.sampler.CpuSampler; |
| 4 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import cucumber.api.java.Before; |
| 8 | +import cucumber.api.java.en.Given; |
| 9 | +import cucumber.api.java.en.Then; |
| 10 | +import cucumber.api.java.en.When; |
| 11 | +import lombok.AllArgsConstructor; |
| 12 | +import lombok.Data; |
| 13 | +import org.apache.jmeter.control.LoopController; |
| 14 | +import org.apache.jmeter.engine.StandardJMeterEngine; |
| 15 | +import org.apache.jmeter.protocol.http.sampler.HTTPSampler; |
| 16 | +import org.apache.jmeter.reporters.ResultCollector; |
| 17 | +import org.apache.jmeter.testelement.TestPlan; |
| 18 | +import org.apache.jmeter.threads.ThreadGroup; |
| 19 | +import org.apache.jmeter.util.JMeterUtils; |
| 20 | +import org.apache.jorphan.collections.HashTree; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | +import static org.hamcrest.number.OrderingComparison.lessThan; |
| 30 | + |
| 31 | +public class DataScienceLabDemoTestPerformanceAnomaliesSteps { |
| 32 | + |
| 33 | + @Data |
| 34 | + public class Endpoint { |
| 35 | + private String domain; |
| 36 | + private int port; |
| 37 | + private String path; |
| 38 | + private String method; |
| 39 | + } |
| 40 | + |
| 41 | + @Data |
| 42 | + @AllArgsConstructor |
| 43 | + private static class RInput { |
| 44 | + private String metrics; |
| 45 | + private Integer samplerCount; |
| 46 | + private Double criticalQiantile; |
| 47 | + } |
| 48 | + |
| 49 | + @Data |
| 50 | + private static class ROutput { |
| 51 | + private Integer outliers; |
| 52 | + @JsonCreator |
| 53 | + public ROutput(@JsonProperty("outliers") Integer outliers) { |
| 54 | + this.outliers = outliers; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private int samplersCount = 0; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void clearTemporaryFiles() throws Exception { |
| 62 | + Files.deleteIfExists(Paths.get("metrics.csv")); |
| 63 | + Files.deleteIfExists(Paths.get("input.json")); |
| 64 | + Files.deleteIfExists(Paths.get("output.json")); |
| 65 | + } |
| 66 | + |
| 67 | + @Given("^endpoints are up and running$") |
| 68 | + public void systemEndpointsAreUpAndRunning(final List<Endpoint> endpoints) throws Exception { |
| 69 | + // TODO Actually validate that endpoints are up and running |
| 70 | + } |
| 71 | + |
| 72 | + @When("^(.+) requests are submitted for endpoints$") |
| 73 | + public void requestsAreSubmitted(final Integer loops, |
| 74 | + final List<Endpoint> endpoints) { |
| 75 | + |
| 76 | + // Set status vars - number of endpoints and a CPU sampler |
| 77 | + this.samplersCount = endpoints.size() + 1; |
| 78 | + |
| 79 | + // Create JMeter engine |
| 80 | + StandardJMeterEngine jmeter = new StandardJMeterEngine(); |
| 81 | + |
| 82 | + // Init JMeter engine |
| 83 | + // TODO Refactor config |
| 84 | + JMeterUtils.setJMeterHome("JMETER_HOME"); |
| 85 | + JMeterUtils.loadJMeterProperties("JMETER_HOME/bin/jmeter.properties"); |
| 86 | + JMeterUtils.initLocale(); |
| 87 | + |
| 88 | + // Build samplers, use path for name of the sampler |
| 89 | + Stream<HTTPSampler> httpSamplers = endpoints.stream().map(endpoint -> { |
| 90 | + HTTPSampler httpSampler = new HTTPSampler(); |
| 91 | + httpSampler.setName(endpoint.getPath()); |
| 92 | + httpSampler.setDomain(endpoint.getDomain()); |
| 93 | + httpSampler.setPort(endpoint.getPort()); |
| 94 | + httpSampler.setPath(endpoint.getPath()); |
| 95 | + httpSampler.setMethod(endpoint.getMethod()); |
| 96 | + return httpSampler; |
| 97 | + }); |
| 98 | + |
| 99 | + // Build a CPU sampler |
| 100 | + CpuSampler cpuSampler = new CpuSampler(); |
| 101 | + cpuSampler.setName("cpu"); |
| 102 | + |
| 103 | + // Loop Controller |
| 104 | + LoopController loopController = new LoopController(); |
| 105 | + loopController.setName("Default Loop Controller"); |
| 106 | + loopController.setLoops(loops); |
| 107 | + loopController.setFirst(true); |
| 108 | + loopController.initialize(); |
| 109 | + |
| 110 | + // Thread Group |
| 111 | + org.apache.jmeter.threads.ThreadGroup threadGroup = new ThreadGroup(); |
| 112 | + threadGroup.setName("Default Thread Group"); |
| 113 | + threadGroup.setNumThreads(1); |
| 114 | + threadGroup.setRampUp(1); |
| 115 | + threadGroup.setSamplerController(loopController); |
| 116 | + |
| 117 | + // Create results collector |
| 118 | + ResultCollector logger = new ResultCollector(); |
| 119 | + logger.setName("Default Results Collector"); |
| 120 | + logger.setFilename("metrics.csv"); |
| 121 | + |
| 122 | + // Create a test structure |
| 123 | + HashTree testPlanTree = new HashTree(); |
| 124 | + |
| 125 | + // Add Test Plan |
| 126 | + TestPlan testPlan = new TestPlan("Sample R integration test plan"); |
| 127 | + testPlanTree.add(testPlan); |
| 128 | + |
| 129 | + // Hang off Thread Group off Test Plan |
| 130 | + HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup); |
| 131 | + |
| 132 | + // Add samplers |
| 133 | + threadGroupHashTree.add(httpSamplers.toArray()); |
| 134 | + threadGroupHashTree.add(cpuSampler); |
| 135 | + |
| 136 | + // Add logger |
| 137 | + testPlanTree.add(testPlan, logger); |
| 138 | + |
| 139 | + // Run Test Plan |
| 140 | + jmeter.configure(testPlanTree); |
| 141 | + jmeter.run(); |
| 142 | + threadGroup.waitThreadsStopped(); |
| 143 | + } |
| 144 | + |
| 145 | + @Then("^metrics outliers are below (.+) percent with critical quantile (.+)$") |
| 146 | + public void outliersAcrossInputAreBelowThreshold(final Integer outliersThreshold, |
| 147 | + final Double criticalQuantile) |
| 148 | + throws Exception { |
| 149 | + |
| 150 | + // Get the object mapper |
| 151 | + ObjectMapper mapper = new ObjectMapper(); |
| 152 | + |
| 153 | + // Create inputs for analytics |
| 154 | + RInput input = new RInput("metrics.csv", this.samplersCount, criticalQuantile); |
| 155 | + |
| 156 | + mapper.writeValue(new File("input.json"), input); |
| 157 | + |
| 158 | + // Run analytics |
| 159 | + // TODO Refactor config |
| 160 | + Runtime rt = Runtime.getRuntime(); |
| 161 | + Process pr = rt.exec("Rscript ./src/test/r/performance-anomalies.R"); |
| 162 | + pr.waitFor(); |
| 163 | + |
| 164 | + // Get the outputs |
| 165 | + ROutput output = mapper.readValue(new File("output.json"), ROutput.class); |
| 166 | + |
| 167 | + // Assert |
| 168 | + assertThat(output.getOutliers(), lessThan(outliersThreshold)); |
| 169 | + } |
| 170 | +} |
0 commit comments