Browse Source

DATAMONGO-1720 - Polishing.

Enhance benchmark statistics with Git/working tree details. Specify byte encoding for JSON to byte encoder.
Add status code check to HttpResultsWriter to verify that the results were accepted. Convert spaces to tabs in pom.xml.

Original pull request: #483.
pull/474/head
Mark Paluch 9 years ago
parent
commit
82fdbe8cc2
  1. 178
      spring-data-mongodb-benchmarks/pom.xml
  2. 2
      spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterBenchmark.java
  3. 39
      spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/HttpResultsWriter.java
  4. 11
      spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/MongoResultsWriter.java
  5. 6
      spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/ResultsWriter.java

178
spring-data-mongodb-benchmarks/pom.xml

@ -1,91 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?> <?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" <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/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId> <artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version> <version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>spring-data-mongodb-benchmarks</artifactId> <artifactId>spring-data-mongodb-benchmarks</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Spring Data MongoDB - Microbenchmarks</name> <name>Spring Data MongoDB - Microbenchmarks</name>
<properties> <properties>
<skipTests>true</skipTests> <!-- Skip tests by default; run only if -DskipTests=false is specified or benchmarks profile is activated --> <!-- Skip tests by default; run only if -DskipTests=false is specified or benchmarks profile is activated -->
<bundlor.enabled>false</bundlor.enabled> <skipTests>true</skipTests>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles> <dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${project.version}</version>
</dependency>
<profile> <dependency>
<id>benchmarks</id> <groupId>junit</groupId>
<properties> <artifactId>junit</artifactId>
<skipTests>false</skipTests> <version>${junit}</version>
</properties> <scope>compile</scope>
</profile> </dependency>
</profiles>
<build> <dependency>
<plugins> <groupId>org.openjdk.jmh</groupId>
<plugin> <artifactId>jmh-core</artifactId>
<artifactId>maven-jar-plugin</artifactId> <version>${jmh.version}</version>
<executions> </dependency>
<execution>
<id>default-jar</id> <dependency>
<phase>never</phase> <groupId>org.openjdk.jmh</groupId>
</execution> <artifactId>jmh-generator-annprocess</artifactId>
</executions> <version>${jmh.version}</version>
</plugin> <scope>provided</scope>
<plugin> </dependency>
<artifactId>maven-surefire-plugin</artifactId>
<configuration> </dependencies>
<testSourceDirectory>${project.build.sourceDirectory}</testSourceDirectory>
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory> <profiles>
<excludes>
<exclude>**/AbstractMicrobenchmark.java</exclude> <profile>
<exclude>**/*$*.class</exclude> <id>benchmarks</id>
<exclude>**/generated/*.class</exclude> <properties>
</excludes> <skipTests>false</skipTests>
<includes> </properties>
<include>**/*Benchmark*</include> </profile>
</includes> </profiles>
<systemPropertyVariables>
<benchmarkReportDir>${project.build.directory}/reports/performance</benchmarkReportDir> <build>
<project.version>${project.version}</project.version> <plugins>
</systemPropertyVariables> <plugin>
</configuration> <groupId>pl.project13.maven</groupId>
</plugin> <artifactId>git-commit-id-plugin</artifactId>
</plugins> <version>2.2.2</version>
</build> <executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>never</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testSourceDirectory>${project.build.sourceDirectory}</testSourceDirectory>
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
<excludes>
<exclude>**/AbstractMicrobenchmark.java</exclude>
<exclude>**/*$*.class</exclude>
<exclude>**/generated/*.class</exclude>
</excludes>
<includes>
<include>**/*Benchmark*</include>
</includes>
<systemPropertyVariables>
<benchmarkReportDir>${project.build.directory}/reports/performance</benchmarkReportDir>
<project.version>${project.version}</project.version>
<git.dirty>${git.dirty}</git.dirty>
<git.commit.id>${git.commit.id}</git.commit.id>
<git.branch>${git.branch}</git.branch>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

2
spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterBenchmark.java

@ -126,7 +126,7 @@ public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
} }
@Benchmark // DATAMONGO-1720 @Benchmark // DATAMONGO-1720
public Customer readObjectWith2tPropertiesAnd1NestedObject() { public Customer readObjectWith2PropertiesAnd1NestedObject() {
return converter.read(Customer.class, documentWith2PropertiesAnd1Nested); return converter.read(Customer.class, documentWith2PropertiesAnd1Nested);
} }

39
spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/HttpResultsWriter.java

