Search in sources :

Example 1 with Event

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));
}
Also used : Event(org.robolectric.util.PerfStatsCollector.Event) Metric(org.robolectric.pluginapi.perf.Metric) Test(org.junit.Test)

Example 2 with Event

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));
}
Also used : Event(org.robolectric.util.PerfStatsCollector.Event) Metric(org.robolectric.pluginapi.perf.Metric) Test(org.junit.Test)

Example 3 with Event

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();
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Sandbox(org.robolectric.internal.bytecode.Sandbox) Event(org.robolectric.util.PerfStatsCollector.Event) BeforeClass(org.junit.BeforeClass) TestClass(org.junit.runners.model.TestClass) AfterClass(org.junit.AfterClass) FrameworkMethod(org.junit.runners.model.FrameworkMethod) PerfStatsCollector(org.robolectric.util.PerfStatsCollector)

Aggregations

Event (org.robolectric.util.PerfStatsCollector.Event)3 Test (org.junit.Test)2 Metric (org.robolectric.pluginapi.perf.Metric)2 Method (java.lang.reflect.Method)1 AfterClass (org.junit.AfterClass)1 BeforeClass (org.junit.BeforeClass)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 Statement (org.junit.runners.model.Statement)1 TestClass (org.junit.runners.model.TestClass)1 Sandbox (org.robolectric.internal.bytecode.Sandbox)1 PerfStatsCollector (org.robolectric.util.PerfStatsCollector)1