OLD | NEW |
| (Empty) |
1 # Copyright (c) 2011 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 import time | |
6 import unittest | |
7 | |
8 from ui_perf_test_utils import UIPerfTestUtils | |
9 | |
10 | |
11 class TestUIPerfUtils(unittest.TestCase): | |
12 """Test UIPerfUtils class.""" | |
13 | |
14 def testConvertDataListToString(self): | |
15 times = [1.023344324, 2.3233333, 2.442324444] | |
16 output_string = UIPerfTestUtils.ConvertDataListToString(times) | |
17 self.assertEqual(output_string, '[1.02334, 2.32333, 2.44232]', | |
18 msg='result output is wrong') | |
19 | |
20 def testGetResultStringForPerfBot(self): | |
21 """Test PrintResultList method.""" | |
22 times = [1.023, 2.323, 2.44232] | |
23 output_string = UIPerfTestUtils.GetResultStringForPerfBot( | |
24 'playback', '', 'bear', times, 'ms') | |
25 self.assertEqual(output_string, | |
26 'RESULT playback: bear= [1.02300, 2.32300, 2.44232] ms', | |
27 msg='result output is wrong') | |
28 | |
29 def testGetResultStringForPerfBotEmptyData(self): | |
30 """Test PrintResultList method with empty data.""" | |
31 times = [] | |
32 output_string = UIPerfTestUtils.GetResultStringForPerfBot( | |
33 'playback', '', 'bear', times, 'ms') | |
34 self.assertFalse(output_string, msg='Result output is not empty.') | |
35 | |
36 def testFindProcessesAndGetResourceInfo(self): | |
37 """Test FindProcesses and GetResourceInfo methods. | |
38 | |
39 Python process should be found when we run this script. Assert all | |
40 elements in processInfo are not None. | |
41 """ | |
42 list = UIPerfTestUtils.FindProcesses('python') | |
43 self.assertTrue(len(list) > 0, 'python process cannot be found') | |
44 info = UIPerfTestUtils.GetResourceInfo(list[0], time.time()) | |
45 self._AssertProcessInfo(info) | |
46 | |
47 def GetChromeRendererProcessInfo(self): | |
48 """Test GetChromeRendererProcessInfo method. | |
49 | |
50 You must start Chrome before you run your test. Otherwise, it fails. | |
51 So, this test is not included in the unit test (i.e., the method name | |
52 does not start with "test"). | |
53 | |
54 TODO(imasaki@chromium.org): find a way to start Chrome automatically. | |
55 """ | |
56 start_time = time.time() | |
57 info = UIPerfTestUtils.GetChromeRendererProcessInfo(start_time) | |
58 self._AssertProcessInfo(info) | |
59 | |
60 def _AssertProcessInfo(self, info): | |
61 """Assert process info has correct length and each element is not null.""" | |
62 # See UIPerfTestUtils.chrome_process_info_names. | |
63 self.assertEqual(len(info), 7, msg='the length of info should be 7') | |
64 for i in range(len(info)): | |
65 self.assertTrue(info[i] is not None, msg='process info has None data') | |
66 | |
67 def _CreateFakeProcessInfo(self, time, process_info_length): | |
68 """Create fake process info for testing. | |
69 | |
70 Args: | |
71 time: time used for measured_time. | |
72 | |
73 Returns: | |
74 a process info with some data for testing. | |
75 """ | |
76 chrome_renderer_process_info = [] | |
77 for i in range(process_info_length): | |
78 chrome_renderer_process_info.append(i + time) | |
79 return chrome_renderer_process_info | |
80 | |
81 def _PrintMeasuredDataTestHelper(self, show_time_index, | |
82 expected_output_string, | |
83 display_filter=None): | |
84 """A helper function for tests testPrintMeasuredData*. | |
85 | |
86 Create fake process and call PrintMeasuredData with appropriate arguments. | |
87 | |
88 Args: | |
89 show_time_index: call PrintMeasuredData with this show_time_index. | |
90 expected_output_string: the expected result string to be compared. | |
91 display_filter: run test with this display_filter, which specifies which | |
92 measurements to display. | |
93 """ | |
94 # Build process info for testing. | |
95 chrome_renderer_process_infos = [] | |
96 run_info1 = [] | |
97 run_info1.append(self._CreateFakeProcessInfo(10, 7)) | |
98 run_info1.append(self._CreateFakeProcessInfo(20, 7)) | |
99 chrome_renderer_process_infos.append(run_info1) | |
100 run_info2 = [] | |
101 run_info2.append(self._CreateFakeProcessInfo(10, 7)) | |
102 chrome_renderer_process_infos.append(run_info2) | |
103 chrome_process_info_names = ['measure-time', 'pct-cpu', 'cpu-user', | |
104 'cpu-system', 'memory-rss', 'memory-vms', | |
105 'pct-process-memory'] | |
106 chrome_process_info_units = ['sec', 'percent', 'load', | |
107 'load', 'MB', 'MB', 'percent'] | |
108 chrome_process_trace_list = ['t', 'p', 'l', 'l', 'm', 'm', 'p'] | |
109 | |
110 output_string = UIPerfTestUtils.PrintMeasuredData( | |
111 measured_data_list=chrome_renderer_process_infos, | |
112 measured_data_name_list=chrome_process_info_names, | |
113 measured_data_unit_list=chrome_process_info_units, | |
114 parameter_string='', trace_list=chrome_process_trace_list, | |
115 show_time_index=show_time_index, remove_first_result=False, | |
116 display_filter=display_filter) | |
117 | |
118 self.assertEqual(output_string, expected_output_string, | |
119 msg=('output string is wrong' | |
120 '\nexpected:\n%s \nactual:\n%s') % ( | |
121 expected_output_string, output_string)) | |
122 | |
123 def testPrintMeasuredDataShowTimeIndex(self): | |
124 expected_output_string = ( | |
125 'RESULT measure-time-0: t= [10.00000, 10.00000] sec\n' | |
126 'RESULT measure-time-1: t= [20.00000] sec\n' | |
127 'RESULT pct-cpu-0: p= [11.00000, 11.00000] percent\n' | |
128 'RESULT pct-cpu-1: p= [21.00000] percent\n' | |
129 'RESULT cpu-user-0: l= [12.00000, 12.00000] load\n' | |
130 'RESULT cpu-user-1: l= [22.00000] load\n' | |
131 'RESULT cpu-system-0: l= [13.00000, 13.00000] load\n' | |
132 'RESULT cpu-system-1: l= [23.00000] load\n' | |
133 'RESULT memory-rss-0: m= [14.00000, 14.00000] MB\n' | |
134 'RESULT memory-rss-1: m= [24.00000] MB\n' | |
135 'RESULT memory-vms-0: m= [15.00000, 15.00000] MB\n' | |
136 'RESULT memory-vms-1: m= [25.00000] MB\n' | |
137 'RESULT pct-process-memory-0: p= [16.00000, 16.00000] percent\n' | |
138 'RESULT pct-process-memory-1: p= [26.00000] percent\n') | |
139 self._PrintMeasuredDataTestHelper(True, expected_output_string) | |
140 | |
141 def testPrintMeasuredDataNoShowTimeIndex(self): | |
142 expected_output_string = ( | |
143 'RESULT measure-time: t= [10.00000, 10.00000, 20.00000] sec\n' | |
144 'RESULT pct-cpu: p= [11.00000, 11.00000, 21.00000] percent\n' | |
145 'RESULT cpu-user: l= [12.00000, 12.00000, 22.00000] load\n' | |
146 'RESULT cpu-system: l= [13.00000, 13.00000, 23.00000] load\n' | |
147 'RESULT memory-rss: m= [14.00000, 14.00000, 24.00000] MB\n' | |
148 'RESULT memory-vms: m= [15.00000, 15.00000, 25.00000] MB\n' | |
149 'RESULT pct-process-memory: p= [16.00000, 16.00000, 26.00000]' | |
150 ' percent\n') | |
151 self._PrintMeasuredDataTestHelper(False, expected_output_string) | |
152 | |
153 def testPrintMeasuredDataNoShowTimeIndexWithDisplayFilter(self): | |
154 expected_output_string = ( | |
155 'RESULT pct-cpu: p= [11.00000, 11.00000, 21.00000] percent\n') | |
156 self._PrintMeasuredDataTestHelper(False, expected_output_string, | |
157 display_filter=['pct-cpu']) | |
OLD | NEW |