|
| 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