Fix benchmarks.

Original Pull Request: #4819
This commit is contained in:
Mark Paluch
2024-10-24 11:33:54 +02:00
committed by Christoph Strobl
parent 5b1b0a4668
commit 5419414734
7 changed files with 163 additions and 52 deletions

View File

@@ -19,6 +19,7 @@ import org.bson.Document;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.TearDown;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
@@ -27,8 +28,8 @@ import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
/**
@@ -56,7 +57,7 @@ public class ProjectionsBenchmark extends AbstractMicrobenchmark {
@Setup
public void setUp() {
client = new MongoClient(new ServerAddress());
client = MongoClients.create();
template = new MongoTemplate(client, DB_NAME);
source = new Person();
@@ -83,7 +84,7 @@ public class ProjectionsBenchmark extends AbstractMicrobenchmark {
@TearDown
public void tearDown() {
client.dropDatabase(DB_NAME);
client.getDatabase(DB_NAME).drop();
client.close();
}

View File

@@ -18,8 +18,6 @@ package org.springframework.data.mongodb.core.convert;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@@ -29,14 +27,15 @@ import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
/**
* @author Christoph Strobl
@@ -55,7 +54,7 @@ public class DbRefMappingBenchmark extends AbstractMicrobenchmark {
@Setup
public void setUp() throws Exception {
client = new MongoClient(new ServerAddress());
client = MongoClients.create();
template = new MongoTemplate(client, DB_NAME);
List<RefObject> refObjects = new ArrayList<>();
@@ -80,7 +79,7 @@ public class DbRefMappingBenchmark extends AbstractMicrobenchmark {
@TearDown
public void tearDown() {
client.dropDatabase(DB_NAME);
client.getDatabase(DB_NAME).drop();
client.close();
}
@@ -94,18 +93,56 @@ public class DbRefMappingBenchmark extends AbstractMicrobenchmark {
return template.findOne(queryObjectWithDBRefList, ObjectWithDBRef.class);
}
@Data
static class ObjectWithDBRef {
private @Id ObjectId id;
private @DBRef RefObject ref;
private @DBRef List<RefObject> refList;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
public RefObject getRef() {
return ref;
}
public void setRef(RefObject ref) {
this.ref = ref;
}
public List<RefObject> getRefList() {
return refList;
}
public void setRefList(List<RefObject> refList) {
this.refList = refList;
}
}
@Data
static class RefObject {
private @Id String id;
private String someValue;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSomeValue() {
return someValue;
}
public void setSomeValue(String someValue) {
this.someValue = someValue;
}
}
}

View File

@@ -15,11 +15,6 @@
*/
package org.springframework.data.mongodb.core.convert;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
@@ -29,25 +24,29 @@ import java.util.UUID;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.platform.commons.annotation.Testable;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.springframework.data.annotation.Id;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
import org.springframework.util.ObjectUtils;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
/**
* @author Christoph Strobl
*/
@State(Scope.Benchmark)
@Testable
public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
private static final String DB_NAME = "mapping-mongo-converter-benchmark";
@@ -64,13 +63,13 @@ public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
@Setup
public void setUp() throws Exception {
client = new MongoClient(new ServerAddress());
client = MongoClients.create();
this.mappingContext = new MongoMappingContext();
this.mappingContext.setInitialEntitySet(Collections.singleton(Customer.class));
this.mappingContext.afterPropertiesSet();
DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoDbFactory(client, DB_NAME));
DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoClientDatabaseFactory(client, DB_NAME));
this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
this.converter.setCustomConversions(new MongoCustomConversions(Collections.emptyList()));
@@ -116,7 +115,7 @@ public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
@TearDown
public void tearDown() {
client.dropDatabase(DB_NAME);
client.getDatabase(DB_NAME).drop();
client.close();
}
@@ -151,22 +150,36 @@ public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
return sink;
}
@Getter
@RequiredArgsConstructor
static class Customer {
private @Id ObjectId id;
private final String firstname, lastname;
private final Address address;
public Customer(String firstname, String lastname, Address address) {
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
}
}
@Getter
@AllArgsConstructor
static class Address {
private String zipCode, city;
public Address(String zipCode, String city) {
this.zipCode = zipCode;
this.city = city;
}
public String getZipCode() {
return zipCode;
}
public String getCity() {
return city;
}
}
@Data
static class SlightlyMoreComplexObject {
@Id String id;
@@ -177,5 +190,59 @@ public class MappingMongoConverterBenchmark extends AbstractMicrobenchmark {
Customer customer;
List<Address> addressList;
Map<String, Customer> customerMap;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SlightlyMoreComplexObject)) {
return false;
}
SlightlyMoreComplexObject that = (SlightlyMoreComplexObject) o;
if (intOne != that.intOne) {
return false;
}
if (intTwo != that.intTwo) {
return false;
}
if (!ObjectUtils.nullSafeEquals(id, that.id)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(stringOne, that.stringOne)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(stringTwo, that.stringTwo)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(renamedField, that.renamedField)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(location, that.location)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(customer, that.customer)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(addressList, that.addressList)) {
return false;
}
return ObjectUtils.nullSafeEquals(customerMap, that.customerMap);
}
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(id);
result = 31 * result + intOne;
result = 31 * result + intTwo;
result = 31 * result + ObjectUtils.nullSafeHashCode(stringOne);
result = 31 * result + ObjectUtils.nullSafeHashCode(stringTwo);
result = 31 * result + ObjectUtils.nullSafeHashCode(renamedField);
result = 31 * result + ObjectUtils.nullSafeHashCode(location);
result = 31 * result + ObjectUtils.nullSafeHashCode(customer);
result = 31 * result + ObjectUtils.nullSafeHashCode(addressList);
result = 31 * result + ObjectUtils.nullSafeHashCode(customerMap);
return result;
}
}
}

