Created
January 15, 2018 09:22
-
-
Save mp911de/72e148b8a3462cdd7dd74e7aa9a314a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 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.mapping.model; | |
import lombok.Data; | |
import java.util.concurrent.TimeUnit; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
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.mapping.Association; | |
import org.springframework.data.mapping.PersistentProperty; | |
import org.springframework.data.mapping.PersistentPropertyAccessor; | |
import org.springframework.data.mapping.context.AbstractMappingContext; | |
import org.springframework.data.util.TypeInformation; | |
/** | |
* @author Mark Paluch | |
*/ | |
@State(Scope.Benchmark) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public class PropertyAccessorFactoryBenchmarks { | |
public static final Person person = new Person(); | |
public static final PersistentPropertyAccessor bytecodeAccessor; | |
public static final PersistentPropertyAccessor reflectionAccessor; | |
public static final PersistentProperty id; | |
public static final PersistentProperty name; | |
static { | |
BasicPersistentEntity<?, SamplePersistentProperty> entity = new SampleMappingContext() | |
.getPersistentEntity(Person.class); | |
ClassGeneratingPropertyAccessorFactory factory = new ClassGeneratingPropertyAccessorFactory(); | |
bytecodeAccessor = factory.getPropertyAccessor(entity, person); | |
reflectionAccessor = BeanWrapperPropertyAccessorFactory.INSTANCE.getPropertyAccessor(entity, person); | |
id = entity.getPersistentProperty("id"); | |
name = entity.getPersistentProperty("name"); | |
} | |
@Benchmark | |
public void setPropertiesGenerated() { | |
bytecodeAccessor.setProperty(id, "foo"); | |
bytecodeAccessor.setProperty(name, "bar"); | |
} | |
@Benchmark | |
public void setPropertiesBeanWrapper() { | |
reflectionAccessor.setProperty(id, "foo"); | |
reflectionAccessor.setProperty(name, "bar"); | |
} | |
public static class SamplePersistentProperty extends AnnotationBasedPersistentProperty<SamplePersistentProperty> { | |
public SamplePersistentProperty(Property property, BasicPersistentEntity<?, SamplePersistentProperty> owner, | |
SimpleTypeHolder simpleTypeHolder) { | |
super(property, owner, simpleTypeHolder); | |
} | |
@Override | |
protected Association<SamplePersistentProperty> createAssociation() { | |
return null; | |
} | |
} | |
public static class SampleMappingContext | |
extends AbstractMappingContext<BasicPersistentEntity<?, SamplePersistentProperty>, SamplePersistentProperty> { | |
@Override | |
protected <T> BasicPersistentEntity<?, SamplePersistentProperty> createPersistentEntity( | |
TypeInformation<T> typeInformation) { | |
return new BasicPersistentEntity<>(typeInformation); | |
} | |
@Override | |
protected SamplePersistentProperty createPersistentProperty(Property property, | |
BasicPersistentEntity<?, SamplePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) { | |
return new SamplePersistentProperty(property, owner, simpleTypeHolder); | |
} | |
} | |
@Data | |
public static class Person { | |
String id; | |
String name; | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options build = new OptionsBuilder().forks(1) | |
// .jvmArgs("-XX:+UnlockExperimentalVMOptions", "-XX:+TrustFinalNonStaticFields") | |
.include(".*PropertyAccessorFactoryBenchmarks.*").build(); | |
new Runner(build).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment