Skip to content

Commit

Permalink
Update BeanMapperPerformanceLogger
Browse files Browse the repository at this point in the history
- Added BeanMapperPerformanceLogger#runTimed(Supplier, String, Object...), to allow for just-in-time formatting of log messages.
  • Loading branch information
marcus-talbot42 committed Apr 3, 2024
1 parent 1d9335e commit 725c26a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public abstract class AbstractCollectionHandler<C> implements CollectionHandler<C> {

private static final String LOGGING_STRING = "AbstractCollectionHandler#mapItem(BeanMapper, Class, Object) -> BeanMapper#map(Object)";
private static final String LOGGING_STRING = "%s#mapItem(BeanMapper, Class, Object) -> BeanMapper#map(Object)";

private final Class<C> type;
private final DefaultBeanInitializer beanInitializer = new DefaultBeanInitializer();
Expand All @@ -35,13 +35,12 @@ public Object mapItem(
BeanMapper beanMapper,
Class<?> collectionElementClass,
Object source) {
return BeanMapperPerformanceLogger.runTimed(LOGGING_STRING,
() -> beanMapper.wrap()
return BeanMapperPerformanceLogger.runTimed(() -> beanMapper.wrap()
.setTargetClass(collectionElementClass)
.setCollectionClass(null)
.setConverterChoosable(true)
.build()
.map(source));
.map(source), LOGGING_STRING, getClass().getSimpleName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class CollectionConverter implements BeanConverter {

private static final String LOGGING_STRING = "CollectionConverter#convert(BeanMapper, Object, Class, BeanPropertyMatch) -> BeanMapper#map(Object)";
private static final String LOGGING_STRING = "%s#convert(BeanMapper, Object, Class, BeanPropertyMatch) -> BeanMapper#map(Object)";

private final CollectionHandler<?> collectionHandler;

Expand All @@ -27,8 +27,7 @@ public <R, U> U convert(
return targetClass.cast(source);
}

return BeanMapperPerformanceLogger.runTimed(LOGGING_STRING,
() -> beanMapper.wrap()
return BeanMapperPerformanceLogger.runTimed(() -> beanMapper.wrap()
.setCollectionClass(collectionHandler.getType())
.setCollectionUsage(beanPropertyMatch.getCollectionInstructions().getBeanCollectionUsage())
.setPreferredCollectionClass(beanPropertyMatch.getCollectionInstructions().getPreferredCollectionClass().getAnnotationClass())
Expand All @@ -37,7 +36,7 @@ public <R, U> U convert(
.setTarget(beanPropertyMatch.getTargetObject())
.setUseNullValue()
.build()
.map(source));
.map(source), LOGGING_STRING, getClass().getSimpleName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class MapToDynamicClassStrategy extends AbstractMapStrategy {

private static final String LOGGING_STRING = "Recursively calling BeanMapper#map(Object)";
private static final String LOGGING_STRING = "Recursively calling BeanMapper#map(Object), to map source of type %s, to type %s.";

public MapToDynamicClassStrategy(BeanMapper beanMapper, Configuration configuration) {
super(beanMapper, configuration);
Expand Down Expand Up @@ -51,23 +51,21 @@ public <S, T> T downsizeSource(S source, List<String> downsizeSourceFields) {
Class<?> targetClass = getConfiguration().getTargetClass();
Object target = getConfiguration().getTarget();

Object dynSource = BeanMapperPerformanceLogger.runTimed(LOGGING_STRING,
() -> getBeanMapper()
Object dynSource = BeanMapperPerformanceLogger.runTimed(() -> getBeanMapper()
.wrap()
.downsizeSource(null)
.setTarget(target)
.setTargetClass(dynamicClass)
.build()
.map(source));
.map(source), LOGGING_STRING, source.getClass().getSimpleName(), targetClass != null ? targetClass.getSimpleName() : null);

return BeanMapperPerformanceLogger.runTimed(LOGGING_STRING,
() -> getBeanMapper()
return BeanMapperPerformanceLogger.runTimed(() -> getBeanMapper()
.wrap()
.downsizeSource(null)
.setTarget(target)
.setTargetClass(targetClass)
.build()
.map(dynSource));
.map(dynSource), LOGGING_STRING, dynSource.getClass().getSimpleName(), targetClass != null ? targetClass.getSimpleName() : null);
}

public <S, T> T downsizeTarget(S source, List<String> downsizeTargetFields) {
Expand All @@ -76,12 +74,12 @@ public <S, T> T downsizeTarget(S source, List<String> downsizeTargetFields) {
downsizeTargetFields,
getConfiguration().getStrictMappingProperties());
Class<?> collectionClass = getBeanMapper().getConfiguration().getCollectionClass();
return BeanMapperPerformanceLogger.runTimed(LOGGING_STRING, () -> getBeanMapper()
return BeanMapperPerformanceLogger.runTimed(() -> getBeanMapper()
.wrap()
.downsizeTarget(null)
.setCollectionClass(collectionClass)
.setTargetClass(dynamicClass)
.build()
.map(source));
.map(source), LOGGING_STRING, source.getClass().getSimpleName(), dynamicClass.getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public static <T> T runTimed(String taskName, Supplier<T> task) {
return result;
}

public static <T> T runTimed(Supplier<T> task, String unformattedTaskName, Object... messageArguments) {
if (log.isDebugEnabled()) {
String taskName = unformattedTaskName.formatted(messageArguments);
return runTimed(taskName, task);
}
return task.get();
}

private static class Stopwatch {

private final Instant started;
Expand Down

0 comments on commit 725c26a

Please sign in to comment.