#79 - Tweaked benchmark to use API to avoid property population.

We now use PersistentEntity.requiresPropertyPopulation() (via Spring Data Commons' DATACMNS-1366) to eagerly return the initially created instance and delaying all setup and execution of property population until really necessary. We also avoid the creation of a ParameterValueProvider in case the entity exposes a default constructor.
This commit is contained in:
Oliver Gierke
2018-08-08 13:20:11 +02:00
parent 02a8562d94
commit 87cbcbb989

View File

@@ -34,6 +34,7 @@ import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.PreferredConstructor.Parameter; import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.context.AbstractMappingContext; import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
@@ -59,6 +60,17 @@ public class TypicalEntityReaderBenchmark extends AbstractMicrobenchmark {
private final ConversionService conversionService = DefaultConversionService.getSharedInstance(); private final ConversionService conversionService = DefaultConversionService.getSharedInstance();
private final CustomConversions customConversions = new CustomConversions(StoreConversions.NONE, private final CustomConversions customConversions = new CustomConversions(StoreConversions.NONE,
Collections.emptyList()); Collections.emptyList());
private final ParameterValueProvider<MyPersistentProperty> NONE = new ParameterValueProvider<TypicalEntityReaderBenchmark.MyPersistentProperty>() {
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/
@Override
public <T> T getParameterValue(Parameter<T, MyPersistentProperty> parameter) {
return null;
}
};
private final Map<String, Object> simpleEntityData = new HashMap<>(); private final Map<String, Object> simpleEntityData = new HashMap<>();
@@ -178,6 +190,24 @@ public class TypicalEntityReaderBenchmark extends AbstractMicrobenchmark {
} }
MyPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(classToRead); MyPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(classToRead);
PreferredConstructor<?, MyPersistentProperty> constructor = persistentEntity.getPersistenceConstructor();
ParameterValueProvider<MyPersistentProperty> provider = constructor.isNoArgConstructor() //
? NONE //
: new ParameterValueProvider<MyPersistentProperty>() {
@Override
public <T> T getParameterValue(Parameter<T, MyPersistentProperty> parameter) {
return (T) getValue(data, parameter.getName(), parameter.getType().getType(), queryCustomConversions);
}
};
EntityInstantiator instantiator = instantiators.getInstantiatorFor(persistentEntity);
Object instance = instantiator.createInstance(persistentEntity, provider);
if (!persistentEntity.requiresPropertyPopulation()) {
return instance;
}
PropertyValueProvider<MyPersistentProperty> valueProvider = new PropertyValueProvider<MyPersistentProperty>() { PropertyValueProvider<MyPersistentProperty> valueProvider = new PropertyValueProvider<MyPersistentProperty>() {
@@ -187,16 +217,6 @@ public class TypicalEntityReaderBenchmark extends AbstractMicrobenchmark {
} }
}; };
ParameterValueProvider<MyPersistentProperty> provider = new ParameterValueProvider<MyPersistentProperty>() {
@Override
public <T> T getParameterValue(Parameter<T, MyPersistentProperty> parameter) {
return (T) getValue(data, parameter.getName(), parameter.getType().getType(), queryCustomConversions);
}
};
EntityInstantiator instantiator = instantiators.getInstantiatorFor(persistentEntity);
Object instance = instantiator.createInstance(persistentEntity, provider);
PersistentPropertyAccessor<?> accessor = new ConvertingPropertyAccessor<>( PersistentPropertyAccessor<?> accessor = new ConvertingPropertyAccessor<>(
persistentEntity.getPropertyAccessor(instance), conversionService); persistentEntity.getPropertyAccessor(instance), conversionService);