Created
August 31, 2015 11:38
-
-
Save nurkiewicz/94f6ad0989bbcf3eb52b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.BeansException; | |
import org.springframework.beans.factory.config.BeanPostProcessor; | |
import org.springframework.stereotype.Component; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
@Component | |
public class ProfilingBeanPostProcessor implements BeanPostProcessor { | |
private static final Logger log = LoggerFactory.getLogger(ProfilingBeanPostProcessor.class); | |
private final Map<String, Instant> beansCreationTime = new ConcurrentHashMap<>(); | |
@Override | |
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
beansCreationTime.put(beanName, Instant.now()); | |
return bean; | |
} | |
@Override | |
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
final Instant endTime = Instant.now(); | |
final Instant startTime = beansCreationTime.get(beanName); | |
final long initializationTimeMillis = Duration.between(startTime, endTime).toMillis(); | |
log.warn("Bean {} created in {}ms", beanName, initializationTimeMillis); | |
return bean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment