1# Copyright 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import os 5import unittest 6 7from telemetry import story 8from telemetry import page as page_module 9from telemetry.value import improvement_direction 10from telemetry.value import list_of_scalar_values 11from telemetry.value import merge_values 12from telemetry.value import scalar 13 14 15class TestBase(unittest.TestCase): 16 def setUp(self): 17 story_set = story.StorySet(base_dir=os.path.dirname(__file__)) 18 story_set.AddStory( 19 page_module.Page('http://www.bar.com/', story_set, story_set.base_dir)) 20 story_set.AddStory( 21 page_module.Page('http://www.baz.com/', story_set, story_set.base_dir)) 22 story_set.AddStory( 23 page_module.Page('http://www.foo.com/', story_set, story_set.base_dir)) 24 self.story_set = story_set 25 26 @property 27 def pages(self): 28 return self.story_set.stories 29 30class MergeValueTest(TestBase): 31 def testDefaultKeyFunc(self): 32 page0 = self.pages[0] 33 34 value = scalar.ScalarValue( 35 page0, 'x', 'units', 1, 36 improvement_direction=improvement_direction.UP, 37 tir_label='foo', grouping_keys={'soup': 'nuts', 'a': 'b'}) 38 39 self.assertEquals((('value_name', 'x'), ('ue_stable_id', 'foo'), 40 ('a', 'b'), ('soup', 'nuts')), 41 merge_values.DefaultKeyFunc(value)) 42 43 def testSamePageMergeBasic(self): 44 page0 = self.pages[0] 45 page1 = self.pages[1] 46 47 all_values = [scalar.ScalarValue( 48 page0, 'x', 'units', 1, 49 improvement_direction=improvement_direction.UP), 50 scalar.ScalarValue( 51 page1, 'x', 'units', 4, 52 improvement_direction=improvement_direction.UP), 53 scalar.ScalarValue( 54 page0, 'x', 'units', 2, 55 improvement_direction=improvement_direction.UP), 56 scalar.ScalarValue( 57 page1, 'x', 'units', 5, 58 improvement_direction=improvement_direction.UP)] 59 60 merged_values = merge_values.MergeLikeValuesFromSamePage(all_values) 61 # Sort the results so that their order is predictable for the subsequent 62 # assertions. 63 merged_values.sort(key=lambda x: x.page.url) 64 65 self.assertEquals(2, len(merged_values)) 66 67 self.assertEquals((page0, 'x'), 68 (merged_values[0].page, merged_values[0].name)) 69 self.assertEquals([1, 2], merged_values[0].values) 70 71 self.assertEquals((page1, 'x'), 72 (merged_values[1].page, merged_values[1].name)) 73 self.assertEquals([4, 5], merged_values[1].values) 74 75 def testSamePageMergeNonstandardKeyFunc(self): 76 page0 = self.pages[0] 77 page1 = self.pages[1] 78 79 all_values = [scalar.ScalarValue( 80 page0, 'x', 'units', 1, 81 improvement_direction=improvement_direction.UP), 82 scalar.ScalarValue( 83 page1, 'x', 'units', 4, 84 improvement_direction=improvement_direction.UP), 85 scalar.ScalarValue( 86 page0, 'y', 'units', 2, 87 improvement_direction=improvement_direction.UP), 88 scalar.ScalarValue( 89 page1, 'y', 'units', 5, 90 improvement_direction=improvement_direction.UP)] 91 92 merged_values = merge_values.MergeLikeValuesFromSamePage( 93 all_values, key_func=lambda v: v.page.display_name) 94 # Sort the results so that their order is predictable for the subsequent 95 # assertions. 96 merged_values.sort(key=lambda x: x.page.url) 97 98 self.assertEquals(2, len(merged_values)) 99 self.assertEquals([1, 2], merged_values[0].values) 100 self.assertEquals([4, 5], merged_values[1].values) 101 102 def testSamePageMergeOneValue(self): 103 page0 = self.pages[0] 104 105 all_values = [scalar.ScalarValue( 106 page0, 'x', 'units', 1, 107 improvement_direction=improvement_direction.DOWN)] 108 109 # Sort the results so that their order is predictable for the subsequent 110 # assertions. 111 merged_values = merge_values.MergeLikeValuesFromSamePage(all_values) 112 self.assertEquals(1, len(merged_values)) 113 self.assertEquals(all_values[0].name, merged_values[0].name) 114 self.assertEquals(all_values[0].units, merged_values[0].units) 115 116 def testSamePageMergeWithInteractionRecord(self): 117 page0 = self.pages[0] 118 119 all_values = [scalar.ScalarValue( 120 page0, 'foo-x', 'units', 1, tir_label='foo', 121 improvement_direction=improvement_direction.UP), 122 scalar.ScalarValue( 123 page0, 'foo-x', 'units', 4, tir_label='foo', 124 improvement_direction=improvement_direction.UP)] 125 126 merged_values = merge_values.MergeLikeValuesFromSamePage(all_values) 127 self.assertEquals(1, len(merged_values)) 128 self.assertEquals('foo', merged_values[0].tir_label) 129 130 def testSamePageMergeWithTwoInteractionRecords(self): 131 page0 = self.pages[0] 132 133 all_values = [scalar.ScalarValue(page0, 'x', 'units', 1, tir_label='foo'), 134 scalar.ScalarValue(page0, 'x', 'units', 4, tir_label='bar')] 135 136 merged_values = merge_values.MergeLikeValuesFromSamePage(all_values) 137 self.assertEquals(2, len(merged_values)) 138 self.assertEquals('foo', merged_values[0].tir_label) 139 self.assertEquals('bar', merged_values[1].tir_label) 140 141 def testSamePageMergeWithSameGroupingKey(self): 142 page0 = self.pages[0] 143 all_values = [scalar.ScalarValue(page0, 'x', 'units', 1, 144 grouping_keys={'foo': 'bar'}), 145 scalar.ScalarValue(page0, 'x', 'units', 4, 146 grouping_keys={'foo': 'bar'})] 147 148 merged_values = merge_values.MergeLikeValuesFromSamePage(all_values) 149 self.assertEquals(1, len(merged_values)) 150 self.assertEquals({'foo': 'bar'}, merged_values[0].grouping_keys) 151 152 def testSamePageMergeWithDifferentGroupingKey(self): 153 page0 = self.pages[0] 154 all_values = [scalar.ScalarValue(page0, 'x', 'units', 1, 155 grouping_keys={'foo': 'bar'}), 156 scalar.ScalarValue(page0, 'x', 'units', 4, 157 grouping_keys={'foo': 'qux'})] 158 159 merged_values = merge_values.MergeLikeValuesFromSamePage(all_values) 160 self.assertEquals(2, len(merged_values)) 161 self.assertEquals({'foo': 'bar'}, merged_values[0].grouping_keys) 162 self.assertEquals({'foo': 'qux'}, merged_values[1].grouping_keys) 163 164 def testDifferentPageMergeBasic(self): 165 page0 = self.pages[0] 166 page1 = self.pages[1] 167 168 all_values = [scalar.ScalarValue( 169 page0, 'x', 'units', 1, 170 improvement_direction=improvement_direction.UP), 171 scalar.ScalarValue( 172 page1, 'x', 'units', 2, 173 improvement_direction=improvement_direction.UP), 174 scalar.ScalarValue( 175 page0, 'y', 'units', 10, 176 improvement_direction=improvement_direction.UP), 177 scalar.ScalarValue( 178 page1, 'y', 'units', 20, 179 improvement_direction=improvement_direction.UP)] 180 181 merged_values = merge_values.MergeLikeValuesFromDifferentPages(all_values) 182 merged_values.sort(key=lambda x: x.name) 183 self.assertEquals(2, len(merged_values)) 184 185 self.assertEquals((None, 'x'), 186 (merged_values[0].page, merged_values[0].name)) 187 self.assertEquals([1, 2], merged_values[0].values) 188 189 self.assertEquals((None, 'y'), 190 (merged_values[1].page, merged_values[1].name)) 191 self.assertEquals([10, 20], merged_values[1].values) 192 193 def testDifferentPageMergeNonstandardKeyFunc(self): 194 page0 = self.pages[0] 195 page1 = self.pages[1] 196 197 all_values = [scalar.ScalarValue( 198 page0, 'x', 'units', 1, 199 improvement_direction=improvement_direction.UP), 200 scalar.ScalarValue( 201 page1, 'x', 'units', 2, 202 improvement_direction=improvement_direction.UP), 203 scalar.ScalarValue( 204 page0, 'y', 'units', 10, 205 improvement_direction=improvement_direction.UP), 206 scalar.ScalarValue( 207 page1, 'y', 'units', 20, 208 improvement_direction=improvement_direction.UP)] 209 210 merged_values = merge_values.MergeLikeValuesFromDifferentPages( 211 all_values, key_func=lambda v: True) 212 213 self.assertEquals(1, len(merged_values)) 214 self.assertEquals([1, 2, 10, 20], merged_values[0].values) 215 216 def testDifferentPageMergeSingleValueStillMerges(self): 217 page0 = self.pages[0] 218 219 all_values = [scalar.ScalarValue( 220 page0, 'x', 'units', 1, 221 improvement_direction=improvement_direction.DOWN)] 222 223 # Sort the results so that their order is predictable for the subsequent 224 # assertions. 225 merged_values = merge_values.MergeLikeValuesFromDifferentPages(all_values) 226 self.assertEquals(1, len(merged_values)) 227 228 self.assertEquals((None, 'x'), 229 (merged_values[0].page, merged_values[0].name)) 230 self.assertTrue( 231 isinstance(merged_values[0], list_of_scalar_values.ListOfScalarValues)) 232 self.assertEquals([1], merged_values[0].values) 233 234 def testDifferentPageMergeWithInteractionRecord(self): 235 page0 = self.pages[0] 236 page1 = self.pages[1] 237 238 v0 = scalar.ScalarValue(page0, 'x', 'units', 1, tir_label='foo') 239 v1 = scalar.ScalarValue(page0, 'y', 'units', 30, tir_label='bar') 240 v2 = scalar.ScalarValue(page1, 'x', 'units', 2, tir_label='foo') 241 v3 = scalar.ScalarValue(page1, 'y', 'units', 40, tir_label='baz') 242 243 all_values = [v0, v1, v2, v3] 244 245 merged_x = list_of_scalar_values.ListOfScalarValues( 246 None, 'x', 'units', [1, 2], tir_label='foo') 247 merged_y_bar = list_of_scalar_values.ListOfScalarValues( 248 None, 'y', 'units', [30], tir_label='bar') 249 merged_y_baz = list_of_scalar_values.ListOfScalarValues( 250 None, 'y', 'units', [40], tir_label='baz') 251 252 merged_values = merge_values.MergeLikeValuesFromDifferentPages(all_values) 253 merged_values.sort(key=lambda x: x.tir_label) 254 255 self.assertEquals([merged_y_bar, merged_y_baz, merged_x], merged_values) 256