Skip to content

Commit 82fdbe8

Browse files
committed
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: spring-projects#483.
1 parent 747625b commit 82fdbe8

File tree

5 files changed

+142
-94
lines changed

5 files changed

+142
-94
lines changed
Lines changed: 99 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,111 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
44

5-
<modelVersion>4.0.0</modelVersion>
5+
<modelVersion>4.0.0</modelVersion>
66

7-
<parent>
8-
<groupId>org.springframework.data</groupId>
9-
<artifactId>spring-data-mongodb-parent</artifactId>
10-
<version>2.0.0.BUILD-SNAPSHOT</version>
11-
<relativePath>../pom.xml</relativePath>
12-
</parent>
7+
<parent>
8+
<groupId>org.springframework.data</groupId>
9+
<artifactId>spring-data-mongodb-parent</artifactId>
10+
<version>2.0.0.BUILD-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
1313

14-
<artifactId>spring-data-mongodb-benchmarks</artifactId>
15-
<packaging>jar</packaging>
14+
<artifactId>spring-data-mongodb-benchmarks</artifactId>
15+
<packaging>jar</packaging>
1616

17-
<name>Spring Data MongoDB - Microbenchmarks</name>
17+
<name>Spring Data MongoDB - Microbenchmarks</name>
1818

19-
<properties>
20-
<skipTests>true</skipTests> <!-- Skip tests by default; run only if -DskipTests=false is specified or benchmarks profile is activated -->
21-
<bundlor.enabled>false</bundlor.enabled>
22-
</properties>
19+
<properties>
20+
<!-- Skip tests by default; run only if -DskipTests=false is specified or benchmarks profile is activated -->
21+
<skipTests>true</skipTests>
22+
</properties>
2323

24-
<dependencies>
25-
<dependency>
26-
<groupId>${project.groupId}</groupId>
27-
<artifactId>spring-data-mongodb</artifactId>
28-
<version>${project.version}</version>
29-
</dependency>
30-
<dependency>
31-
<groupId>junit</groupId>
32-
<artifactId>junit</artifactId>
33-
<version>${junit}</version>
34-
<scope>compile</scope>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.openjdk.jmh</groupId>
38-
<artifactId>jmh-core</artifactId>
39-
<version>${jmh.version}</version>
40-
</dependency>
41-
<dependency>
42-
<groupId>org.openjdk.jmh</groupId>
43-
<artifactId>jmh-generator-annprocess</artifactId>
44-
<version>${jmh.version}</version>
45-
<scope>provided</scope>
46-
</dependency>
47-
</dependencies>
24+
<dependencies>
4825

49-
<profiles>
26+
<dependency>
27+
<groupId>${project.groupId}</groupId>
28+
<artifactId>spring-data-mongodb</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
5031

51-
<profile>
52-
<id>benchmarks</id>
53-
<properties>
54-
<skipTests>false</skipTests>
55-
</properties>
56-
</profile>
57-
</profiles>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>${junit}</version>
36+
<scope>compile</scope>
37+
</dependency>
5838

59-
<build>
60-
<plugins>
61-
<plugin>
62-
<artifactId>maven-jar-plugin</artifactId>
63-
<executions>
64-
<execution>
65-
<id>default-jar</id>
66-
<phase>never</phase>
67-
</execution>
68-
</executions>
69-
</plugin>
70-
<plugin>
71-
<artifactId>maven-surefire-plugin</artifactId>
72-
<configuration>
73-
<testSourceDirectory>${project.build.sourceDirectory}</testSourceDirectory>
74-
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
75-
<excludes>
76-
<exclude>**/AbstractMicrobenchmark.java</exclude>
77-
<exclude>**/*$*.class</exclude>
78-
<exclude>**/generated/*.class</exclude>
79-
</excludes>
80-
<includes>
81-
<include>**/*Benchmark*</include>
82-
</includes>
83-
<systemPropertyVariables>
84-
<benchmarkReportDir>${project.build.directory}/reports/performance</benchmarkReportDir>
85-
<project.version>${project.version}</project.version>
86-
</systemPropertyVariables>
87-
</configuration>
88-
</plugin>
89-
</plugins>
90-
</build>
39+
<dependency>
40+
<groupId>org.openjdk.jmh</groupId>
41+
<artifactId>jmh-core</artifactId>
42+
<version>${jmh.version}</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.openjdk.jmh</groupId>
47+
<artifactId>jmh-generator-annprocess</artifactId>
48+
<version>${jmh.version}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
<profiles>
55+
56+
<profile>
57+
<id>benchmarks</id>
58+
<properties>
59+
<skipTests>false</skipTests>
60+
</properties>
61+
</profile>
62+
</profiles>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>pl.project13.maven</groupId>
68+
<artifactId>git-commit-id-plugin</artifactId>
69+
<version>2.2.2</version>
70+
<executions>
71+
<execution>
72+
<goals>
73+
<goal>revision</goal>
74+
</goals>
75+
</execution>
76+
</executions>
77+
</plugin>
78+
<plugin>
79+
<artifactId>maven-jar-plugin</artifactId>
80+
<executions>
81+
<execution>
82+
<id>default-jar</id>
83+
<phase>never</phase>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
<plugin>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<configuration>
90+
<testSourceDirectory>${project.build.sourceDirectory}</testSourceDirectory>
91+
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
92+
<excludes>
93+
<exclude>**/AbstractMicrobenchmark.java</exclude>
94+
<exclude>**/*$*.class</exclude>
95+
<exclude>**/generated/*.class</exclude>
96+
</excludes>
97+
<includes>
98+
<include>**/*Benchmark*</include>
99+
</includes>
100+
<systemPropertyVariables>
101+
<benchmarkReportDir>${project.build.directory}/reports/performance</benchmarkReportDir>
102+
<project.version>${project.version}</project.version>
103+
<git.dirty>${git.dirty}</git.dirty>
104+
<git.commit.id>${git.commit.id}</git.commit.id>
105+
<git.branch>${git.branch}</git.branch>
106+
</systemPropertyVariables>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
91111
</project>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public Customer readObjectWith2Properties() {
126126
}
127127

