From f8640242fc9eda6aad794c1ad441f48fa5f11adf Mon Sep 17 00:00:00 2001
From: Oliver Gierke <ogierke@pivotal.io>
Date: Fri, 13 Mar 2015 09:50:10 +0100
Subject: [PATCH 1/2] Hacking.

---
 spring-data-mongodb-benchmarks/pom.xml        | 101 ++++++++++++
 .../data/mongodb/benchmarks/Address.java      |  26 +++
 .../data/mongodb/benchmarks/Customer.java     |  34 ++++
 .../data/mongodb/benchmarks/MyBenchmark.java  | 149 ++++++++++++++++++
 4 files changed, 310 insertions(+)
 create mode 100644 spring-data-mongodb-benchmarks/pom.xml
 create mode 100644 spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Address.java
 create mode 100644 spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Customer.java
 create mode 100644 spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java

diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml
new file mode 100644
index 0000000000..32ea953f20
--- /dev/null
+++ b/spring-data-mongodb-benchmarks/pom.xml
@@ -0,0 +1,101 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<artifactId>spring-data-mongodb-benchmarks</artifactId>
+
+	<parent>
+		<groupId>org.springframework.data</groupId>
+		<artifactId>spring-data-mongodb-parent</artifactId>
+		<version>1.7.0.BUILD-SNAPSHOT</version>
+		<relativePath>../pom.xml</relativePath>
+	</parent>
+
+	<name>Spring Data MongoDB - Benchmarks</name>
+
+	<dependencies>
+
+		<!-- Spring Data -->
+		<dependency>
+			<groupId>org.springframework.data</groupId>
+			<artifactId>spring-data-mongodb</artifactId>
+			<version>${project.version}</version>
+		</dependency>
+
+		<dependency>
+			<groupId>org.openjdk.jmh</groupId>
+			<artifactId>jmh-core</artifactId>
+			<version>${jmh.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.projectlombok</groupId>
+			<artifactId>lombok</artifactId>
+			<version>1.14.6</version>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.openjdk.jmh</groupId>
+			<artifactId>jmh-generator-annprocess</artifactId>
+			<version>${jmh.version}</version>
+			<scope>provided</scope>
+		</dependency>
+
+	</dependencies>
+
+	<properties>
+		<jmh.version>1.4.2</jmh.version>
+		<javac.target>1.6</javac.target>
+		<uberjar.name>benchmarks</uberjar.name>
+		<bundlor.enabled>false</bundlor.enabled>
+	</properties>
+
+	<build>
+		<plugins>
+
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+				</configuration>
+			</plugin>
+
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-shade-plugin</artifactId>
+				<version>2.2</version>
+				<executions>
+					<execution>
+						<phase>package</phase>
+						<goals>
+							<goal>shade</goal>
+						</goals>
+						<configuration>
+							<finalName>${uberjar.name}</finalName>
+							<transformers>
+								<transformer
+									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+									<mainClass>org.openjdk.jmh.Main</mainClass>
+								</transformer>
+							</transformers>
+							<filters>
+								<filter>
+									<!-- Shading signed JARs will fail without this. http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar -->
+									<artifact>*:*</artifact>
+									<excludes>
+										<exclude>META-INF/*.SF</exclude>
+										<exclude>META-INF/*.DSA</exclude>
+										<exclude>META-INF/*.RSA</exclude>
+									</excludes>
+								</filter>
+							</filters>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+
+</project>
diff --git a/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Address.java b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Address.java
new file mode 100644
index 0000000000..bfc6c1d7a0
--- /dev/null
+++ b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Address.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2015 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.data.mongodb.benchmarks;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+@Getter
+@AllArgsConstructor
+public class Address {
+
+	private String zipCode, city;
+}
diff --git a/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Customer.java b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Customer.java
new file mode 100644
index 0000000000..671ee24298
--- /dev/null
+++ b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/Customer.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2015 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.data.mongodb.benchmarks;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import org.bson.types.ObjectId;
+import org.springframework.data.annotation.Id;
+
+/**
+ * @author Oliver Gierke
+ */
+@Getter
+@RequiredArgsConstructor
+public class Customer {
+
+	private @Id ObjectId id;
+	private final String firstname, lastname;
+	private final Address address;
+}
diff --git a/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java
new file mode 100644
index 0000000000..06bb0d4bed
--- /dev/null
+++ b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2014, Oracle America, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ *  * Neither the name of Oracle nor the names of its contributors may be used
+ *    to endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.springframework.data.mongodb.benchmarks;
+
+import java.net.UnknownHostException;
+import java.util.Collections;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+import org.springframework.data.convert.EntityInstantiator;
+import org.springframework.data.convert.EntityInstantiators;
+import org.springframework.data.mapping.PreferredConstructor.Parameter;
+import org.springframework.data.mapping.model.ParameterValueProvider;
+import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
+import org.springframework.data.mongodb.core.convert.DbRefResolver;
+import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
+import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
+import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
+import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
+import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBObject;
+import com.mongodb.MongoClient;
+
+@State(Scope.Benchmark)
+public class MyBenchmark {
+
+	private final MongoMappingContext mappingContext;
+	private final MappingMongoConverter converter;
+	private final DBObject plainSource, sourceWithAddress;
+	private final Customer withAddress;
+
+	public MyBenchmark() {
+
+		MongoClient mongo;
+
+		try {
+			mongo = new MongoClient();
+		} catch (UnknownHostException e) {
+			throw new RuntimeException(e);
+		}
+
+		this.mappingContext = new MongoMappingContext();
+		this.mappingContext.setInitialEntitySet(Collections.singleton(Customer.class));
+		this.mappingContext.afterPropertiesSet();
+
+		DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoDbFactory(mongo, "benchmark"));
+
+		this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
+		this.plainSource = new BasicDBObject("firstname", "Dave").append("lastname", "Matthews");
+
+		DBObject address = new BasicDBObject("zipCode", "ABCDE").append("city", "Some Place");
+
+		this.sourceWithAddress = new BasicDBObject("firstname", "Dave").//
+				append("lastname", "Matthews").//
+				append("address", address);
+
+		this.withAddress = new Customer("Dave", "Matthews", new Address("zipCode", "City"));
+	}
+
+	@Benchmark
+	public void readPlainSource() {
+		converter.read(Customer.class, plainSource);
+	}
+
+	@Benchmark
+	public void readSourceWithAddress() {
+		converter.read(Customer.class, sourceWithAddress);
+	}
+
+	@Benchmark
+	// @Test
+	public void writeSourceWithAddress() {
+		converter.write(withAddress, new BasicDBObject());
+	}
+
+	public void foo() {
+
+		BasicMongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(Customer.class);
+
+		EntityInstantiators instantiators = new EntityInstantiators();
+		EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
+
+		instantiator.createInstance(entity, new ParameterValueProvider<MongoPersistentProperty>() {
+
+			@Override
+			public <T> T getParameterValue(Parameter<T, MongoPersistentProperty> parameter) {
+				return null;
+			}
+		});
+	}
+
+	@Benchmark
+	public void testMethod() {
+
+		DBObject addressSource = (DBObject) sourceWithAddress.get("address");
+		Address address = new Address(addressSource.get("zipCode").toString(), addressSource.get("city").toString());
+
+		foo(new Customer(sourceWithAddress.get("firstname").toString(), sourceWithAddress.get("lastname").toString(),
+				address));
+	}
+
+	private void foo(Customer customer) {}
+
+	public static void main(String[] args) throws RunnerException {
+
+		Options opt = new OptionsBuilder().//
+				include(MyBenchmark.class.getSimpleName()).//
+				forks(1).//
+				build();
+
+		new Runner(opt).run();
+	}
+}

