Home | History | Annotate | Download | only in recorder
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.android.tradefed.profiler.recorder;
     18 
     19 import org.junit.Assert;
     20 import org.junit.Before;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.junit.runners.JUnit4;
     24 
     25 import java.util.Map;
     26 import java.util.regex.Matcher;
     27 import java.util.regex.Pattern;
     28 
     29 @RunWith(JUnit4.class)
     30 public class TraceParserTest {
     31 
     32     TraceParser mParser;
     33 
     34     @Before
     35     public void setUp() throws Exception {
     36         mParser = new TraceParser();
     37     }
     38 
     39     @Test
     40     public void testParseTaskName() {
     41         String line = "         mmcqd/0-260 ";
     42         Matcher m = Pattern.compile(TraceParser.REGEX_TASK_NAME).matcher(line);
     43         m.find();
     44         Assert.assertEquals("mmcqd/0", m.group(1).trim());
     45     }
     46 
     47     @Test
     48     public void testParseCpuNum() {
     49         String line = "   [002] ";
     50         Matcher m = Pattern.compile(TraceParser.REGEX_CPU_NUM).matcher(line);
     51         m.find();
     52         Assert.assertEquals("002", m.group(1).trim());
     53     }
     54 
     55     @Test
     56     public void testParseFlagCluster() {
     57         String line = " d.h4 ";
     58         Matcher m = Pattern.compile(TraceParser.REGEX_FLAG_CLUSTER).matcher(line);
     59         m.find();
     60         Assert.assertEquals("d", m.group(1).trim());
     61         Assert.assertEquals(".", m.group(2).trim());
     62         Assert.assertEquals("h", m.group(3).trim());
     63         Assert.assertEquals("4", m.group(4).trim());
     64     }
     65 
     66     @Test
     67     public void testParseTimestamp() {
     68         String line = " 87062.464667: ";
     69         Matcher m = Pattern.compile(TraceParser.REGEX_TIMESTAMP).matcher(line);
     70         m.find();
     71         Assert.assertEquals("87062.464667", m.group(1).trim());
     72     }
     73 
     74     @Test
     75     public void testParseFunctionName() {
     76         String line = " mmc_cmd_rw_end: ";
     77         Matcher m = Pattern.compile(TraceParser.REGEX_FUNCTION_NAME).matcher(line);
     78         m.find();
     79         Assert.assertEquals("mmc_cmd_rw_end", m.group(1));
     80     }
     81 
     82     @Test
     83     public void testParseFunctionParams() {
     84         String line = "cmd=25,int_status=0x00000001,response=0x00000010";
     85         Map<String, Long> params = mParser.parseFunctionParams(line);
     86         Assert.assertTrue(params.containsKey("cmd"));
     87         Assert.assertTrue(params.containsKey("int_status"));
     88         Assert.assertTrue(params.containsKey("response"));
     89         Assert.assertEquals(Long.valueOf(25), params.get("cmd"));
     90         Assert.assertEquals(Long.valueOf(1), params.get("int_status"));
     91         Assert.assertEquals(Long.valueOf(16), params.get("response"));
     92     }
     93 
     94     @Test
     95     public void testParseWholeTraceLine() {
     96         String line =
     97                 "         mmcqd/0-260   [000] d.h2 87062.464736: mmc_cmd_rw_end: cmd=25,int_status=0x00000001,response=0x00000900";
     98         Matcher m = TraceParser.TRACE_LINE.matcher(line);
     99         Assert.assertTrue(m.find());
    100         Assert.assertEquals("mmcqd/0", m.group(1));
    101         Assert.assertEquals("000", m.group(2));
    102         Assert.assertEquals("d", m.group(3));
    103         Assert.assertEquals(".", m.group(4));
    104         Assert.assertEquals("h", m.group(5));
    105         Assert.assertEquals("2", m.group(6));
    106         Assert.assertEquals("87062.464736", m.group(7));
    107         Assert.assertEquals("mmc_cmd_rw_end", m.group(8));
    108         Assert.assertEquals("cmd=25,int_status=0x00000001,response=0x00000900", m.group(9));
    109         line =
    110                 "          mmcqd/0-260   [000] ...1    58.216426: mmc_blk_rw_start: cmd=18,addr=0x0039f640,size=0x00000020";
    111         m = TraceParser.TRACE_LINE.matcher(line);
    112         Assert.assertTrue(m.find());
    113     }
    114 }
    115