• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ProcrankItem;
19 import com.android.loganalysis.util.ArrayUtil;
20 
21 import junit.framework.TestCase;
22 
23 import java.util.Arrays;
24 import java.util.List;
25 
26 /**
27  * Unit tests for {@link ProcrankParser}
28  */
29 public class ProcrankParserTest extends TestCase {
30 
31     /**
32      * Test that normal input is parsed.
33      */
testProcRankParserShortLine()34     public void testProcRankParserShortLine() {
35         List<String> inputBlock = Arrays.asList(
36                 "  PID      Vss      Rss      Pss      Uss  cmdline",
37                 "  178   87136K   81684K   52829K   50012K  system_server",
38                 " 1313   78128K   77996K   48603K   45812K  com.google.android.apps.maps",
39                 " 3247   61652K   61492K   33122K   30972K  com.android.browser",
40                 "  334   55740K   55572K   29629K   28360K  com.android.launcher",
41                 " 2072   51348K   51172K   24263K   22812K  android.process.acore",
42                 " 1236   51440K   51312K   22911K   20608K  com.android.settings",
43                 "                 51312K   22911K   20608K  invalid.format",
44                 "                          ------   ------  ------",
45                 "                          203624K  163604K  TOTAL",
46                 "RAM: 731448K total, 415804K free, 9016K buffers, 108548K cached",
47                 "[procrank: 1.6s elapsed]");
48 
49         ProcrankItem procrank = new ProcrankParser().parse(inputBlock);
50 
51         // Ensures that only valid lines are parsed. Only 6 of the 11 lines under the header are
52         // valid.
53         assertEquals(6, procrank.getPids().size());
54 
55         // Make sure all expected rows are present, and do a diagonal check of values
56         assertEquals((Integer) 87136, procrank.getVss(178));
57         assertEquals((Integer) 77996, procrank.getRss(1313));
58         assertEquals((Integer) 33122, procrank.getPss(3247));
59         assertEquals((Integer) 28360, procrank.getUss(334));
60         assertEquals("android.process.acore", procrank.getProcessName(2072));
61         assertEquals(ArrayUtil.join("\n", inputBlock), procrank.getText());
62     }
63 
64     /**
65      * Test that normal input is parsed.
66      */
testProcRankParserLongLine()67     public void testProcRankParserLongLine() {
68         List<String> inputBlock = Arrays.asList(
69                 "  PID       Vss      Rss      Pss      Uss     Swap    PSwap    USwap    ZSwap  cmdline",
70                 " 6711  3454396K  146300K  108431K  105524K   31540K   20522K   20188K    4546K  com.google.android.GoogleCamera",
71                 " 1515  2535920K  131984K   93750K   89440K   42676K   31792K   31460K    7043K  system_server",
72                 "19906  2439540K  130228K   85418K   69296K   11680K     353K       0K      78K  com.android.chrome:sandboxed_process10",
73                 "13790  2596308K  124424K   75673K   69680K   11336K     334K       0K      74K  com.google.android.youtube",
74                 " 9288  2437704K  119496K   74288K   69532K   11344K     334K       0K      74K  com.google.android.videos",
75                 "                           51312K   22911K   20608K       0K       0K       0K  invalid.format",
76                 "                           ------   ------   ------   ------   ------   ------  ------",
77                 "                         1061237K  940460K  619796K  225468K  201688K   49950K  TOTAL",
78                 "ZRAM: 52892K physical used for 238748K in swap (520908K total swap)",
79                 "RAM: 1857348K total, 51980K free, 3780K buffers, 456272K cached, 29220K shmem, 97560K slab",
80                 "[/system/xbin/su: 3.260s elapsed]");
81 
82         ProcrankItem procrank = new ProcrankParser().parse(inputBlock);
83 
84         // Ensures that only valid lines are parsed. Only 6 of the 11 lines under the header are
85         // valid.
86         assertEquals(5, procrank.getPids().size());
87 
88         // Make sure all expected rows are present, and do a diagonal check of values
89         assertEquals((Integer) 3454396, procrank.getVss(6711));
90         assertEquals((Integer) 146300, procrank.getRss(6711));
91         assertEquals((Integer) 108431, procrank.getPss(6711));
92         assertEquals((Integer) 105524, procrank.getUss(6711));
93         assertEquals("com.google.android.GoogleCamera", procrank.getProcessName(6711));
94         assertEquals(ArrayUtil.join("\n", inputBlock), procrank.getText());
95     }
96 
97     /**
98      * Test that an empty input returns {@code null}.
99      */
testEmptyInput()100     public void testEmptyInput() {
101         ProcrankItem item = new ProcrankParser().parse(Arrays.asList(""));
102         assertNull(item);
103     }
104 }
105 
106