1 /* 2 * Copyright (c) 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * 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 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 17 package com.android.vts.util; 18 19 import static org.junit.Assert.*; 20 21 import com.android.vts.entity.ProfilingPointEntity; 22 import com.android.vts.entity.ProfilingPointSummaryEntity; 23 import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode; 24 import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; 25 import com.google.appengine.tools.development.testing.LocalServiceTestHelper; 26 import java.util.Arrays; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 import org.junit.After; 31 import org.junit.Before; 32 import org.junit.Test; 33 34 public class ProfilingPointSummaryTest { 35 private final LocalServiceTestHelper helper = 36 new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); 37 private static String[] labels = new String[] {"label1", "label2", "label3"}; 38 private static long[] values = new long[] {1, 2, 3}; 39 private static ProfilingPointSummary summary; 40 41 /** 42 * Helper method for creating ProfilingPointSummaryEntity objects. 43 * 44 * @param labels The list of data labels. 45 * @param values The list of data values. Must be equal in size to the labels list. 46 * @param regressionMode The regression mode. 47 * @return A ProfilingPointSummaryEntity with specified data. 48 */ 49 private static ProfilingPointSummaryEntity createProfilingReport( 50 String[] labels, long[] values, VtsProfilingRegressionMode regressionMode) { 51 List<String> labelList = Arrays.asList(labels); 52 StatSummary globalStats = new StatSummary("global", regressionMode); 53 Map<String, StatSummary> labelStats = new HashMap<>(); 54 for (int i = 0; i < labels.length; ++i) { 55 StatSummary stat = new StatSummary(labels[i], regressionMode); 56 stat.updateStats(values[i]); 57 labelStats.put(labels[i], stat); 58 globalStats.updateStats(values[i]); 59 } 60 return new ProfilingPointSummaryEntity( 61 ProfilingPointEntity.createKey("test", "pp"), 62 globalStats, 63 labelList, 64 labelStats, 65 "branch", 66 "build", 67 null, 68 0); 69 } 70 71 @Before 72 public void setUp() { 73 helper.setUp(); 74 VtsProfilingRegressionMode mode = VtsProfilingRegressionMode.VTS_REGRESSION_MODE_INCREASING; 75 summary = new ProfilingPointSummary("x", "y", mode); 76 ProfilingPointSummaryEntity pt = createProfilingReport(labels, values, mode); 77 summary.update(pt); 78 } 79 80 @After 81 public void tearDown() { 82 helper.tearDown(); 83 } 84 85 /** Test that all labels are found by hasLabel. */ 86 @Test 87 public void testHasLabel() { 88 for (String label : labels) { 89 assertTrue(summary.hasLabel(label)); 90 } 91 } 92 93 /** Test that invalid labels are not found by hasLabel. */ 94 @Test 95 public void testInvalidHasLabel() { 96 assertFalse(summary.hasLabel("bad label")); 97 } 98 99 /** Test that all stat summaries can be retrieved by profiling point label. */ 100 @Test 101 public void testGetStatSummary() { 102 for (String label : labels) { 103 StatSummary stats = summary.getStatSummary(label); 104 assertNotNull(stats); 105 assertEquals(label, stats.getLabel()); 106 } 107 } 108 109 /** Test that the getStatSummary method returns null when the label is not present. */ 110 @Test 111 public void testInvalidGetStatSummary() { 112 StatSummary stats = summary.getStatSummary("bad label"); 113 assertNull(stats); 114 } 115 116 /** Test that StatSummary objects are iterated in the order that the labels were provided. */ 117 @Test 118 public void testIterator() { 119 VtsProfilingRegressionMode mode = VtsProfilingRegressionMode.VTS_REGRESSION_MODE_INCREASING; 120 ProfilingPointSummaryEntity pt = createProfilingReport(labels, values, mode); 121 summary.update(pt); 122 123 int i = 0; 124 for (StatSummary stats : summary) { 125 assertEquals(labels[i++], stats.getLabel()); 126 } 127 } 128 129 /** Test that the updateLabel method updates the StatSummary for just the label provided. */ 130 @Test 131 public void testUpdateLabelGrouped() { 132 VtsProfilingRegressionMode mode = VtsProfilingRegressionMode.VTS_REGRESSION_MODE_INCREASING; 133 summary = new ProfilingPointSummary("x", "y", mode); 134 ProfilingPointSummaryEntity pt = createProfilingReport(labels, values, mode); 135 summary.updateLabel(pt, labels[0]); 136 137 // Ensure the label specified is present and has been updated for each data point. 138 assertTrue(summary.hasLabel(labels[0])); 139 assertNotNull(summary.getStatSummary(labels[0])); 140 assertEquals(summary.getStatSummary(labels[0]).getCount(), labels.length); 141 142 // Check that the other labels were not updated. 143 for (int i = 1; i < labels.length; i++) { 144 assertFalse(summary.hasLabel(labels[i])); 145 } 146 } 147 } 148