- add sources.
[platform/framework/web/crosswalk.git] / src / tools / perf / metrics / speedindex_unittest.py
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.
4
5 # These tests access private methods in the speedindex module.
6 # pylint: disable=W0212
7
8 import json
9 import os
10 import unittest
11
12 from telemetry.core.timeline import model
13 from metrics import speedindex
14
15 # Sample timeline data in the json format provided by devtools.
16 # The sample events will be used in several tests below.
17 _TEST_DIR = os.path.join(os.path.dirname(__file__), 'unittest_data')
18 _SAMPLE_DATA = json.load(open(os.path.join(_TEST_DIR, 'sample_timeline.json')))
19 _SAMPLE_EVENTS = model.TimelineModel(event_data=_SAMPLE_DATA).GetAllEvents()
20
21
22 class IncludedPaintEventsTest(unittest.TestCase):
23   def testNumberPaintEvents(self):
24     # In the sample data, there's one event that occurs before the layout event,
25     # and one paint event that's not a leaf paint event.
26     events = speedindex._IncludedPaintEvents(_SAMPLE_EVENTS)
27     self.assertEquals(len(events), 5)
28
29
30 class TimeAreaDictTest(unittest.TestCase):
31   def testAdjustedAreaDict(self):
32     paint_events = speedindex._IncludedPaintEvents(_SAMPLE_EVENTS)
33     viewport = 1000, 1000
34     time_area_dict = speedindex._TimeAreaDict(paint_events, viewport)
35     self.assertEquals(len(time_area_dict), 4)
36     # The event that ends at time 100 is a fullscreen; it's discounted by half.
37     self.assertEquals(time_area_dict[100], 500000)
38     self.assertEquals(time_area_dict[300], 100000)
39     self.assertEquals(time_area_dict[400], 200000)
40     self.assertEquals(time_area_dict[800], 200000)
41
42
43 class SpeedIndexTest(unittest.TestCase):
44   def testWithSampleData(self):
45     viewport = 1000, 1000
46     # Add up the parts of the speed index for each time interval.
47     # Each part is the time interval multiplied by the proportion of the
48     # total area value that is not yet painted for that interval.
49     parts = []
50     parts.append(100 * 1.0)
51     parts.append(200 * 0.5)
52     parts.append(100 * 0.4)
53     parts.append(400 * 0.2)
54     expected = sum(parts)  # 330.0
55     actual = speedindex._SpeedIndex(_SAMPLE_EVENTS, viewport)
56     self.assertEqual(actual, expected)
57
58
59 class WPTComparisonTest(unittest.TestCase):
60   """Compare the speed index results with results given by webpagetest.org.
61
62   Given the same timeline data, both this speedindex metric and webpagetest.org
63   should both return the same results. Fortunately, webpagetest.org also
64   provides timeline data in json format along with the speed index results.
65   """
66
67   def _TestJsonTimelineExpectation(self, filename, viewport, expected):
68     """Check whether the result for some timeline data is as expected.
69
70     Args:
71       filename: Filename of a json file which contains a
72       expected: The result expected based on the WPT result.
73     """
74     file_path = os.path.join(_TEST_DIR, filename)
75     with open(file_path) as json_file:
76       raw_events = json.load(json_file)
77       events = model.TimelineModel(event_data=raw_events).GetAllEvents()
78       actual = speedindex._SpeedIndex(events, viewport)
79       # The result might differ by 1 or more milliseconds due to rounding,
80       # so compare to the nearest 10 milliseconds.
81       self.assertAlmostEqual(actual, expected, places=-1)
82
83   def testCern(self):
84     # Page: http://info.cern.ch/hypertext/WWW/TheProject.html
85     # This page has only one paint event.
86     self._TestJsonTimelineExpectation(
87         'cern_repeat_timeline.json', (1014, 650), 379.0)
88
89   def testBaidu(self):
90     # Page: http://www.baidu.com/
91     # This page has several paint events, but no nested paint events.
92     self._TestJsonTimelineExpectation(
93         'baidu_repeat_timeline.json', (1014, 650), 1761.43)
94
95   def test2ch(self):
96     # Page: http://2ch.net/
97     # This page has several paint events, including nested paint events.
98     self._TestJsonTimelineExpectation(
99         '2ch_repeat_timeline.json', (997, 650), 674.58)
100
101
102 if __name__ == "__main__":
103   unittest.main()
104