summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/parser/SmartMonkeyLogParser.java
blob: c35325c755f7d13a5504ac5f0aa1825083a88bbd (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2013 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.loganalysis.parser;

import com.android.loganalysis.item.SmartMonkeyLogItem;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A {@link IParser} to parse monkey logs.
 */
public class SmartMonkeyLogParser implements IParser {

    private static final String TIME_STAMP_GROUP =
            "^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}): ";
    private static final String INVOKE_NUM_GROUP = "\\[.*?(\\d+)\\]";
    private static final String SEQ_NUM_GROUP = "\\(Seq:.*?(\\d+)\\)";

    private static final Pattern START_TIME = Pattern.compile(
            TIME_STAMP_GROUP + "Starting.*");

    private static final Pattern START_UPTIME = Pattern.compile(
            TIME_STAMP_GROUP + "Device uptime: (\\d+) sec$");

    private static final Pattern STOP_UPTIME = Pattern.compile(
            TIME_STAMP_GROUP + "Device uptime: (\\d+) sec, Monkey run duration: (\\d+) sec$");

    private static final Pattern THROTTLE = Pattern.compile(
            TIME_STAMP_GROUP + "Throttle: (\\d+).*");

    private static final Pattern TARGET_INVOCATIONS = Pattern.compile(
            TIME_STAMP_GROUP + "Target invocation count: (\\d+)");

    private static final Pattern INTERMEDIATE_COUNT = Pattern.compile(
            TIME_STAMP_GROUP + INVOKE_NUM_GROUP + SEQ_NUM_GROUP + ".*");

    private static final Pattern INTERMEDIATE_TIME = Pattern.compile(TIME_STAMP_GROUP + ".*");

    private static final Pattern FINISHED = Pattern.compile(
            TIME_STAMP_GROUP + "Monkey finished");

    private static final Pattern FINAL_COUNT = Pattern.compile(
            TIME_STAMP_GROUP + "Invocations completed: (\\d+)");

    private static final Pattern APPS_PACKAGES = Pattern.compile(
            TIME_STAMP_GROUP + "Starting \\[(.*)\\]\\[(.*)\\]");

    private static final Pattern ABORTED = Pattern.compile(
            TIME_STAMP_GROUP + "Monkey aborted.");

    private static final Pattern UI_ANR = Pattern.compile(
            TIME_STAMP_GROUP + INVOKE_NUM_GROUP + SEQ_NUM_GROUP + "-UI Exception: ANR: (.*)");

    private static final Pattern UI_CRASH = Pattern.compile(
            TIME_STAMP_GROUP + INVOKE_NUM_GROUP + SEQ_NUM_GROUP + "-UI Exception: CRASH: (.*)");

    private final SmartMonkeyLogItem mSmartMonkeyLog = new SmartMonkeyLogItem();

    /**
     * Parse a monkey log from a {@link BufferedReader} into an {@link SmartMonkeyLogItem}
     * object.
     *
     * @param input a {@link BufferedReader}.
     * @return The {@link SmartMonkeyLogItem}.
     * @see #parse(List)
     */
    public SmartMonkeyLogItem parse(BufferedReader input) throws IOException {
        String line;
        while ((line = input.readLine()) != null) {
            parseLine(line);
        }
        return mSmartMonkeyLog;
    }

    /**
     * {@inheritDoc}
     *
     * @return The {@link SmartMonkeyLogItem}.
     */
    @Override
    public SmartMonkeyLogItem parse(List<String> lines) {
        for (String line : lines) {
            parseLine(line);
        }

        if (mSmartMonkeyLog.getStopUptimeDuration() == 0)
            mSmartMonkeyLog.setIsFinished(false);
        else
            mSmartMonkeyLog.setIsFinished(true);

        return mSmartMonkeyLog;
    }

    /**
     * Parse a line of input.
     */
    private void parseLine(String line) {
        Matcher m = THROTTLE.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setThrottle(Integer.parseInt(m.group(2)));
        }
        m = TARGET_INVOCATIONS.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setTargetInvocations(Integer.parseInt(m.group(2)));
        }
        m = APPS_PACKAGES.matcher(line);
        if (m.matches()) {
            String apps = m.group(2);
            String packages = m.group(3);

            String[] appsArray = apps.split("\\|");
            for (String a : appsArray) {
                mSmartMonkeyLog.addApplication(a);
            }

            String[] pkgsArray = packages.split("\\|");
            for (String p : pkgsArray) {
                mSmartMonkeyLog.addPackage(p);
            }
        }
        m = INTERMEDIATE_COUNT.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setIntermediateCount(Integer.parseInt(m.group(2)));
        }
        m = START_TIME.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setStartTime(parseTime(m.group(1)));
        }
        m = START_UPTIME.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setStartUptimeDuration(Long.parseLong(m.group(2)));
        }
        m = STOP_UPTIME.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setStopTime(parseTime(m.group(1)));
            mSmartMonkeyLog.setStopUptimeDuration(Long.parseLong(m.group(2)));
            mSmartMonkeyLog.setTotalDuration(Long.parseLong(m.group(3)));
        }
        m = INTERMEDIATE_TIME.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setIntermediateTime(parseTime(m.group(1)));
        }
        m = FINAL_COUNT.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setFinalCount(Integer.parseInt(m.group(2)));
        }
        m = FINISHED.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setIsFinished(true);
        }
        m = ABORTED.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.setIsAborted(true);
        }
        m = UI_CRASH.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.addCrashTime(parseTime(m.group(1)));
        }
        m = UI_ANR.matcher(line);
        if (m.matches()) {
            mSmartMonkeyLog.addAnrTime(parseTime(m.group(1)));
        }
    }

    /**
     * Parse the timestamp and return a date.
     *
     * @param timeStr The timestamp in the format {@code yyyy-MM-dd HH:mm:ss.SSS}
     * @return The {@link Date}.
     */
    public static Date parseTime(String timeStr) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(timeStr);
        } catch (ParseException e) {
        }
        return null;
    }

}