From f8640242fc9eda6aad794c1ad441f48fa5f11adf Mon Sep 17 00:00:00 2001 From: Oliver Gierke 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 @@ + + 4.0.0 + + spring-data-mongodb-benchmarks + + + org.springframework.data + spring-data-mongodb-parent + 1.7.0.BUILD-SNAPSHOT + ../pom.xml + + + Spring Data MongoDB - Benchmarks + + + + + + org.springframework.data + spring-data-mongodb + ${project.version} + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.projectlombok + lombok + 1.14.6 + provided + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + + + + + 1.4.2 + 1.6 + benchmarks + false + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.2 + + + package + + shade + + + ${uberjar.name} + + + org.openjdk.jmh.Main + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + 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() { + + @Override + public T getParameterValue(Parameter 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 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 @@ - 1.4.2 + 1.9.3 1.6 benchmarks false 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()); }