1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.res;
18
19import android.util.AttributeSet;
20import android.util.Xml;
21
22import com.android.internal.R;
23
24import org.xmlpull.v1.XmlPullParser;
25
26import com.google.caliper.AfterExperiment;
27import com.google.caliper.BeforeExperiment;
28
29public class ResourcesBenchmark {
30
31    private AssetManager mAsset;
32    private Resources mRes;
33
34    private int mTextId;
35    private int mColorId;
36    private int mIntegerId;
37    private int mLayoutId;
38
39    @BeforeExperiment
40    protected void setUp() {
41        mAsset = new AssetManager();
42        mAsset.addAssetPath("/system/framework/framework-res.apk");
43        mRes = new Resources(mAsset, null, null);
44
45        mTextId = mRes.getIdentifier("cancel", "string", "android");
46        mColorId = mRes.getIdentifier("transparent", "color", "android");
47        mIntegerId = mRes.getIdentifier("config_shortAnimTime", "integer", "android");
48        mLayoutId = mRes.getIdentifier("two_line_list_item", "layout", "android");
49    }
50
51    @AfterExperiment
52    protected void tearDown() {
53        mAsset.close();
54    }
55
56    public void timeGetString(int reps) {
57        for (int i = 0; i < reps; i++) {
58            mRes.getText(mTextId);
59        }
60    }
61
62    public void timeGetColor(int reps) {
63        for (int i = 0; i < reps; i++) {
64            mRes.getColor(mColorId, null);
65        }
66    }
67
68    public void timeGetInteger(int reps) {
69        for (int i = 0; i < reps; i++) {
70            mRes.getInteger(mIntegerId);
71        }
72    }
73
74    public void timeGetLayoutAndTraverse(int reps) throws Exception {
75        for (int i = 0; i < reps; i++) {
76            final XmlResourceParser parser = mRes.getLayout(mLayoutId);
77            try {
78                while (parser.next() != XmlPullParser.END_DOCUMENT) {
79                    // Walk the entire tree
80                }
81            } finally {
82                parser.close();
83            }
84        }
85    }
86}
87