128128
@Benchmark // DATAMONGO-1720
129-
public Customer readObjectWith2tPropertiesAnd1NestedObject() {
129+
public Customer readObjectWith2PropertiesAnd1NestedObject() {
130130
return converter.read(Customer.class, documentWith2PropertiesAnd1Nested);
131131
}
132132

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
*/
1616
package org.springframework.data.mongodb.microbenchmark;
1717

18+
import lombok.SneakyThrows;
19+
1820
import java.io.OutputStream;
21+
import java.net.HttpURLConnection;
1922
import java.net.URL;
2023
import java.net.URLConnection;
2124
import java.nio.charset.StandardCharsets;
2225
import java.time.Duration;
2326
import java.util.Collection;
2427

2528
import org.openjdk.jmh.results.RunResult;
29+
import org.springframework.core.env.StandardEnvironment;
2630
import org.springframework.util.CollectionUtils;
2731

2832
/**
@@ -39,24 +43,39 @@ class HttpResultsWriter implements ResultsWriter {
3943
}
4044

4145
@Override
46+
@SneakyThrows
4247
public void write(Collection<RunResult> results) {
4348

4449
if (CollectionUtils.isEmpty(results)) {
4550
return;
4651
}
4752

48-
try {
53+
StandardEnvironment env = new StandardEnvironment();
54+
55+
String projectVersion = env.getProperty("project.version", "unknown");
56+
String gitBranch = env.getProperty("git.branch", "unknown");
57+
String gitDirty = env.getProperty("git.dirty", "no");
58+
String gitCommitId = env.getProperty("git.commit.id", "unknown");
59+
60+
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
61+
connection.setConnectTimeout((int) Duration.ofSeconds(1).toMillis());
62+
connection.setReadTimeout((int) Duration.ofSeconds(1).toMillis());
63+
connection.setDoOutput(true);
64+
connection.setRequestMethod("POST");
4965

50-
URLConnection connection = new URL(url).openConnection();
51-
connection.setConnectTimeout((int) Duration.ofSeconds(1).toMillis());
52-
connection.setDoOutput(true);
53-
connection.setRequestProperty("Content-Type", "application/json");
66+
connection.setRequestProperty("Content-Type", "application/json");
67+
connection.addRequestProperty("X-Project-Version", projectVersion);
68+
connection.addRequestProperty("X-Git-Branch", gitBranch);
69+
connection.addRequestProperty("X-Git-Dirty", gitDirty);
70+
connection.addRequestProperty("X-Git-Commit-Id", gitCommitId);
71+
72+
try (OutputStream output = connection.getOutputStream()) {
73+
output.write(ResultsWriter.jsonifyResults(results).getBytes(StandardCharsets.UTF_8));
74+
}
5475

55-
try (OutputStream output = connection.getOutputStream()) {
56-
output.write(ResultsWriter.jsonifyResults(results).getBytes(StandardCharsets.UTF_8));
57-
}
58-
} catch (Exception e) {
59-
throw new RuntimeException(e);
76+
if (connection.getResponseCode() >= 400) {
77+
throw new IllegalStateException(
78+
String.format("Status %d %s", connection.getResponseCode(), connection.getResponseMessage()));
6079
}
6180
}
6281
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public void write(Collection<RunResult> results) {
5252
StandardEnvironment env = new StandardEnvironment();
5353

5454
String projectVersion = env.getProperty("project.version", "unknown");
55+
String gitBranch = env.getProperty("git.branch", "unknown");
56+
String gitDirty = env.getProperty("git.dirty", "no");
57+
String gitCommitId = env.getProperty("git.commit.id", "unknown");
5558

5659
MongoClientURI uri = new MongoClientURI(this.uri);
5760
MongoClient client = new MongoClient(uri);
@@ -65,6 +68,9 @@ public void write(Collection<RunResult> results) {
6568

6669
Document sink = new Document();
6770
sink.append("_version", projectVersion);
71+
sink.append("_branch", gitBranch);
72+
sink.append("_commit", gitCommitId);
73+
sink.append("_dirty", gitDirty);
6874
sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString()));
6975
sink.append("_date", now);
7076
sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot"));
@@ -75,7 +81,6 @@ public void write(Collection<RunResult> results) {
7581
}
7682

7783
client.close();
78-
7984
}
8085

8186
/**
@@ -113,13 +118,13 @@ private Document fixDocumentKeys(Document doc) {
113118
return sanitized;
114119
}
115120

116-
private String extractClass(String source) {
121+
private static String extractClass(String source) {
117122

118123
String tmp = source.substring(0, source.lastIndexOf('.'));
119124
return tmp.substring(tmp.lastIndexOf(".") + 1);
120125
}
121126

122-
private String extractBenchmarkName(String source) {
127+
private static String extractBenchmarkName(String source) {
123128
return source.substring(source.lastIndexOf(".") + 1);
124129
}
125130

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.microbenchmark;
1717

18+
import lombok.SneakyThrows;
19+
1820
import java.io.ByteArrayOutputStream;
1921
import java.io.PrintStream;
2022
import java.nio.charset.StandardCharsets;
@@ -54,10 +56,12 @@ static ResultsWriter forUri(String uri) {
5456
* @return json string representation of results.
5557
* @see org.openjdk.jmh.results.format.JSONResultFormat
5658
*/
59+
@SneakyThrows
5760
static String jsonifyResults(Collection<RunResult> results) {
5861

5962
ByteArrayOutputStream baos = new ByteArrayOutputStream();
60-
ResultFormatFactory.getInstance(ResultFormatType.JSON, new PrintStream(baos)).writeOut(results);
63+
ResultFormatFactory.getInstance(ResultFormatType.JSON, new PrintStream(baos, true, "UTF-8")).writeOut(results);
64+
6165
return new String(baos.toByteArray(), StandardCharsets.UTF_8);
6266
}
6367
}

0 commit comments

Comments
 (0)