Skip to content

Commit b4c213b

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1733 - Added benchmark for projections in FluentMongoOperations.
Original pull request: spring-projects#486.
1 parent 2230b51 commit b4c213b

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import org.bson.Document;
19+
import org.openjdk.jmh.annotations.Benchmark;
20+
import org.openjdk.jmh.annotations.Setup;
21+
import org.openjdk.jmh.annotations.TearDown;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.data.annotation.Id;
24+
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindOperationWithQuery;
25+
import org.springframework.data.mongodb.core.ExecutableFindOperation.TerminatingFindOperation;
26+
import org.springframework.data.mongodb.core.mapping.Field;
27+
import org.springframework.data.mongodb.core.query.BasicQuery;
28+
import org.springframework.data.mongodb.microbenchmark.AbstractMicrobenchmark;
29+
30+
import com.mongodb.MongoClient;
31+
import com.mongodb.ServerAddress;
32+
import com.mongodb.client.MongoCollection;
33+
34+
/**
35+
* @author Christoph Strobl
36+
*/
37+
public class ProjectionsBenchmark extends AbstractMicrobenchmark {
38+
39+
private static final String DB_NAME = "projections-benchmark";
40+
private static final String COLLECTION_NAME = "projections";
41+
42+
private MongoTemplate template;
43+
private MongoClient client;
44+
private MongoCollection<Document> mongoCollection;
45+
46+
private Person source;
47+
48+
private FindOperationWithQuery<Person> asPerson;
49+
private FindOperationWithQuery<DtoProjection> asDtoProjection;
50+
private FindOperationWithQuery<ClosedProjection> asClosedProjection;
51+
private FindOperationWithQuery<OpenProjection> asOpenProjection;
52+
53+
private TerminatingFindOperation<Person> asPersonWithFieldsRestriction;
54+
private Document fields = new Document("firstname", 1);
55+
56+
@Setup
57+
public void setUp() {
58+
59+
client = new MongoClient(new ServerAddress());
60+
template = new MongoTemplate(client, DB_NAME);
61+
62+
source = new Person();
63+
source.firstname = "luke";
64+
source.lastname = "skywalker";
65+
66+
source.address = new Address();
67+
source.address.street = "melenium falcon 1";
68+
source.address.city = "deathstar";
69+
70+
template.save(source, COLLECTION_NAME);
71+
72+
asPerson = template.query(Person.class).inCollection(COLLECTION_NAME);
73+
asDtoProjection = template.query(Person.class).inCollection(COLLECTION_NAME).as(DtoProjection.class);
74+
asClosedProjection = template.query(Person.class).inCollection(COLLECTION_NAME).as(ClosedProjection.class);
75+
asOpenProjection = template.query(Person.class).inCollection(COLLECTION_NAME).as(OpenProjection.class);
76+
77+
asPersonWithFieldsRestriction = template.query(Person.class).inCollection(COLLECTION_NAME)
78+
.matching(new BasicQuery(new Document(), fields));
79+
80+
mongoCollection = client.getDatabase(DB_NAME).getCollection(COLLECTION_NAME);
81+
}
82+
83+
@TearDown
84+
public void tearDown() {
85+
86+
client.dropDatabase(DB_NAME);
87+
client.close();
88+
}
89+
90+
/**
91+
* Set the baseline for comparison by using the plain MongoDB java driver api without any additional fluff.
92+
*
93+
* @return
94+
*/
95+
@Benchmark // DATAMONGO-1733
96+
public Object baseline() {
97+
return mongoCollection.find().first();
98+
}
99+
100+
/**
101+
* Read into the domain type including all fields.
102+
*
103+
* @return
104+
*/
105+
@Benchmark // DATAMONGO-1733
106+
public Object readIntoDomainType() {
107+
return asPerson.all();
108+
}
109+
110+
/**
111+
* Read into the domain type but restrict query to only return one field.
112+
*
113+
* @return
114+
*/
115+
@Benchmark // DATAMONGO-1733
116+
public Object readIntoDomainTypeRestrictingToOneField() {
117+
return asPersonWithFieldsRestriction.all();
118+
}
119+
120+
/**
121+
* Read into dto projection that only needs to map one field back.
122+
*
123+
* @return
124+
*/
125+
@Benchmark // DATAMONGO-1733
126+
public Object readIntoDtoProjectionWithOneField() {
127+
return asDtoProjection.all();
128+
}
129+
130+
/**
131+
* Read into closed interface projection.
132+
*
133+
* @return
134+
*/
135+
@Benchmark // DATAMONGO-1733
136+
public Object readIntoClosedProjectionWithOneField() {
137+
return asClosedProjection.all();
138+
}
139+
140+
/**
141+
* Read into an open projection backed by the mapped domain object.
142+
*
143+
* @return
144+
*/
145+
@Benchmark // DATAMONGO-1733
146+
public Object readIntoOpenProjection() {
147+
return asOpenProjection.all();
148+
}
149+
150+
static class Person {
151+
152+
@Id String id;
153+
String firstname;
154+
String lastname;
155+
Address address;
156+
}
157+
158+
static class Address {
159+
160+
String city;
161+
String street;
162+
}
163+
164+
static class DtoProjection {
165+
166+
@Field("firstname") String name;
167+
}
168+
169+
static interface ClosedProjection {
170+
171+
String getFirstname();
172+
}
173+
174+
static interface OpenProjection {
175+
176+
@Value("#{target.firstname}")
177+
String name();
178+
}
179+
180+
}

0 commit comments

Comments
 (0)