Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore performance optimizations #4819

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.0-GH-4818-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -26,10 +26,10 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>3.4.0-SNAPSHOT</springdata.commons>
<springdata.commons>3.4.0-GH-3185-SNAPSHOT</springdata.commons>
<mongo>5.2.0</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
<jmh.version>1.37</jmh.version>
</properties>

<developers>
Expand Down
34 changes: 29 additions & 5 deletions spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.0-GH-4818-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -19,8 +19,16 @@
<properties>
<!-- Skip tests by default; run only if -DskipTests=false is specified or benchmarks profile is activated -->
<skipTests>true</skipTests>
<source.level>17</source.level>
</properties>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>

<dependency>
Expand All @@ -30,10 +38,15 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit}</version>
<scope>compile</scope>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongo}</version>
</dependency>

<dependency>
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
<artifactId>microbenchmark-runner-junit5</artifactId>
<version>0.4.0.RELEASE</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -63,6 +76,17 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessors>
<annotationProcessor>
org.openjdk.jmh.generators.BenchmarkProcessor
</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -27,8 +28,8 @@
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;

/**
Expand Down Expand Up @@ -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();
Expand All @@ -83,7 +84,7 @@ public void setUp() {
@TearDown
public void tearDown() {

client.dropDatabase(DB_NAME);
client.getDatabase(DB_NAME).drop();
client.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
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;

Expand All @@ -29,14 +27,15 @@
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
Expand All @@ -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<>();
Expand All @@ -80,7 +79,7 @@ public void setUp() throws Exception {
@TearDown
public void tearDown() {

client.dropDatabase(DB_NAME);
client.getDatabase(DB_NAME).drop();
client.close();
}

Expand All @@ -94,18 +93,56 @@ public ObjectWithDBRef readMultipleDbRefs() {
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,25 +24,29 @@

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";
Expand All @@ -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()));
Expand Down Expand Up @@ -116,7 +115,7 @@ public void setUp() throws Exception {
@TearDown
public void tearDown() {

client.dropDatabase(DB_NAME);
client.getDatabase(DB_NAME).drop();
client.close();
}

Expand Down Expand Up @@ -151,22 +150,36 @@ public Object writeObjectWithListAndMapsOfComplexType() {
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;
Expand All @@ -177,5 +190,59 @@ static class SlightlyMoreComplexObject {
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;
}
}
}
Loading