use of org.robolectric.util.PerfStatsCollector in project robolectric by robolectric.
the class SandboxTestRunner method methodBlock.
@Override
protected Statement methodBlock(final FrameworkMethod method) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
PerfStatsCollector perfStatsCollector = PerfStatsCollector.getInstance();
perfStatsCollector.reset();
perfStatsCollector.setEnabled(!perfStatsReporters.isEmpty());
Event initialization = perfStatsCollector.startEvent("initialization");
final Sandbox sandbox = getSandbox(method);
// Configure sandbox *BEFORE* setting the ClassLoader. This is necessary because
// creating the ShadowMap loads all ShadowProviders via ServiceLoader and this is
// not available once we install the Robolectric class loader.
configureSandbox(sandbox, method);
sandbox.runOnMainThread(() -> {
ClassLoader priorContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(sandbox.getRobolectricClassLoader());
Class bootstrappedTestClass = sandbox.bootstrappedClass(getTestClass().getJavaClass());
HelperTestRunner helperTestRunner = getHelperTestRunner(bootstrappedTestClass);
helperTestRunner.frameworkMethod = method;
final Method bootstrappedMethod;
try {
// noinspection unchecked
bootstrappedMethod = bootstrappedTestClass.getMethod(method.getMethod().getName());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
try {
// Only invoke @BeforeClass once per class
invokeBeforeClass(bootstrappedTestClass, sandbox);
beforeTest(sandbox, method, bootstrappedMethod);
initialization.finished();
Statement statement = helperTestRunner.methodBlock(new FrameworkMethod(bootstrappedMethod));
// todo: this try/finally probably isn't right -- should mimic RunAfters? [xw]
try {
statement.evaluate();
} finally {
afterTest(method, bootstrappedMethod);
}
} catch (Throwable throwable) {
throw Util.sneakyThrow(throwable);
} finally {
Thread.currentThread().setContextClassLoader(priorContextClassLoader);
try {
finallyAfterTest(method);
} catch (Exception e) {
e.printStackTrace();
}
}
});
reportPerfStats(perfStatsCollector);
perfStatsCollector.reset();
}
};
}
use of org.robolectric.util.PerfStatsCollector in project robolectric by robolectric.
the class RobolectricTestRunner method beforeTest.
@Override
protected void beforeTest(Sandbox sandbox, FrameworkMethod method, Method bootstrappedMethod) throws Throwable {
AndroidSandbox androidSandbox = (AndroidSandbox) sandbox;
RobolectricFrameworkMethod roboMethod = (RobolectricFrameworkMethod) method;
PerfStatsCollector perfStatsCollector = PerfStatsCollector.getInstance();
Sdk sdk = roboMethod.getSdk();
perfStatsCollector.putMetadata(AndroidMetadata.class, new AndroidMetadata(ImmutableMap.of("ro.build.version.sdk", "" + sdk.getApiLevel()), roboMethod.resourcesMode.name()));
Logger.lifecycle(roboMethod.getDeclaringClass().getName() + "." + roboMethod.getMethod().getName() + ": sdk=" + sdk.getApiLevel() + "; resources=" + roboMethod.resourcesMode);
if (roboMethod.resourcesMode == ResourcesMode.LEGACY) {
Logger.warn("Legacy resources mode is deprecated; see" + " http://robolectric.org/migrating/#migrating-to-40");
}
roboMethod.setStuff(androidSandbox, androidSandbox.getTestEnvironment());
Class<TestLifecycle> cl = androidSandbox.bootstrappedClass(getTestLifecycleClass());
roboMethod.testLifecycle = ReflectionHelpers.newInstance(cl);
AndroidManifest appManifest = roboMethod.getAppManifest();
roboMethod.getTestEnvironment().setUpApplicationState(bootstrappedMethod, roboMethod.getConfiguration(), appManifest);
roboMethod.testLifecycle.beforeTest(bootstrappedMethod);
}
use of org.robolectric.util.PerfStatsCollector in project robolectric by robolectric.
the class ClassInstrumentor method instrument.
public byte[] instrument(ClassDetails classDetails, InstrumentationConfiguration config, ClassNodeProvider classNodeProvider) {
PerfStatsCollector perfStats = PerfStatsCollector.getInstance();
MutableClass mutableClass = perfStats.measure("analyze class", () -> analyzeClass(classDetails.getClassBytes(), config, classNodeProvider));
byte[] instrumentedBytes = perfStats.measure("instrument class", () -> instrumentToBytes(mutableClass));
recordPackageStats(perfStats, mutableClass);
return instrumentedBytes;
}
Aggregations