Skip to content

Commit 5419414

Browse files
mp911dechristophstrobl
authored andcommitted
Fix benchmarks.
Original Pull Request: #4819
1 parent 5b1b0a4 commit 5419414

File tree

7 files changed

+163
-52
lines changed

7 files changed

+163
-52
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openjdk.jmh.annotations.Benchmark;
2020
import org.openjdk.jmh.annotations.Setup;
2121
import org.openjdk.jmh.annotations.TearDown;
22+
2223
import org.springframework.beans.factory.annotation.Value;
2324
import org.springframework.data.annotation.Id;
2425
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
@@ -27,8 +28,8 @@
2728
import org.springframework.data.mongodb.core.query.BasicQuery;
2829
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
2930

30-
import com.mongodb.MongoClient;
31-
import com.mongodb.ServerAddress;
31+
import com.mongodb.client.MongoClient;
32+
import com.mongodb.client.MongoClients;
3233
import com.mongodb.client.MongoCollection;
3334

3435
/**
@@ -56,7 +57,7 @@ public class ProjectionsBenchmark extends AbstractMicrobenchmark {
5657
@Setup
5758
public void setUp() {
5859

59-
client = new MongoClient(new ServerAddress());
60+
client = MongoClients.create();
6061
template = new MongoTemplate(client, DB_NAME);
6162

6263
source = new Person();
@@ -83,7 +84,7 @@ public void setUp() {
8384
@TearDown
8485
public void tearDown() {
8586

86-
client.dropDatabase(DB_NAME);
87+
client.getDatabase(DB_NAME).drop();
8788
client.close();
8889
}
8990

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static org.springframework.data.mongodb.core.query.Criteria.*;
1919
import static org.springframework.data.mongodb.core.query.Query.*;
2020

21-
import lombok.Data;
22-
2321
import java.util.ArrayList;
2422
import java.util.List;
2523

@@ -29,14 +27,15 @@
2927
import org.openjdk.jmh.annotations.Setup;
3028
import org.openjdk.jmh.annotations.State;
3129
import org.openjdk.jmh.annotations.TearDown;
30+
3231
import org.springframework.data.annotation.Id;
3332
import org.springframework.data.mongodb.core.MongoTemplate;
3433
import org.springframework.data.mongodb.core.mapping.DBRef;
3534
import org.springframework.data.mongodb.core.query.Query;
3635
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
3736

38-
import com.mongodb.MongoClient;
39-
import com.mongodb.ServerAddress;
37+
import com.mongodb.client.MongoClient;
38+
import com.mongodb.client.MongoClients;
4039

4140
/**
4241
* @author Christoph Strobl
@@ -55,7 +54,7 @@ public class DbRefMappingBenchmark extends AbstractMicrobenchmark {
5554
@Setup
5655
public void setUp() throws Exception {
5756

58-
client = new MongoClient(new ServerAddress());
57+
client = MongoClients.create();
5958
template = new MongoTemplate(client, DB_NAME);
6059

6160
List<RefObject> refObjects = new ArrayList<>();
@@ -80,7 +79,7 @@ public void setUp() throws Exception {
8079
@TearDown
8180
public void tearDown() {
8281

83-
client.dropDatabase(DB_NAME);
82+
client.getDatabase(DB_NAME).drop();
8483
client.close();
8584
}
8685

@@ -94,18 +93,56 @@ public ObjectWithDBRef readMultipleDbRefs() {
9493
return template.findOne(queryObjectWithDBRefList, ObjectWithDBRef.class);
9594
}
9695

97-
@Data
9896
static class ObjectWithDBRef {
9997

10098
private @Id ObjectId id;
10199
private @DBRef RefObject ref;
102100
private @DBRef List<RefObject> refList;
101+
102+
public ObjectId getId() {
103+
return id;
104+
}
105+
106+
public void setId(ObjectId id) {
107+
this.id = id;
108+
}
109+
110+
public RefObject getRef() {
111+
return ref;
112+
}
113+
114+
public void setRef(RefObject ref) {
115+
this.ref = ref;
116+
}
117+
118+
public List<RefObject> getRefList() {
119+
return refList;
120+
}
121+
122+
public void setRefList(List<RefObject> refList) {
123+
this.refList = refList;
124+
}
103125
}
104126

105-
@Data
106127
static class RefObject {
107128

108129
private @Id String id;
109130
private String someValue;
131+
132+
public String getId() {
133+
return id;
134+
}
135+
136+
public void setId(String id) {
137+
this.id = id;
138+
}
139+
140+
public String getSomeValue() {
141+
return someValue;
142+
}
143+
144+
public void setSomeValue(String someValue) {
145+
this.someValue = someValue;
146+
}
110147
}
111148
}

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

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
20-
import lombok.Getter;
21-
import lombok.RequiredArgsConstructor;
22-
2318
import java.util.Arrays;
2419
import java.util.Collections;
2520
import java.util.LinkedHashMap;
@@ -29,25 +24,29 @@
2924

3025
import org.bson.Document;
3126
import org.bson.types.ObjectId;
27+
import org.junit.platform.commons.annotation.Testable;
3228
import org.openjdk.jmh.annotations.Benchmark;
3329
import org.openjdk.jmh.annotations.Scope;
3430
import org.openjdk.jmh.annotations.Setup;
3531
import org.openjdk.jmh.annotations.State;
3632
import org.openjdk.jmh.annotations.TearDown;
33+
3734
import org.springframework.data.annotation.Id;
3835
import org.springframework.data.geo.Point;
39-
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
36+
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
4037
import org.springframework.data.mongodb.core.mapping.Field;
4138
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4239
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
40+
import org.springframework.util.ObjectUtils;
4341

44-
import com.mongodb.MongoClient;
45-
import com.mongodb.ServerAddress;
42+
import com.mongodb.client.MongoClient;
43+
import com.mongodb.client.MongoClients;
4644

4745
/**
4846
* @author Christoph Strobl
4947
*/
5048
@State(Scope.Benchmark)
49+
@Testable
5150
public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
5251