From ec8565b7e21634b2171daf3ab1747e54f01f15c5 Mon Sep 17 00:00:00 2001
From: Oliver Gierke <ogierke@pivotal.io>
Date: Mon, 25 May 2015 12:22:29 +0200
Subject: [PATCH 2/2] Upgraded to JMH 1.9.3.

---
 spring-data-mongodb-benchmarks/pom.xml                          | 2 +-
 .../springframework/data/mongodb/benchmarks/MyBenchmark.java    | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml
index 32ea953f20..892659ba57 100644
--- a/spring-data-mongodb-benchmarks/pom.xml
+++ b/spring-data-mongodb-benchmarks/pom.xml
@@ -43,7 +43,7 @@
 	</dependencies>
 
 	<properties>
-		<jmh.version>1.4.2</jmh.version>
+		<jmh.version>1.9.3</jmh.version>
 		<javac.target>1.6</javac.target>
 		<uberjar.name>benchmarks</uberjar.name>
 		<bundlor.enabled>false</bundlor.enabled>
diff --git a/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java
index 06bb0d4bed..9e5c9efd13 100644
--- a/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java
+++ b/spring-data-mongodb-benchmarks/src/main/java/org/springframework/data/mongodb/benchmarks/MyBenchmark.java
@@ -104,7 +104,6 @@ public void readSourceWithAddress() {
 	}
 
 	@Benchmark
-	// @Test
 	public void writeSourceWithAddress() {
 		converter.write(withAddress, new BasicDBObject());
 	}