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.
8 from telemetry.page import page_set
9 from telemetry.results import csv_pivot_table_output_formatter
10 from telemetry.results import page_test_results
11 from telemetry.value import scalar
15 ps = page_set.PageSet(file_path=os.path.dirname(__file__))
16 ps.AddPageWithDefaultRunNavigate('http://www.foo.com/')
17 ps.AddPageWithDefaultRunNavigate('http://www.bar.com/')
21 class CsvPivotTableOutputFormatterTest(unittest.TestCase):
23 # The line separator used by CSV formatter.
24 _LINE_SEPARATOR = '\r\n'
27 self._output = StringIO.StringIO()
28 self._page_set = _MakePageSet()
29 self._results = page_test_results.PageTestResults()
30 self._formatter = None
33 def MakeFormatter(self, trace_tag=''):
35 csv_pivot_table_output_formatter.CsvPivotTableOutputFormatter(
36 self._output, trace_tag))
38 def SimulateBenchmarkRun(self, dict_of_values):
39 """Simulate one run of a benchmark, using the supplied values.
42 dict_of_values: dictionary w/ Page instance as key, a list of Values
45 for page, values in dict_of_values.iteritems():
46 self._results.WillRunPage(page)
49 self._results.AddValue(v)
50 self._results.DidRunPage(page)
53 self._formatter.Format(self._results)
54 return self._output.getvalue()
57 # Test a simple benchmark with only one value:
58 self.SimulateBenchmarkRun({
59 self._page_set[0]: [scalar.ScalarValue(None, 'foo', 'seconds', 3)]})
60 expected = self._LINE_SEPARATOR.join([
61 'page_set,page,name,value,units,run_index',
62 'page_set,http://www.foo.com/,foo,3,seconds,0',
65 self.assertEqual(expected, self.Format())
67 def testMultiplePagesAndValues(self):
68 self.SimulateBenchmarkRun({
69 self._page_set[0]: [scalar.ScalarValue(None, 'foo', 'seconds', 4)],
70 self._page_set[1]: [scalar.ScalarValue(None, 'foo', 'seconds', 3.4),
71 scalar.ScalarValue(None, 'bar', 'km', 10),
72 scalar.ScalarValue(None, 'baz', 'count', 5)]})
74 # Parse CSV output into list of lists.
75 csv_string = self.Format()
76 lines = csv_string.split(self._LINE_SEPARATOR)
77 values = map(lambda s : s.split(','), lines[1:-1])
79 self.assertEquals(len(values), 4) # We expect 4 value in total.
80 self.assertEquals(len(set((v[1] for v in values))), 2) # 2 pages.
81 self.assertEquals(len(set((v[2] for v in values))), 3) # 3 value names.
83 def testTraceTag(self):
84 self.MakeFormatter(trace_tag='date,option')
85 self.SimulateBenchmarkRun({
86 self._page_set[0]: [scalar.ScalarValue(None, 'foo', 'seconds', 3),
87 scalar.ScalarValue(None, 'bar', 'tons', 5)]})
88 output = self.Format().split(self._LINE_SEPARATOR)
90 self.assertTrue(output[0].endswith(',trace_tag_0,trace_tag_1'))
91 for line in output[1:-1]:
92 self.assertTrue(line.endswith(',date,option'))