use of org.robolectric.util.PerfStatsCollector.Event in project robolectric by robolectric.
the class PerfStatsCollectorTest method shouldMeasureElapsedTimeForEvents.
@Test
public void shouldMeasureElapsedTimeForEvents() throws Exception {
Event firstEvent = collector.startEvent("first event");
fakeClock.delay(20);
firstEvent.finished();
Collection<Metric> metrics = collector.getMetrics();
assertThat(metrics).containsExactly(new Metric("first event", 1, 20, true));
}
use of org.robolectric.util.PerfStatsCollector.Event in project robolectric by robolectric.
the class PerfStatsCollectorTest method shouldMeasureElapsedTimeForRepeatedEvents.
@Test
public void shouldMeasureElapsedTimeForRepeatedEvents() throws Exception {
Event firstEvent = collector.startEvent("repeatable event");
fakeClock.delay(20);
firstEvent.finished();
Event secondEvent = collector.startEvent("repeatable event");
fakeClock.delay(20);
secondEvent.finished();
Event thirdEvent = collector.startEvent("repeatable event");
fakeClock.delay(20);
thirdEvent.finished();
Collection<Metric> metrics = collector.getMetrics();
assertThat(metrics).containsExactly(new Metric("repeatable event", 3, 60, true));
}
use of org.robolectric.util.PerfStatsCollector.Event 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();
}
};
}
Aggregations