Skip to content

Commit f864024

Browse files
committed
Hacking.
1 parent d151a13 commit f864024

File tree

4 files changed

+310
-0
lines changed

4 files changed

+310
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>spring-data-mongodb-benchmarks</artifactId>
6+
7+
<parent>
8+
<groupId>org.springframework.data</groupId>
9+
<artifactId>spring-data-mongodb-parent</artifactId>
10+
<version>1.7.0.BUILD-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<name>Spring Data MongoDB - Benchmarks</name>
15+
16+
<dependencies>
17+
18+
<!-- Spring Data -->
19+
<dependency>
20+
<groupId>org.springframework.data</groupId>
21+
<artifactId>spring-data-mongodb</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.openjdk.jmh</groupId>
27+
<artifactId>jmh-core</artifactId>
28+
<version>${jmh.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
<version>1.14.6</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.openjdk.jmh</groupId>
38+
<artifactId>jmh-generator-annprocess</artifactId>
39+
<version>${jmh.version}</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
<properties>
46+
<jmh.version>1.4.2</jmh.version>
47+
<javac.target>1.6</javac.target>
48+
<uberjar.name>benchmarks</uberjar.name>
49+
<bundlor.enabled>false</bundlor.enabled>
50+
</properties>
51+
52+
<build>
53+
<plugins>
54+
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<version>3.1</version>
59+
<configuration>
60+
<source>1.8</source>
61+
<target>1.8</target>
62+
</configuration>
63+
</plugin>
64+
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-shade-plugin</artifactId>
68+
<version>2.2</version>
69+
<executions>
70+
<execution>
71+
<phase>package</phase>
72+
<goals>
73+
<goal>shade</goal>
74+
</goals>
75+
<configuration>
76+
<finalName>${uberjar.name}</finalName>
77+
<transformers>
78+
<transformer
79+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
80+
<mainClass>org.openjdk.jmh.Main</mainClass>
81+
</transformer>
82+
</transformers>
83+
<filters>
84+
<filter>
85+
<!-- Shading signed JARs will fail without this. http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar -->
86+
<artifact>*:*</artifact>
87+
<excludes>
88+
<exclude>META-INF/*.SF</exclude>
89+
<exclude>META-INF/*.DSA</exclude>
90+
<exclude>META-INF/*.RSA</exclude>
91+
</excludes>
92+
</filter>
93+
</filters>
94+
</configuration>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2015 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.benchmarks;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Getter;
20+
21+
@Getter
22+
@AllArgsConstructor
23+
public class Address {
24+
25+
private String zipCode, city;
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015 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.benchmarks;
17+
18+
import lombok.Getter;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import org.bson.types.ObjectId;
22+
import org.springframework.data.annotation.Id;
23+
24+
/**
25+
* @author Oliver Gierke
26+
*/
27+
@Getter
28+
@RequiredArgsConstructor
29+
public class Customer {
30+
31+
private @Id ObjectId id;
32+
private final String firstname, lastname;
33+
private final Address address;
34+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright (c) 2014, Oracle America, Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* * Neither the name of Oracle nor the names of its contributors may be used
16+
* to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29+
* THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package org.springframework.data.mongodb.benchmarks;
33+
34+
import java.net.UnknownHostException;
35+
import java.util.Collections;
36+
37+
import org.openjdk.jmh.annotations.Benchmark;
38+
import org.openjdk.jmh.annotations.Scope;
39+
import org.openjdk.jmh.annotations.State;
40+
import org.openjdk.jmh.runner.Runner;
41+
import org.openjdk.jmh.runner.RunnerException;
42+
import org.openjdk.jmh.runner.options.Options;
43+
import org.openjdk.jmh.runner.options.OptionsBuilder;
44+
import org.springframework.data.convert.EntityInstantiator;
45+
import org.springframework.data.convert.EntityInstantiators;
46+
import org.springframework.data.mapping.PreferredConstructor.Parameter;
47+
import org.springframework.data.mapping.model.ParameterValueProvider;
48+
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
49+
import org.springframework.data.mongodb.core.convert.DbRefResolver;
50+
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
51+
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
52+
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
53+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
54+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
55+
56+
import com.mongodb.BasicDBObject;
57+
import com.mongodb.DBObject;
58+
import com.mongodb.MongoClient;
59+
60+
@State(Scope.Benchmark)
61+
public class MyBenchmark {
62+
63+
private final MongoMappingContext mappingContext;
64+
private final MappingMongoConverter converter;
65+
private final DBObject plainSource, sourceWithAddress;
66+
private final Customer withAddress;
67+
68+
public MyBenchmark() {
69+
70+
MongoClient mongo;
71+
72+
try {
73+
mongo = new MongoClient();
74+
} catch (UnknownHostException e) {
75+
throw new RuntimeException(e);
76+
}
77+
78+
this.mappingContext = new MongoMappingContext();
79+
this.mappingContext.setInitialEntitySet(Collections.singleton(Customer.class));
80+
this.mappingContext.afterPropertiesSet();
81+
82+
DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoDbFactory(mongo, "benchmark"));
83+
84+
this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
85+
this.plainSource = new BasicDBObject("firstname", "Dave").append("lastname", "Matthews");
86+
87+
DBObject address = new BasicDBObject("zipCode", "ABCDE").append("city", "Some Place");
88+
89+
this.sourceWithAddress = new BasicDBObject("firstname", "Dave").//
90+
append("lastname", "Matthews").//
91+
append("address", address);
92+
93+
this.withAddress = new Customer("Dave", "Matthews", new Address("zipCode", "City"));
94+
}
95+
96+
@Benchmark
97+
public void readPlainSource() {
98+
converter.read(Customer.class, plainSource);
99+
}
100+
101+
@Benchmark
102+
public void readSourceWithAddress() {
103+
converter.read(Customer.class, sourceWithAddress);
104+
}
105+
106+
@Benchmark
107+
// @Test
108+
public void writeSourceWithAddress() {
109+
converter.write(withAddress, new BasicDBObject());
110+
}
111+
112+
public void foo() {
113+
114+
BasicMongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(Customer.class);
115+
116+
EntityInstantiators instantiators = new EntityInstantiators();
117+
EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
118+
119+
instantiator.createInstance(entity, new ParameterValueProvider<MongoPersistentProperty>() {
120+
121+
@Override
122+
public <T> T getParameterValue(Parameter<T, MongoPersistentProperty> parameter) {
123+
return null;
124+
}
125+
});
126+
}
127+
128+
@Benchmark
129+
public void testMethod() {
130+
131+
DBObject addressSource = (DBObject) sourceWithAddress.get("address");
132+
Address address = new Address(addressSource.get("zipCode").toString(), addressSource.get("city").toString());
133+
134+
foo(new Customer(sourceWithAddress.get("firstname").toString(), sourceWithAddress.get("lastname").toString(),
135+
address));
136+
}
137+
138+
private void foo(Customer customer) {}
139+
140+
public static void main(String[] args) throws RunnerException {
141+
142+
Options opt = new OptionsBuilder().//
143+
include(MyBenchmark.class.getSimpleName()).//
144+
forks(1).//
145+
build();
146+
147+
new Runner(opt).run();
148+
}
149+
}

0 commit comments

Comments
 (0)