| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 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. | |
| 4 import os | |
| 5 import StringIO | |
| 6 import unittest | |
| 7 | |
| 8 import mock | |
| 9 | |
| 10 from telemetry import story | |
| 11 from telemetry.internal.results import csv_pivot_table_output_formatter | |
| 12 from telemetry.internal.results import page_test_results | |
| 13 from telemetry import page as page_module | |
| 14 from telemetry.value import improvement_direction | |
| 15 from telemetry.value import scalar | |
| 16 from telemetry.value import trace | |
| 17 from tracing.trace_data import trace_data | |
| 18 | |
| 19 | |
| 20 def _MakeStorySet(): | |
| 21 story_set = story.StorySet(base_dir=os.path.dirname(__file__)) | |
| 22 story_set.AddStory(page_module.Page( | |
| 23 'http://www.foo.com/', story_set, story_set.base_dir, | |
| 24 name='http://www.foo.com/')) | |
| 25 story_set.AddStory(page_module.Page( | |
| 26 'http://www.bar.com/', story_set, story_set.base_dir, | |
| 27 name='http://www.bar.com/')) | |
| 28 return story_set | |
| 29 | |
| 30 | |
| 31 class CsvPivotTableOutputFormatterTest(unittest.TestCase): | |
| 32 | |
| 33 # The line separator used by CSV formatter. | |
| 34 _LINE_SEPARATOR = '\r\n' | |
| 35 | |
| 36 def setUp(self): | |
| 37 self._output = StringIO.StringIO() | |
| 38 self._story_set = _MakeStorySet() | |
| 39 self._results = page_test_results.PageTestResults() | |
| 40 self._formatter = None | |
| 41 self.MakeFormatter() | |
| 42 | |
| 43 def MakeFormatter(self, trace_tag=''): | |
| 44 self._formatter = ( | |
| 45 csv_pivot_table_output_formatter.CsvPivotTableOutputFormatter( | |
| 46 self._output, trace_tag)) | |
| 47 | |
| 48 def SimulateBenchmarkRun(self, list_of_page_and_values): | |
| 49 """Simulate one run of a benchmark, using the supplied values. | |
| 50 | |
| 51 Args: | |
| 52 list_of_pages_and_values: a list of tuple (page, list of values) | |
| 53 """ | |
| 54 for page, values in list_of_page_and_values: | |
| 55 self._results.WillRunPage(page) | |
| 56 for v in values: | |
| 57 v.page = page | |
| 58 self._results.AddValue(v) | |
| 59 self._results.DidRunPage(page) | |
| 60 | |
| 61 def Format(self): | |
| 62 self._formatter.Format(self._results) | |
| 63 return self._output.getvalue() | |
| 64 | |
| 65 def testSimple(self): | |
| 66 # Test a simple benchmark with only one value: | |
| 67 self.SimulateBenchmarkRun([ | |
| 68 (self._story_set[0], [scalar.ScalarValue( | |
| 69 None, 'foo', 'seconds', 3, | |
| 70 improvement_direction=improvement_direction.DOWN)])]) | |
| 71 expected = self._LINE_SEPARATOR.join([ | |
| 72 'story_set,page,name,value,units,run_index', | |
| 73 'story_set,http://www.foo.com/,foo,3,seconds,0', | |
| 74 '']) | |
| 75 | |
| 76 self.assertEqual(expected, self.Format()) | |
| 77 | |
| 78 @mock.patch('py_utils.cloud_storage.Insert') | |
| 79 def testMultiplePagesAndValues(self, cs_insert_mock): | |
| 80 cs_insert_mock.return_value = 'https://cloud_storage_url/foo' | |
| 81 trace_value = trace.TraceValue( | |
| 82 None, trace_data.CreateTraceDataFromRawData('{"traceEvents": []}')) | |
| 83 trace_value.UploadToCloud(bucket='foo') | |
| 84 self.SimulateBenchmarkRun([ | |
| 85 (self._story_set[0], [ | |
| 86 scalar.ScalarValue( | |
| 87 None, 'foo', 'seconds', 4, | |
| 88 improvement_direction=improvement_direction.DOWN)]), | |
| 89 (self._story_set[1], [ | |
| 90 scalar.ScalarValue( | |
| 91 None, 'foo', 'seconds', 3.4, | |
| 92 improvement_direction=improvement_direction.DOWN), | |
| 93 trace_value, | |
| 94 scalar.ScalarValue( | |
| 95 None, 'bar', 'km', 10, | |
| 96 improvement_direction=improvement_direction.DOWN), | |
| 97 scalar.ScalarValue( | |
| 98 None, 'baz', 'count', 5, | |
| 99 improvement_direction=improvement_direction.DOWN)])]) | |
| 100 | |
| 101 # Parse CSV output into list of lists. | |
| 102 csv_string = self.Format() | |
| 103 lines = csv_string.split(self._LINE_SEPARATOR) | |
| 104 values = [s.split(',') for s in lines[1:-1]] | |
| 105 | |
| 106 self.assertEquals(len(values), 5) # We expect 5 value in total. | |
| 107 self.assertEquals(len(set((v[1] for v in values))), 2) # 2 pages. | |
| 108 self.assertEquals(len(set((v[2] for v in values))), 4) # 4 value names. | |
| 109 self.assertEquals(values[2], | |
| 110 ['story_set', 'http://www.bar.com/', 'trace', | |
| 111 'https://cloud_storage_url/foo', '', '1']) | |
| 112 | |
| 113 def testTraceTag(self): | |
| 114 self.MakeFormatter(trace_tag='date,option') | |
| 115 self.SimulateBenchmarkRun([ | |
| 116 (self._story_set[0], [ | |
| 117 scalar.ScalarValue( | |
| 118 None, 'foo', 'seconds', 3, | |
| 119 improvement_direction=improvement_direction.DOWN), | |
| 120 scalar.ScalarValue( | |
| 121 None, 'bar', 'tons', 5, | |
| 122 improvement_direction=improvement_direction.DOWN)])]) | |
| 123 output = self.Format().split(self._LINE_SEPARATOR) | |
| 124 | |
| 125 self.assertTrue(output[0].endswith(',trace_tag_0,trace_tag_1')) | |
| 126 for line in output[1:-1]: | |
| 127 self.assertTrue(line.endswith(',date,option')) | |
| OLD | NEW |