Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f26befd authored by Aurimas Liutikas's avatar Aurimas Liutikas
Browse files

Add a basic ResourcePerfTest.

Moving ResourcesBenchmark.java to the proper android.perftests infrastructure.

Test: ran the benchmark locally.
Change-Id: Ia981274e1e3c167a2a8900498fc40b7a03508a74
parent 3066d1eb
Loading
Loading
Loading
Loading
+107 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -14,19 +14,33 @@
 * limitations under the License.
 */

package android.content.res;
package android.app;

import android.util.AttributeSet;
import android.util.Xml;
import static org.junit.Assert.fail;

import com.android.internal.R;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;
import android.support.test.filters.LargeTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.google.caliper.AfterExperiment;
import com.google.caliper.BeforeExperiment;
import java.io.IOException;

public class ResourcesBenchmark {
/**
 * Benchmarks for {@link android.content.res.Resources}.
 */
@LargeTest
public class ResourcesPerfTest {
    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    private AssetManager mAsset;
    private Resources mRes;
@@ -36,8 +50,8 @@ public class ResourcesBenchmark {
    private int mIntegerId;
    private int mLayoutId;

    @BeforeExperiment
    protected void setUp() {
    @Before
    public void setUp() {
        mAsset = new AssetManager();
        mAsset.addAssetPath("/system/framework/framework-res.apk");
        mRes = new Resources(mAsset, null, null);
@@ -48,38 +62,45 @@ public class ResourcesBenchmark {
        mLayoutId = mRes.getIdentifier("two_line_list_item", "layout", "android");
    }

    @AfterExperiment
    protected void tearDown() {
    @After
    public void tearDown() {
        mAsset.close();
    }

    public void timeGetString(int reps) {
        for (int i = 0; i < reps; i++) {
    @Test
    public void getText() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mRes.getText(mTextId);
        }
    }

    public void timeGetColor(int reps) {
        for (int i = 0; i < reps; i++) {
    @Test
    public void getColor() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mRes.getColor(mColorId, null);
        }
    }

    public void timeGetInteger(int reps) {
        for (int i = 0; i < reps; i++) {
    @Test
    public void getInteger() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mRes.getInteger(mIntegerId);
        }
    }

    public void timeGetLayoutAndTraverse(int reps) throws Exception {
        for (int i = 0; i < reps; i++) {
            final XmlResourceParser parser = mRes.getLayout(mLayoutId);
            try {
    @Test
    public void getLayoutAndTravese() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            try (XmlResourceParser parser = mRes.getLayout(mLayoutId)) {
                while (parser.next() != XmlPullParser.END_DOCUMENT) {
                    // Walk the entire tree
                }
            } finally {
                parser.close();
            } catch (IOException | XmlPullParserException exception) {
                fail("Parsing of the layout failed. Something is really broken");
            }
        }
    }