Search in sources :

Example 1 with Metric

use of org.robolectric.pluginapi.perf.Metric in project robolectric by robolectric.

the class PerfStatsCollectorTest method shouldRunAndMeasureExceptionThrowingCallable.

@Test
public void shouldRunAndMeasureExceptionThrowingCallable() throws Exception {
    collector.measure("event", () -> {
        fakeClock.delay(10);
        return "return value";
    });
    try {
        collector.measure("event", () -> {
            fakeClock.delay(5);
            throw new RuntimeException("fake");
        });
        fail("should have thrown");
    } catch (RuntimeException e) {
        assertThat(e.getMessage()).isEqualTo("fake");
    }
    Collection<Metric> metrics = collector.getMetrics();
    assertThat(metrics).containsAtLeast(new Metric("event", 1, 10, true), new Metric("event", 1, 5, false));
}
Also used : Metric(org.robolectric.pluginapi.perf.Metric) Test(org.junit.Test)

Example 2 with Metric

use of org.robolectric.pluginapi.perf.Metric 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 3 with Metric

use of org.robolectric.pluginapi.perf.Metric 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 4 with Metric

use of org.robolectric.pluginapi.perf.Metric in project robolectric by robolectric.

the class SandboxTestRunner method reportPerfStats.

private void reportPerfStats(PerfStatsCollector perfStatsCollector) {
    if (perfStatsReporters.isEmpty()) {
        return;
    }
    Metadata metadata = perfStatsCollector.getMetadata();
    Collection<Metric> metrics = perfStatsCollector.getMetrics();
    for (PerfStatsReporter perfStatsReporter : perfStatsReporters) {
        try {
            perfStatsReporter.report(metadata, metrics);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : PerfStatsReporter(org.robolectric.pluginapi.perf.PerfStatsReporter) Metadata(org.robolectric.pluginapi.perf.Metadata) Metric(org.robolectric.pluginapi.perf.Metric)

Example 5 with Metric

use of org.robolectric.pluginapi.perf.Metric in project robolectric by robolectric.

the class RobolectricTestRunnerTest method shouldReportPerfStats.

@Test
public void shouldReportPerfStats() throws Exception {
    List<Metric> metrics = new ArrayList<>();
    PerfStatsReporter reporter = (metadata, metrics1) -> metrics.addAll(metrics1);
    RobolectricTestRunner runner = new SingleSdkRobolectricTestRunner(TestWithTwoMethods.class, RobolectricTestRunner.defaultInjector().bind(PerfStatsReporter[].class, new PerfStatsReporter[] { reporter }).build());
    runner.run(notifier);
    Set<String> metricNames = metrics.stream().map(Metric::getName).collect(toSet());
    assertThat(metricNames).contains("initialization");
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Result(org.junit.runner.Result) SdkCollection(org.robolectric.plugins.SdkCollection) PerfStatsReporter(org.robolectric.pluginapi.perf.PerfStatsReporter) ShadowProvider(org.robolectric.internal.ShadowProvider) ShadowLooper.shadowMainLooper(org.robolectric.shadows.ShadowLooper.shadowMainLooper) Configuration(org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration) Handler(android.os.Handler) Looper(android.os.Looper) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Collectors.toSet(java.util.stream.Collectors.toSet) StubSdk(org.robolectric.plugins.StubSdk) RunListener(org.junit.runner.notification.RunListener) AndroidTestEnvironment(org.robolectric.android.internal.AndroidTestEnvironment) Sdk(org.robolectric.pluginapi.Sdk) Set(java.util.Set) Description(org.junit.runner.Description) RobolectricFrameworkMethod(org.robolectric.RobolectricTestRunner.RobolectricFrameworkMethod) StandardCharsets(java.nio.charset.StandardCharsets) Failure(org.junit.runner.notification.Failure) LazyLoad(org.robolectric.annotation.experimental.LazyApplication.LazyLoad) List(java.util.List) Application(android.app.Application) TestEnvironmentLifecyclePlugin(org.robolectric.pluginapi.TestEnvironmentLifecyclePlugin) FixMethodOrder(org.junit.FixMethodOrder) Implementation(org.robolectric.annotation.Config.Implementation) Mockito.mock(org.mockito.Mockito.mock) TestEnvironmentSpec(org.robolectric.internal.AndroidSandbox.TestEnvironmentSpec) RobolectricTestRunner.defaultInjector(org.robolectric.RobolectricTestRunner.defaultInjector) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) HashMap(java.util.HashMap) FileSystemProvider(java.nio.file.spi.FileSystemProvider) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) JarEntry(java.util.jar.JarEntry) SuppressLint(android.annotation.SuppressLint) ResourcesMode(org.robolectric.internal.ResourcesMode) ResModeStrategy(org.robolectric.RobolectricTestRunner.ResModeStrategy) AndroidManifest(org.robolectric.manifest.AndroidManifest) Named(javax.inject.Named) Build(android.os.Build) DefaultSdkPicker(org.robolectric.plugins.DefaultSdkPicker) JarOutputStream(java.util.jar.JarOutputStream) Nonnull(javax.annotation.Nonnull) Before(org.junit.Before) AssumptionViolatedException(org.junit.AssumptionViolatedException) FrameworkMethod(org.junit.runners.model.FrameworkMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) LazyApplication(org.robolectric.annotation.experimental.LazyApplication) SdkProvider(org.robolectric.pluginapi.SdkProvider) TempDirectory(org.robolectric.util.TempDirectory) TestUtil(org.robolectric.util.TestUtil) Metric(org.robolectric.pluginapi.perf.Metric) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) RunNotifier(org.junit.runner.notification.RunNotifier) PerfStatsReporter(org.robolectric.pluginapi.perf.PerfStatsReporter) ArrayList(java.util.ArrayList) Metric(org.robolectric.pluginapi.perf.Metric) Test(org.junit.Test)

Aggregations

Metric (org.robolectric.pluginapi.perf.Metric)9 Test (org.junit.Test)6 Event (org.robolectric.util.PerfStatsCollector.Event)2 SuppressLint (android.annotation.SuppressLint)1 Application (android.app.Application)1 Build (android.os.Build)1 Handler (android.os.Handler)1 Looper (android.os.Looper)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 FileSystemProvider (java.nio.file.spi.FileSystemProvider)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1