summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/gallery3d/util/ProfileTest.java
blob: 798b905f571ee0f785de81bbd160858572caba31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (C) 2012 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.util;

import com.android.gallery3d.util.Profile;

import android.os.Environment;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import junit.framework.Assert;
import junit.framework.TestCase;

@SmallTest
public class ProfileTest extends TestCase {
    private static final String TAG = "ProfileTest";
    private static final String TEST_FILE =
            Environment.getExternalStorageDirectory().getPath() + "/test.dat";


    public void testProfile() throws IOException {
        ProfileData p = new ProfileData();
        ParsedProfile q;
        String[] A = {"A"};
        String[] B = {"B"};
        String[] AC = {"A", "C"};
        String[] AD = {"A", "D"};

        // Empty profile
        p.dumpToFile(TEST_FILE);
        q = new ParsedProfile(TEST_FILE);
        assertTrue(q.mEntries.isEmpty());
        assertTrue(q.mSymbols.isEmpty());

        // Only one sample
        p.addSample(A);
        p.dumpToFile(TEST_FILE);
        q = new ParsedProfile(TEST_FILE);
        assertEquals(1, q.mEntries.size());
        assertEquals(1, q.mSymbols.size());
        assertEquals(1, q.mEntries.get(0).sampleCount);

        // Two samples at the same place
        p.addSample(A);
        p.dumpToFile(TEST_FILE);
        q = new ParsedProfile(TEST_FILE);
        assertEquals(1, q.mEntries.size());
        assertEquals(1, q.mSymbols.size());
        assertEquals(2, q.mEntries.get(0).sampleCount);

        // Two samples at the different places
        p.reset();
        p.addSample(A);
        p.addSample(B);
        p.dumpToFile(TEST_FILE);
        q = new ParsedProfile(TEST_FILE);
        assertEquals(2, q.mEntries.size());
        assertEquals(2, q.mSymbols.size());
        assertEquals(1, q.mEntries.get(0).sampleCount);
        assertEquals(1, q.mEntries.get(1).sampleCount);

        // depth > 1
        p.reset();
        p.addSample(AC);
        p.dumpToFile(TEST_FILE);
        q = new ParsedProfile(TEST_FILE);
        assertEquals(1, q.mEntries.size());
        assertEquals(2, q.mSymbols.size());
        assertEquals(1, q.mEntries.get(0).sampleCount);

        // two samples (AC and AD)
        p.addSample(AD);
        p.dumpToFile(TEST_FILE);
        q = new ParsedProfile(TEST_FILE);
        assertEquals(2, q.mEntries.size());
        assertEquals(3, q.mSymbols.size());  // three symbols: A, C, D
        assertEquals(1, q.mEntries.get(0).sampleCount);
        assertEquals(1, q.mEntries.get(0).sampleCount);

        // Remove the test file
        new File(TEST_FILE).delete();
    }
}

class ParsedProfile {
    public class Entry {
        int sampleCount;
        int stackId[];
    }

    ArrayList<Entry> mEntries = new ArrayList<Entry>();
    HashMap<Integer, String> mSymbols = new HashMap<Integer, String>();
    private DataInputStream mIn;
    private byte[] mScratch = new byte[4];  // scratch buffer for readInt

    public ParsedProfile(String filename) throws IOException {
        mIn = new DataInputStream(new FileInputStream(filename));

        Entry entry = parseOneEntry();
        checkIsFirstEntry(entry);

        while (true) {
            entry = parseOneEntry();
            if (entry.sampleCount == 0) {
                checkIsLastEntry(entry);
                break;
            }
            mEntries.add(entry);
        }

        // Read symbol table
        while (true) {
            String line = mIn.readLine();
            if (line == null) break;
            String[] fields = line.split(" +");
            checkIsValidSymbolLine(fields);
            mSymbols.put(Integer.decode(fields[0]), fields[1]);
        }
    }

    private void checkIsFirstEntry(Entry entry) {
        Assert.assertEquals(0, entry.sampleCount);
        Assert.assertEquals(3, entry.stackId.length);
        Assert.assertEquals(1, entry.stackId[0]);
        Assert.assertTrue(entry.stackId[1] > 0);  // sampling period
        Assert.assertEquals(0, entry.stackId[2]);  // padding
    }

    private void checkIsLastEntry(Entry entry) {
        Assert.assertEquals(0, entry.sampleCount);
        Assert.assertEquals(1, entry.stackId.length);
        Assert.assertEquals(0, entry.stackId[0]);
    }

    private void checkIsValidSymbolLine(String[] fields) {
        Assert.assertEquals(2, fields.length);
        Assert.assertTrue(fields[0].startsWith("0x"));
    }

    private Entry parseOneEntry() throws IOException {
        int sampleCount = readInt();
        int depth = readInt();
        Entry e = new Entry();
        e.sampleCount = sampleCount;
        e.stackId = new int[depth];
        for (int i = 0; i < depth; i++) {
            e.stackId[i] = readInt();
        }
        return e;
    }

    private int readInt() throws IOException {
        mIn.read(mScratch, 0, 4);
        return (mScratch[0] & 0xff) |
                ((mScratch[1] & 0xff) << 8) |
                ((mScratch[2] & 0xff) << 16) |
                ((mScratch[3] & 0xff) << 24);
    }
}