Skip to content

Commit

Permalink
recreated PerfTestApp as springboot application (#834)
Browse files Browse the repository at this point in the history
* converting PerfTestApp to spring boot

* finished spring boot perf app implementation

* fixed jsp compilation
misc cleanup

* removed comment

* fixed visibility

* using try with resources
  • Loading branch information
littleaj authored Mar 11, 2019
1 parent cf45044 commit e78f5e5
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ include ':test:smoke:framework:fakeIngestion:servlet'
include ':test:smoke:framework:fakeIngestion:standalone'

include ':test:smoke:testApps:PerfTestApp'
include ':test:smoke:testApps:PerfTestAppSpringBoot'
//include ':test:smoke:testApps:SimpleCalculator'
include ':test:smoke:testApps:CachingCalculator'
include ':test:smoke:testApps:CoreAndFilter'
Expand Down
48 changes: 48 additions & 0 deletions test/smoke/testApps/PerfTestAppSpringBoot/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.19.RELEASE'
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'

compileJava.sourceCompatibility = 1.7
compileJava.targetCompatibility = 1.7
compileSmokeTestJava.sourceCompatibility = 1.8
compileSmokeTestJava.targetCompatibility = 1.8

dependencies {
compile springBootStarterJar
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat'
compile 'com.google.guava:guava:20.0'
compile 'org.apache.httpcomponents:httpclient:4.5.3'

compileOnly 'org.apache.tomcat.embed:tomcat-embed-jasper'
compile 'javax.servlet:jstl'

compile project(':test:smoke:framework:testCases')

providedCompile 'javax.servlet:javax.servlet-api:3.0.1'

providedRuntime 'mysql:mysql-connector-java:5.1.44'

smokeTestCompile 'com.google.guava:guava:23.0'
testCompile 'com.google.guava:guava:23.0' // VSCODE intellisense bug workaround

testCompile group: 'junit', name: 'junit', version: '4.12'
}

configurations {
smokeTestCompile.exclude group:'org.springframework.boot'
smokeTestRuntime.exclude group:'org.springframework.boot'
}

ext.testAppArtifactDir = war.destinationDir
ext.testAppArtifactFilename = war.archiveName
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.microsoft.applicationinsights.testapps.perf;

import com.google.common.base.Stopwatch;

import java.util.concurrent.TimeUnit;

public class TestCaseRunnable implements Runnable {
private final Runnable op;

private static final int MAX_LOOP = 250;

private final String name;

public TestCaseRunnable(Runnable op) {
this(op, null);
}

public TestCaseRunnable(Runnable op, String name) {
if (op == null) {
this.op = new Runnable(){
@Override
public void run() {
// NOP
}
};
this.name = (name == null) ? "NOP" : name;
} else {
this.op = op;
this.name = (name == null) ? "some operation" : name;
}
}

private void pre() {
churn("pre");
}

private void post() {
churn("post");
}

public void run() {
pre();
Stopwatch sw = Stopwatch.createStarted();
op.run();
System.out.printf("%s ran in %dms.%n", name, sw.elapsed(TimeUnit.MILLISECONDS));
post();
sw.stop();
}

private void churn(String name) {
Stopwatch sw = Stopwatch.createStarted();
double d = 100.0;
for(int i = 1; i <= MAX_LOOP; i++) {
d += d / Math.abs(-(double)i);
}
System.out.printf("%s loop finished %d iterations in %dms (d=%f)%n", name, MAX_LOOP, sw.elapsed(TimeUnit.MILLISECONDS), d);
sw.stop();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.microsoft.applicationinsights.testapps.perf;

public abstract class TestCaseRunnableFactory {

private final String name;

public TestCaseRunnableFactory() {
this(null);
}
public TestCaseRunnableFactory(String name) {
this.name = name;
}

public TestCaseRunnable get() {
return new TestCaseRunnable(getRunnable(), name);
}

protected abstract Runnable getRunnable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.microsoft.applicationinsights.testapps.perf.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class PerfTestAppSpringBoot extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
return applicationBuilder.sources(PerfTestAppSpringBoot.class);
}

public static void main(String[] args) {
SpringApplication.run(PerfTestAppSpringBoot.class, args);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.microsoft.applicationinsights.testapps.perf.boot;

import com.google.common.base.Stopwatch;
import com.microsoft.applicationinsights.testapps.perf.TestCaseRunnable;

import java.util.concurrent.TimeUnit;

public class SpringBootPerfTestHelper {
public static String runTest(TestCaseRunnable runnable) {
Stopwatch sw = Stopwatch.createStarted();
try {
runnable.run();
} finally {
sw.stop();
return String.valueOf(sw.elapsed(TimeUnit.MILLISECONDS));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.microsoft.applicationinsights.testapps.perf.boot.controllers;

import com.microsoft.applicationinsights.testapps.perf.TestCaseRunnable;
import com.microsoft.applicationinsights.testapps.perf.boot.SpringBootPerfTestHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BaselineController {
@GetMapping("/baseline")
public String baseline() {
return SpringBootPerfTestHelper.runTest(new TestCaseRunnable(null, "baseline"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.microsoft.applicationinsights.testapps.perf.boot.controllers;

import com.google.common.io.CharStreams;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.telemetry.Duration;
import com.microsoft.applicationinsights.testapps.perf.TestCaseRunnable;
import com.microsoft.applicationinsights.testapps.perf.boot.SpringBootPerfTestHelper;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;

@RestController
public class FakeRestController {

@Autowired
private TelemetryClient tc;

@GetMapping("/fakeRest")
public String fakeRest(@RequestParam(value = "url", required = false) final String pUrl) {
return SpringBootPerfTestHelper.runTest(new TestCaseRunnable(new Runnable() {
@Override
public void run() {
final String url = (pUrl == null)
? "http://www.msn.com"
: ((pUrl.startsWith("http://"))
? pUrl
: "http://"+pUrl);

try (CloseableHttpClient client = HttpClientBuilder.create().disableAutomaticRetries().build()) {
HttpGet get = new HttpGet(url);
try (CloseableHttpResponse getResp = client.execute(get)) {
HttpEntity entity = getResp.getEntity();
StringWriter cw = new StringWriter();
CharStreams.copy(new InputStreamReader(entity.getContent()), cw);
EntityUtils.consume(entity);
StatusLine status = getResp.getStatusLine();
System.out.printf("GET %s responded %d %s. response size: %d%n", url, status.getStatusCode(), status.getReasonPhrase(), cw.toString().length());
}
catch (IOException e) {
System.err.println("Error requesting "+url+".");
e.printStackTrace();
}
}
catch (IOException e) {
System.err.println("Error creating http client");
e.printStackTrace();
}
tc.trackDependency("FakeRestDependency", "fakeRestCommand", new Duration(123L), true);
tc.trackEvent("FakeRestEvent");
tc.trackMetric("FakeRestMetric", 1.0);
tc.trackTrace("FakeRestTrace");
}
}, "fakeRest operation"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.microsoft.applicationinsights.testapps.perf.boot.controllers;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.smoketest.FixedAiTestCases;
import com.microsoft.applicationinsights.testapps.perf.TestCaseRunnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.microsoft.applicationinsights.testapps.perf.boot.SpringBootPerfTestHelper.runTest;

@RestController
@RequestMapping("/track")
public class TrackController {


private FixedAiTestCases testCases;
@Autowired
private HttpServletRequest request;

public TrackController(TelemetryClient telemetryClient) {
testCases = new FixedAiTestCases(telemetryClient);
}

private void printUri() {
System.out.println("GET "+request.getRequestURI());
}

@GetMapping("/metric/{level}/{type}")
public String trackMetric(@PathVariable String level, @PathVariable String type) {
printUri();
final TestCaseRunnable tcr;
String name = String.format("metric %s %s", level, type);
if ("full".equals(level) && "measurement".equals(type)) {
tcr = new TestCaseRunnable(testCases.getTrackMetric_FullMeasurement(), name);
} else if ("full".equals(level) && "aggregate".equals(type)) {
tcr = new TestCaseRunnable(testCases.getTrackMetric_FullAggregate(), name);
} else if ("helper".equals(level) && "measurement".equals(type)) {
tcr = new TestCaseRunnable(testCases.getTrackMetric_HelperMeasurement(), name);
} else if ("helper".equals(level) && "aggregate".equals(type)) {
tcr = new TestCaseRunnable(testCases.getTrackMetric_HelperAggregate(), name);
} else {
throw new UnsupportedOperationException("unknown level/type pair: "+name);
}

return runTest(tcr);
}

@GetMapping("/event")
public String trackEvent() {
printUri();
return runTest(new TestCaseRunnable(testCases.getTrackEvent(), "event"));
}

@GetMapping("/request/full")
public String trackRequestFull() {
printUri();
return runTest(new TestCaseRunnable(testCases.getTrackRequest_Full(), "request full"));
}

@GetMapping("/httpRequest")
public String trackHttpRequest() {
printUri();
return runTest(new TestCaseRunnable(testCases.getTrackHttpRequest_Success(), "http request"));
}

@GetMapping({"/dependency/{level}", "/dependency"})
public String trackDependency(@PathVariable(value = "level", required = false) String level) {
printUri();
final TestCaseRunnable tcr;
if ("full".equals(level)) {
tcr = new TestCaseRunnable(testCases.getTrackDependency_Full(), "dependency full");
} else if (level == null) {
tcr = new TestCaseRunnable(testCases.getTrackDependency(), "dependency simple");
} else {
throw new UnsupportedOperationException("Unknown dependency level: "+level);
}
return runTest(tcr);
}

@GetMapping("/exception")
public String trackException() {
printUri();
return runTest(new TestCaseRunnable(testCases.getTrackException(), "exception"));
}

@GetMapping("/trace")
public String trackTrace() {
printUri();
return runTest(new TestCaseRunnable(testCases.getTrackTrace(), "trace"));
}

@GetMapping({"/pageView/{level}", "/pageView"})
public String trackPageView(@PathVariable(value = "level", required = false) String level) {
printUri();
final TestCaseRunnable tcr;
if ("full".equals(level)) {
tcr = new TestCaseRunnable(testCases.getTrackPageView_Full(), "pageView full");
} else if (level == null) {
tcr = new TestCaseRunnable(testCases.getTrackPageView(), "pageView simple");
} else {
throw new UnsupportedOperationException("Unknown pageView level: "+level);
}
return runTest(tcr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.microsoft.applicationinsights.testapps.perf.boot.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ViewController {

@RequestMapping({"/", "/index.jsp"})
public ModelAndView viewIndex() {
return new ModelAndView("index");
}
}
Loading

0 comments on commit e78f5e5

Please sign in to comment.