Search in sources :

Example 6 with Metric

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

the class PerfStatsCollectorTest method shouldRunAndMeasureCheckedException.

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

Example 7 with Metric

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

the class PerfStatsCollectorTest method shouldRunAndMeasureSuccessfulCallable.

@Test
public void shouldRunAndMeasureSuccessfulCallable() throws Exception {
    assertThat(collector.measure("event", () -> {
        fakeClock.delay(10);
        return "return value";
    })).isEqualTo("return value");
    Collection<Metric> metrics = collector.getMetrics();
    assertThat(metrics).containsExactly(new Metric("event", 1, 10, true));
}
Also used : Metric(org.robolectric.pluginapi.perf.Metric) Test(org.junit.Test)

Example 8 with Metric

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

the class SimplePerfStatsReporter method finalReport.

@SuppressWarnings("AndroidJdkLibsChecker)")
private synchronized void finalReport() {
    Map<MetricKey, MetricValue> mergedMetrics = new TreeMap<>();
    for (Data perfStatsData : perfStatsData) {
        AndroidMetadata metadata = perfStatsData.metadata.get(AndroidMetadata.class);
        Map<String, String> deviceBootProperties = metadata.getDeviceBootProperties();
        int sdkInt = Integer.parseInt(deviceBootProperties.get("ro.build.version.sdk"));
        String resourcesMode = metadata.getResourcesMode();
        for (Metric metric : perfStatsData.metrics) {
            MetricKey key = new MetricKey(metric.getName(), metric.isSuccess(), sdkInt, resourcesMode);
            MetricValue mergedMetric = mergedMetrics.get(key);
            if (mergedMetric == null) {
                mergedMetric = new MetricValue();
                mergedMetrics.put(key, mergedMetric);
            }
            mergedMetric.report(metric);
        }
    }
    System.out.println("Name\tSDK\tResources\tSuccess\tCount\tMin ms\tMax ms\tAvg ms\tTotal ms");
    for (Entry<MetricKey, MetricValue> entry : mergedMetrics.entrySet()) {
        MetricKey key = entry.getKey();
        MetricValue value = entry.getValue();
        System.out.println(MessageFormat.format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}", key.name, key.sdkLevel, key.resourcesMode, key.success, value.count, (int) (value.minNs / 1000000), (int) (value.maxNs / 1000000), (int) (value.elapsedNs / 1000000 / value.count), (int) (value.elapsedNs / 1000000)));
    }
}
Also used : AndroidMetadata(org.robolectric.AndroidMetadata) Metric(org.robolectric.pluginapi.perf.Metric) TreeMap(java.util.TreeMap)

Example 9 with Metric

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

the class PerfStatsCollector method incrementCount.

public void incrementCount(String eventName) {
    synchronized (PerfStatsCollector.this) {
        MetricKey key = new MetricKey(eventName, true);
        Metric metric = metricMap.get(key);
        if (metric == null) {
            metricMap.put(key, metric = new Metric(key.name, key.success));
        }
        metric.incrementCount();
    }
}
Also used : Metric(org.robolectric.pluginapi.perf.Metric)

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