Created
October 12, 2018 11:46
-
-
Save JorgenRingen/2b1fda21030eae34e2ab529b7e3b9f89 to your computer and use it in GitHub Desktop.
Timing-extension to measure test performance.
Include before SpringExtension to measure test
including instantiation of applicationcontext.
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
public class TimingExtension implements AfterAllCallback, TestInstancePostProcessor { | |
private static final Logger logger = Logger.getLogger(TimingExtension.class.getName()); | |
private static final String START_TIME = "start time"; | |
@Override | |
public void postProcessTestInstance(Object testInstance, ExtensionContext context) { | |
getStore(context).put(START_TIME, System.currentTimeMillis()); | |
} | |
@Override | |
public void afterAll(ExtensionContext context) { | |
Class<?> testClass = context.getRequiredTestClass(); | |
long startTime = getStore(context).remove(START_TIME, long.class); | |
long duration = System.currentTimeMillis() - startTime; | |
logger.info(() -> String.format("%s=%sms.", getClassName(testClass), Duration.ofMillis(duration).toString())); | |
} | |
private String getClassName(Class<?> testClass) { | |
if (testClass.getEnclosingClass() != null) { | |
return testClass.getEnclosingClass().getSimpleName() + "#" + testClass.getSimpleName(); | |
} else { | |
return testClass.getSimpleName(); | |
} | |
} | |
private Store getStore(ExtensionContext context) { | |
return context.getStore(Namespace.create(getClass(), context.getRequiredTestClass())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment