Add benchmark in separate project

Change-Id: Ib0d29c334cf46744e33fa609eedce52892740af7
This commit is contained in:
Dominik Adam Przybysz 2015-12-15 05:42:22 +01:00 committed by Dominik Przybysz
parent 23e2813e2b
commit f110ffe8a9
3 changed files with 148 additions and 0 deletions

67
performance-tests/pom.xml Normal file
View file

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>http-mock-server</artifactId>
<groupId>pl.touk.mockserver</groupId>
<version>2.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mockserver-performance-tests</artifactId>
<dependencies>
<dependency>
<groupId>pl.touk.mockserver</groupId>
<artifactId>mockserver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>pl.touk.mockserver</groupId>
<artifactId>mockserver-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.11.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>run-benchmarks</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.openjdk.jmh.Main</argument>
<argument>.*</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,80 @@
package pl.touk.mockserver.client;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.infra.ThreadParams;
import pl.touk.mockserver.api.request.AddMock;
import pl.touk.mockserver.server.HttpMockServer;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
public class MockserverTest {
HttpMockServer httpMockServer;
@Setup
public void prepareMockServer(BenchmarkParams params) {
try {
httpMockServer = new HttpMockServer(9999);
} catch (Exception e) {
//OK
}
}
@TearDown
public void stopMockServer() {
try {
httpMockServer.stop();
} catch (Exception e) {
}
}
@State(Scope.Thread)
public static class TestState {
RemoteMockServer remoteMockServer;
HttpClient httpClient;
int current;
@Setup
public void prepareMockServer(ThreadParams params) {
remoteMockServer = new RemoteMockServer("localhost", 9999);
httpClient = HttpClients.createDefault();
current = params.getThreadIndex();
}
}
@Benchmark
@Measurement(iterations = 60)
@Fork(warmups = 1, value = 1)
@BenchmarkMode({Mode.AverageTime, Mode.Throughput})
@Warmup(iterations = 10)
@Threads(4)
public void shouldHandleManyRequestsSimultaneously(TestState testState) throws IOException {
int current = testState.current;
int endpointNumber = current % 10;
int port = 9000 + (current % 7);
AddMock addMock = new AddMock();
addMock.setName("testRest" + current);
addMock.setPath("testEndpoint" + endpointNumber);
addMock.setPort(port);
addMock.setPredicate("{req -> req.xml.name() == 'request" + current + "' }");
addMock.setResponse("{req -> '<goodResponse" + current + "/>'}");
testState.remoteMockServer.addMock(addMock);
HttpPost restPost = new HttpPost("http://localhost:" + port + "/testEndpoint" + endpointNumber);
restPost.setEntity(new StringEntity("<request" + current + "/>", ContentType.create("text/xml", "UTF-8")));
CloseableHttpResponse response = (CloseableHttpResponse) testState.httpClient.execute(restPost);
String stringResponse = Util.extractStringResponse(response);
testState.remoteMockServer.removeMock("testRest" + current, true);
assert stringResponse.equals("<goodResponse" + current + "/>");
}
}

View file

@ -17,6 +17,7 @@
<module>mockserver</module>
<module>mockserver-tests</module>
<module>mockserver-api</module>
<module>performance-tests</module>
</modules>
<properties>