5352
private static final String DB_NAME = "mapping-mongo-converter-benchmark";
@@ -64,13 +63,13 @@ public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
6463
@Setup
6564
public void setUp() throws Exception {
6665

67-
client = new MongoClient(new ServerAddress());
66+
client = MongoClients.create();
6867

6968
this.mappingContext = new MongoMappingContext();
7069
this.mappingContext.setInitialEntitySet(Collections.singleton(Customer.class));
7170
this.mappingContext.afterPropertiesSet();
7271

73-
DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoDbFactory(client, DB_NAME));
72+
DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoClientDatabaseFactory(client, DB_NAME));
7473

7574
this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
7675
this.converter.setCustomConversions(new MongoCustomConversions(Collections.emptyList()));
@@ -116,7 +115,7 @@ public void setUp() throws Exception {
116115
@TearDown
117116
public void tearDown() {
118117

119-
client.dropDatabase(DB_NAME);
118+
client.getDatabase(DB_NAME).drop();
120119
client.close();
121120
}
122121

@@ -151,22 +150,36 @@ public Object writeObjectWithListAndMapsOfComplexType() {
151150
return sink;
152151
}
153152

154-
@Getter
155-
@RequiredArgsConstructor
156153
static class Customer {
157154

158155
private @Id ObjectId id;
159156
private final String firstname, lastname;
160157
private final Address address;
158+
159+
public Customer(String firstname, String lastname, Address address) {
160+
this.firstname = firstname;
161+
this.lastname = lastname;
162+
this.address = address;
163+
}
161164
}
162165

163-
@Getter
164-
@AllArgsConstructor
165166
static class Address {
166167
private String zipCode, city;
168+
169+
public Address(String zipCode, String city) {
170+
this.zipCode = zipCode;
171+
this.city = city;
172+
}
173+
174+
public String getZipCode() {
175+
return zipCode;
176+
}
177+
178+
public String getCity() {
179+
return city;
180+
}
167181
}
168182

169-
@Data
170183
static class SlightlyMoreComplexObject {
171184

172185
@Id String id;
@@ -177,5 +190,59 @@ static class SlightlyMoreComplexObject {
177190
Customer customer;
178191
List<Address> addressList;
179192
Map<String, Customer> customerMap;
193+
194+
@Override
195+
public boolean equals(Object o) {
196+
if (this == o) {
197+
return true;
198+
}
199+
if (!(o instanceof SlightlyMoreComplexObject)) {
200+
return false;
201+
}
202+
SlightlyMoreComplexObject that = (SlightlyMoreComplexObject) o;
203+
if (intOne != that.intOne) {
204+
return false;
205+
}
206+
if (intTwo != that.intTwo) {
207+
return false;
208+
}
209+
if (!ObjectUtils.nullSafeEquals(id, that.id)) {
210+
return false;
211+
}
212+
if (!ObjectUtils.nullSafeEquals(stringOne, that.stringOne)) {
213+
return false;
214+
}
215+
if (!ObjectUtils.nullSafeEquals(stringTwo, that.stringTwo)) {
216+
return false;
217+
}
218+
if (!ObjectUtils.nullSafeEquals(renamedField, that.renamedField)) {
219+
return false;
220+
}
221+
if (!ObjectUtils.nullSafeEquals(location, that.location)) {
222+
return false;
223+
}
224+
if (!ObjectUtils.nullSafeEquals(customer, that.customer)) {
225+
return false;
226+
}
227+
if (!ObjectUtils.nullSafeEquals(addressList, that.addressList)) {
228+
return false;
229+
}
230+
return ObjectUtils.nullSafeEquals(customerMap, that.customerMap);
231+
}
232+
233+
@Override
234+
public int hashCode() {
235+
int result = ObjectUtils.nullSafeHashCode(id);
236+
result = 31 * result + intOne;
237+
result = 31 * result + intTwo;
238+
result = 31 * result + ObjectUtils.nullSafeHashCode(stringOne);
239+
result = 31 * result + ObjectUtils.nullSafeHashCode(stringTwo);
240+
result = 31 * result + ObjectUtils.nullSafeHashCode(renamedField);
241+
result = 31 * result + ObjectUtils.nullSafeHashCode(location);
242+
result = 31 * result + ObjectUtils.nullSafeHashCode(customer);
243+
result = 31 * result + ObjectUtils.nullSafeHashCode(addressList);
244+
result = 31 * result + ObjectUtils.nullSafeHashCode(customerMap);
245+
return result;
246+
}
180247
}
181248
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collection;
2222
import java.util.Date;
2323

24-
import org.junit.Test;
2524
import org.openjdk.jmh.annotations.Fork;
2625
import org.openjdk.jmh.annotations.Measurement;
2726
import org.openjdk.jmh.annotations.Scope;
@@ -33,6 +32,7 @@
3332
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
3433
import org.openjdk.jmh.runner.options.OptionsBuilder;
3534
import org.openjdk.jmh.runner.options.TimeValue;
35+
3636
import org.springframework.core.env.StandardEnvironment;
3737
import org.springframework.util.CollectionUtils;
3838
import org.springframework.util.ResourceUtils;
@@ -41,15 +41,15 @@
4141
/**
4242
* @author Christoph Strobl
4343
*/
44-
@Warmup(iterations = AbstractMicrobenchmark.WARMUP_ITERATIONS)
45-
@Measurement(iterations = AbstractMicrobenchmark.MEASUREMENT_ITERATIONS)
44+
@Warmup(iterations = AbstractMicrobenchmark.WARMUP_ITERATIONS, time = 2)
45+
@Measurement(iterations = AbstractMicrobenchmark.MEASUREMENT_ITERATIONS, time = 2)
4646
@Fork(AbstractMicrobenchmark.FORKS)
4747
@State(Scope.Thread)
4848
public class AbstractMicrobenchmark {
4949

5050
static final int WARMUP_ITERATIONS = 5;
5151
static final int MEASUREMENT_ITERATIONS = 10;
52-
static final int FORKS = 1;
52+
static final int FORKS = 0;
5353
static final String[] JVM_ARGS = { "-server", "-XX:+HeapDumpOnOutOfMemoryError", "-Xms1024m", "-Xmx1024m",
5454
"-XX:MaxDirectMemorySize=1024m" };
5555

@@ -62,7 +62,6 @@ public class AbstractMicrobenchmark {
6262
* @throws Exception
6363
* @see #options(String)
6464
*/
65-
@Test
6665
public void run() throws Exception {
6766

6867
String includes = includes();

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

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

18-
import lombok.SneakyThrows;
1918

19+
import java.io.IOException;
2020
import java.io.OutputStream;
2121
import java.net.HttpURLConnection;
2222
import java.net.URL;
@@ -43,13 +43,20 @@ class HttpResultsWriter implements ResultsWriter {
4343
}
4444

4545
@Override
46-
@SneakyThrows
4746
public void write(Collection<RunResult> results) {
4847

4948
if (CollectionUtils.isEmpty(results)) {
5049
return;
5150
}
5251

52+
try {
53+
doWrite(results);
54+
} catch (IOException e) {
55+
throw new RuntimeException(e);
56+
}
57+
}
58+
59+
private void doWrite(Collection<RunResult> results) throws IOException {
5360
StandardEnvironment env = new StandardEnvironment();
5461

5562
String projectVersion = env.getProperty("project.version", "unknown");

0 commit comments

Comments
 (0)