@ -15,7 +15,10 @@
*/ */
package org.springframework.data.mongodb.microbenchmark; package org.springframework.data.mongodb.microbenchmark;
import lombok.SneakyThrows;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -23,6 +26,7 @@ import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import org.openjdk.jmh.results.RunResult; import org.openjdk.jmh.results.RunResult;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
@ -39,24 +43,39 @@ class HttpResultsWriter implements ResultsWriter {
} }
@Override @Override
@SneakyThrows
public void write(Collection<RunResult> results) { public void write(Collection<RunResult> results) {
if (CollectionUtils.isEmpty(results)) { if (CollectionUtils.isEmpty(results)) {
return; return;
} }
try { StandardEnvironment env = new StandardEnvironment();
String projectVersion = env.getProperty("project.version", "unknown");
String gitBranch = env.getProperty("git.branch", "unknown");
String gitDirty = env.getProperty("git.dirty", "no");
String gitCommitId = env.getProperty("git.commit.id", "unknown");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout((int) Duration.ofSeconds(1).toMillis());
connection.setReadTimeout((int) Duration.ofSeconds(1).toMillis());
connection.setDoOutput(true);
connection.setRequestMethod("POST");
URLConnection connection = new URL(url).openConnection(); connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout((int) Duration.ofSeconds(1).toMillis()); connection.addRequestProperty("X-Project-Version", projectVersion);
connection.setDoOutput(true); connection.addRequestProperty("X-Git-Branch", gitBranch);
connection.setRequestProperty("Content-Type", "application/json"); connection.addRequestProperty("X-Git-Dirty", gitDirty);
connection.addRequestProperty("X-Git-Commit-Id", gitCommitId);
try (OutputStream output = connection.getOutputStream()) {
output.write(ResultsWriter.jsonifyResults(results).getBytes(StandardCharsets.UTF_8));
}
try (OutputStream output = connection.getOutputStream()) { if (connection.getResponseCode() >= 400) {
output.write(ResultsWriter.jsonifyResults(results).getBytes(StandardCharsets.UTF_8)); throw new IllegalStateException(
} String.format("Status %d %s", connection.getResponseCode(), connection.getResponseMessage()));
} catch (Exception e) {
throw new RuntimeException(e);
} }
} }
} }

11
spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/MongoResultsWriter.java

@ -52,6 +52,9 @@ class MongoResultsWriter implements ResultsWriter {
StandardEnvironment env = new StandardEnvironment(); StandardEnvironment env = new StandardEnvironment();
String projectVersion = env.getProperty("project.version", "unknown"); String projectVersion = env.getProperty("project.version", "unknown");
String gitBranch = env.getProperty("git.branch", "unknown");
String gitDirty = env.getProperty("git.dirty", "no");
String gitCommitId = env.getProperty("git.commit.id", "unknown");
MongoClientURI uri = new MongoClientURI(this.uri); MongoClientURI uri = new MongoClientURI(this.uri);
MongoClient client = new MongoClient(uri); MongoClient client = new MongoClient(uri);
@ -65,6 +68,9 @@ class MongoResultsWriter implements ResultsWriter {
Document sink = new Document(); Document sink = new Document();
sink.append("_version", projectVersion); sink.append("_version", projectVersion);
sink.append("_branch", gitBranch);
sink.append("_commit", gitCommitId);
sink.append("_dirty", gitDirty);
sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString())); sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString()));
sink.append("_date", now); sink.append("_date", now);
sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot")); sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot"));
@ -75,7 +81,6 @@ class MongoResultsWriter implements ResultsWriter {
} }
client.close(); client.close();
} }
/** /**
@ -113,13 +118,13 @@ class MongoResultsWriter implements ResultsWriter {
return sanitized; return sanitized;
} }
private String extractClass(String source) { private static String extractClass(String source) {
String tmp = source.substring(0, source.lastIndexOf('.')); String tmp = source.substring(0, source.lastIndexOf('.'));
return tmp.substring(tmp.lastIndexOf(".") + 1); return tmp.substring(tmp.lastIndexOf(".") + 1);
} }
private String extractBenchmarkName(String source) { private static String extractBenchmarkName(String source) {
return source.substring(source.lastIndexOf(".") + 1); return source.substring(source.lastIndexOf(".") + 1);
} }

6
spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/microbenchmark/ResultsWriter.java

@ -15,6 +15,8 @@
*/ */
package org.springframework.data.mongodb.microbenchmark; package org.springframework.data.mongodb.microbenchmark;
import lombok.SneakyThrows;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -54,10 +56,12 @@ interface ResultsWriter {
* @return json string representation of results. * @return json string representation of results.
* @see org.openjdk.jmh.results.format.JSONResultFormat * @see org.openjdk.jmh.results.format.JSONResultFormat
*/ */
@SneakyThrows
static String jsonifyResults(Collection<RunResult> results) { static String jsonifyResults(Collection<RunResult> results) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ResultFormatFactory.getInstance(ResultFormatType.JSON, new PrintStream(baos)).writeOut(results); ResultFormatFactory.getInstance(ResultFormatType.JSON, new PrintStream(baos, true, "UTF-8")).writeOut(results);
return new String(baos.toByteArray(), StandardCharsets.UTF_8); return new String(baos.toByteArray(), StandardCharsets.UTF_8);
} }
} }

Loading…
Cancel
Save