View File

@@ -21,7 +21,6 @@ import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import org.junit.Test;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Scope;
@@ -33,6 +32,7 @@ import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
@@ -41,15 +41,15 @@ import org.springframework.util.StringUtils;
/**
* @author Christoph Strobl
*/
@Warmup(iterations = AbstractMicrobenchmark.WARMUP_ITERATIONS)
@Measurement(iterations = AbstractMicrobenchmark.MEASUREMENT_ITERATIONS)
@Warmup(iterations = AbstractMicrobenchmark.WARMUP_ITERATIONS, time = 2)
@Measurement(iterations = AbstractMicrobenchmark.MEASUREMENT_ITERATIONS, time = 2)
@Fork(AbstractMicrobenchmark.FORKS)
@State(Scope.Thread)
public class AbstractMicrobenchmark {
static final int WARMUP_ITERATIONS = 5;
static final int MEASUREMENT_ITERATIONS = 10;
static final int FORKS = 1;
static final int FORKS = 0;
static final String[] JVM_ARGS = { "-server", "-XX:+HeapDumpOnOutOfMemoryError", "-Xms1024m", "-Xmx1024m",
"-XX:MaxDirectMemorySize=1024m" };
@@ -62,7 +62,6 @@ public class AbstractMicrobenchmark {
* @throws Exception
* @see #options(String)
*/
@Test
public void run() throws Exception {
String includes = includes();

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.data.mongodb.microbenchmark;
import lombok.SneakyThrows;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -43,13 +43,20 @@ class HttpResultsWriter implements ResultsWriter {
}
@Override
@SneakyThrows
public void write(Collection<RunResult> results) {
if (CollectionUtils.isEmpty(results)) {
return;
}
try {
doWrite(results);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void doWrite(Collection<RunResult> results) throws IOException {
StandardEnvironment env = new StandardEnvironment();
String projectVersion = env.getProperty("project.version", "unknown");

View File

@@ -21,15 +21,16 @@ import java.util.List;
import org.bson.Document;
import org.openjdk.jmh.results.RunResult;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON;
/**
* MongoDB specific {@link ResultsWriter} implementation.
@@ -56,13 +57,14 @@ class MongoResultsWriter implements ResultsWriter {
String gitDirty = env.getProperty("git.dirty", "no");
String gitCommitId = env.getProperty("git.commit.id", "unknown");
MongoClientURI uri = new MongoClientURI(this.uri);
MongoClient client = new MongoClient(uri);
ConnectionString connectionString = new ConnectionString(this.uri);
MongoClient client = MongoClients.create(this.uri);
String dbName = StringUtils.hasText(uri.getDatabase()) ? uri.getDatabase() : "spring-data-mongodb-benchmarks";
String dbName = StringUtils.hasText(connectionString.getDatabase()) ? connectionString.getDatabase()
: "spring-data-mongodb-benchmarks";
MongoDatabase db = client.getDatabase(dbName);
for (BasicDBObject dbo : (List<BasicDBObject>) JSON.parse(ResultsWriter.jsonifyResults(results))) {
for (Document dbo : (List<Document>) Document.parse(ResultsWriter.jsonifyResults(results))) {
String collectionName = extractClass(dbo.get("benchmark").toString());
@@ -96,14 +98,15 @@ class MongoResultsWriter implements ResultsWriter {
for (Object key : doc.keySet()) {
Object value = doc.get(key);
if (value instanceof Document document) {
value = fixDocumentKeys(document);
} else if (value instanceof BasicDBObject basicDBObject) {
value = fixDocumentKeys(new Document(basicDBObject));
if (value instanceof Document) {
value = fixDocumentKeys((Document) value);
} else if (value instanceof BasicDBObject) {
value = fixDocumentKeys(new Document((BasicDBObject) value));
}
if (key instanceof String newKey) {
if (key instanceof String) {
String newKey = (String) key;
if (newKey.contains(".")) {
newKey = newKey.replace('.', ',');
}

View File

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