1 /* 2 * Copyright (C) 2016 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.tradefed.util; 17 18 import junit.framework.TestCase; 19 20 import org.junit.Assert; 21 22 import java.util.List; 23 import java.util.Map; 24 25 /** 26 * Unit test class for {@link SimplePerfStatResultParser} 27 */ 28 public class SimplePerfStatResultParserTest extends TestCase { 29 30 public void testParseSingleLineWithEmptyString() { 31 String emptyString = ""; 32 List<String> result = SimplePerfStatResultParser.parseSingleLine(emptyString); 33 Assert.assertTrue(result.size() == 0); 34 } 35 36 public void testParseSingleLineWithNullString() { 37 String nullString = null; 38 List<String> result = SimplePerfStatResultParser.parseSingleLine(nullString); 39 Assert.assertTrue(result.size() == 0); 40 } 41 42 public void testParseSingleLineWithHeaderString() { 43 String headerString = "Performance counter statistics:SomeRandomData"; 44 List<String> result = SimplePerfStatResultParser.parseSingleLine(headerString); 45 Assert.assertTrue(result.size() == 0); 46 } 47 48 public void testParseSingleLineWithReturnString() { 49 String returnString = "\n"; 50 List<String> result = SimplePerfStatResultParser.parseSingleLine(returnString); 51 Assert.assertTrue(result.size() == 0); 52 } 53 54 public void testParseSingleLineWithRandomString() { 55 String randomString = " lkweknqwq(#@?>S@$Q \r\n"; 56 List<String> result = SimplePerfStatResultParser.parseSingleLine(randomString); 57 Assert.assertTrue(result.size() == 0); 58 } 59 60 public void testParseSingleLineWithValidMetricString() { 61 String validMetricString = " 37,037,350 cpu-cycles # 0.126492 GHz (100%)"; 62 List<String> result = SimplePerfStatResultParser.parseSingleLine(validMetricString); 63 Assert.assertTrue(result.size() == 3); 64 Assert.assertTrue(result.get(0).equals("37,037,350")); 65 Assert.assertTrue(result.get(1).equals("cpu-cycles")); 66 Assert.assertTrue(result.get(2).equals("0.126492 GHz (100%)")); 67 } 68 69 public void testParseSingleLineWithValidTotalTimeString() { 70 String validTotalTimeString = "Total test time: 0.292803 seconds."; 71 List<String> result = SimplePerfStatResultParser.parseSingleLine(validTotalTimeString); 72 Assert.assertTrue(result.size() == 1); 73 Assert.assertTrue(result.get(0).equals("0.292803")); 74 } 75 76 public void testParseSingleLineWithMultiplePoundString() { 77 String multiplePoundString = " 37,037,350 cpu-cycles # 0.126492 # GHz (100%)"; 78 List<String> result = SimplePerfStatResultParser.parseSingleLine(multiplePoundString); 79 Assert.assertTrue(result.size() == 0); 80 } 81 82 public void testParseSingleLineWithMultipleSpaceString() { 83 String multiplePoundString = " 37,037,350 3735 cpu-cycles # 0.126492 GHz (100%)"; 84 List<String> result = SimplePerfStatResultParser.parseSingleLine(multiplePoundString); 85 Assert.assertTrue(result.size() == 0); 86 } 87 88 public void testParseRawOutputWithNullString() { 89 String nullString = null; 90 SimplePerfResult result = SimplePerfStatResultParser.parseRawOutput(nullString); 91 Assert.assertNull(result); 92 } 93 94 public void testParseRawOutputWithEmptyString() { 95 String emptyString = "\n\n\n\n\n\n"; 96 SimplePerfResult result = SimplePerfStatResultParser.parseRawOutput(emptyString); 97 Assert.assertNull(result); 98 } 99 100 public void testParseRawOutputWithValidString() { 101 String validString = 102 "USER PID PPID VSIZE RSS WCHAN PC NAME\n" 103 + "root 1 0 7740 1380 SyS_epoll_ 0008d55c S /init\n" 104 + "root 156 2 0 0 worker_thr 00000000 S kworker/3:1H\n" 105 + "root 7262 2 0 0 worker_thr 00000000 S kworker/u8:3\n" 106 + "Performance counter statistics:\n" 107 + "\n" 108 + " 128,716,783 cpu-cycles # 0.103231 GHz (100%)\n" 109 + " 654,321 dummy-benchmark # test\n" 110 + "92.337914(ms) task-clock # 175.304352% cpu usage (50%)\n" 111 + "\n" 112 + "Total test time: 1.246881 seconds.\n"; 113 SimplePerfResult result = SimplePerfStatResultParser.parseRawOutput(validString); 114 Assert.assertEquals("1.246881", result.getTotalTestTime()); 115 Map<String, String> benchmarkMetricsMap = result.getBenchmarkMetrics(); 116 Map<String, String> benchmarkCommentsMap = result.getBenchmarkComments(); 117 Assert.assertEquals("128,716,783", benchmarkMetricsMap.get("cpu-cycles")); 118 Assert.assertEquals("0.103231 GHz (100%)", benchmarkCommentsMap.get("cpu-cycles")); 119 Assert.assertEquals("654,321", benchmarkMetricsMap.get("dummy-benchmark")); 120 Assert.assertEquals("test", benchmarkCommentsMap.get("dummy-benchmark")); 121 } 122 } 123