Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2011 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 package com.android.loganalysis.parser;
     17 
     18 import com.android.loganalysis.item.AnrItem;
     19 import com.android.loganalysis.item.JavaCrashItem;
     20 import com.android.loganalysis.item.MonkeyLogItem;
     21 import com.android.loganalysis.item.MonkeyLogItem.DroppedCategory;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.io.BufferedReader;
     26 import java.io.FileNotFoundException;
     27 import java.io.FileReader;
     28 import java.io.IOException;
     29 
     30 /**
     31  * Functional tests for {@link MonkeyLogParser}
     32  */
     33 public class MonkeyLogParserFuncTest extends TestCase {
     34     // FIXME: Make monkey log file configurable.
     35     private static final String MONKEY_LOG_PATH = "/tmp/monkey_log.txt";
     36 
     37     /**
     38      * A test that is intended to force Brillopad to parse a monkey log. The purpose of this is to
     39      * assist a developer in checking why a given monkey log file might not be parsed correctly by
     40      * Brillopad.
     41      */
     42     public void testParse() {
     43         BufferedReader monkeyLogReader = null;
     44         try {
     45             monkeyLogReader = new BufferedReader(new FileReader(MONKEY_LOG_PATH));
     46         } catch (FileNotFoundException e) {
     47             fail(String.format("File not found at %s", MONKEY_LOG_PATH));
     48         }
     49         MonkeyLogItem monkeyLog = null;
     50         try {
     51             long start = System.currentTimeMillis();
     52             monkeyLog = new MonkeyLogParser().parse(monkeyLogReader);
     53             long stop = System.currentTimeMillis();
     54             System.out.println(String.format("Monkey log took %d ms to parse.", stop - start));
     55         } catch (IOException e) {
     56             fail(String.format("IOException: %s", e.toString()));
     57         } finally {
     58             if (monkeyLogReader != null) {
     59                 try {
     60                     monkeyLogReader.close();
     61                 } catch (IOException e) {
     62                     // Ignore
     63                 }
     64             }        }
     65 
     66         assertNotNull(monkeyLog);
     67         assertNotNull(monkeyLog.getStartTime());
     68         assertNotNull(monkeyLog.getStopTime());
     69         assertNotNull(monkeyLog.getTargetCount());
     70         assertNotNull(monkeyLog.getThrottle());
     71         assertNotNull(monkeyLog.getSeed());
     72         assertNotNull(monkeyLog.getIgnoreSecurityExceptions());
     73         assertTrue(monkeyLog.getPackages().size() > 0);
     74         assertTrue(monkeyLog.getCategories().size() > 0);
     75         assertNotNull(monkeyLog.getIsFinished());
     76         assertNotNull(monkeyLog.getIntermediateCount());
     77         assertNotNull(monkeyLog.getTotalDuration());
     78         assertNotNull(monkeyLog.getStartUptimeDuration());
     79         assertNotNull(monkeyLog.getStopUptimeDuration());
     80 
     81 
     82         StringBuffer sb = new StringBuffer();
     83         sb.append("Stats for monkey log:\n");
     84         sb.append(String.format("  Start time: %s\n", monkeyLog.getStartTime()));
     85         sb.append(String.format("  Stop time: %s\n", monkeyLog.getStopTime()));
     86         sb.append(String.format("  Parameters: target-count=%d, throttle=%d, seed=%d, " +
     87                 "ignore-security-exceptions=%b\n",
     88                 monkeyLog.getTargetCount(), monkeyLog.getThrottle(), monkeyLog.getSeed(),
     89                 monkeyLog.getIgnoreSecurityExceptions()));
     90         sb.append(String.format("  Packages: %s\n", monkeyLog.getPackages()));
     91         sb.append(String.format("  Categories: %s\n", monkeyLog.getCategories()));
     92         if (monkeyLog.getNoActivities()) {
     93             sb.append("  Status: no-activities=true\n");
     94         } else {
     95             sb.append(String.format("  Status: finished=%b, final-count=%d, " +
     96                     "intermediate-count=%d\n", monkeyLog.getIsFinished(), monkeyLog.getFinalCount(),
     97                     monkeyLog.getIntermediateCount()));
     98 
     99             sb.append("  Dropped events:");
    100             for (DroppedCategory drop : DroppedCategory.values()) {
    101                 sb.append(String.format(" %s=%d,", drop.toString(),
    102                         monkeyLog.getDroppedCount(drop)));
    103             }
    104             sb.deleteCharAt(sb.length()-1);
    105             sb.append("\n");
    106         }
    107         sb.append(String.format("  Run time: duration=%d ms, delta-uptime=%d (%d - %d) ms\n",
    108                 monkeyLog.getTotalDuration(),
    109                 monkeyLog.getStopUptimeDuration() - monkeyLog.getStartUptimeDuration(),
    110                 monkeyLog.getStopUptimeDuration(), monkeyLog.getStartUptimeDuration()));
    111 
    112         if (monkeyLog.getCrash() != null && monkeyLog.getCrash() instanceof AnrItem) {
    113             sb.append(String.format("  Stopped due to ANR\n"));
    114         }
    115         if (monkeyLog.getCrash() != null && monkeyLog.getCrash() instanceof JavaCrashItem) {
    116             sb.append(String.format("  Stopped due to Java crash\n"));
    117         }
    118         System.out.println(sb.toString());
    119     }
    120 }
    121 
    122