blob: 3cc7e8584968d38499568c93d49acdd7392dee11 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Test for monorail.framework.profiler."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from framework import profiler
14
15
16class MockPatchResponse(object):
17 def execute(self):
18 pass
19
20
21class MockCloudTraceProjects(object):
22 def __init__(self):
23 self.patch_response = MockPatchResponse()
24 self.project_id = None
25 self.body = None
26
27 def patchTraces(self, projectId, body):
28 self.project_id = projectId
29 self.body = body
30 return self.patch_response
31
32
33class MockCloudTraceApi(object):
34 def __init__(self):
35 self.mock_projects = MockCloudTraceProjects()
36
37 def projects(self):
38 return self.mock_projects
39
40
41class ProfilerTest(unittest.TestCase):
42
43 def testTopLevelPhase(self):
44 prof = profiler.Profiler()
45 self.assertEqual(prof.current_phase.name, 'overall profile')
46 self.assertEqual(prof.current_phase.parent, None)
47 self.assertEqual(prof.current_phase, prof.top_phase)
48 self.assertEqual(prof.next_color, 0)
49
50 def testSinglePhase(self):
51 prof = profiler.Profiler()
52 self.assertEqual(prof.current_phase.name, 'overall profile')
53 with prof.Phase('test'):
54 self.assertEqual(prof.current_phase.name, 'test')
55 self.assertEqual(prof.current_phase.parent.name, 'overall profile')
56 self.assertEqual(prof.current_phase.name, 'overall profile')
57 self.assertEqual(prof.next_color, 1)
58
59 def testSinglePhase_SuperLongName(self):
60 prof = profiler.Profiler()
61 self.assertEqual(prof.current_phase.name, 'overall profile')
62 long_name = 'x' * 1000
63 with prof.Phase(long_name):
64 self.assertEqual(
65 'x' * profiler.MAX_PHASE_NAME_LENGTH, prof.current_phase.name)
66
67 def testSubphaseExecption(self):
68 prof = profiler.Profiler()
69 try:
70 with prof.Phase('foo'):
71 with prof.Phase('bar'):
72 pass
73 with prof.Phase('baz'):
74 raise Exception('whoops')
75 except Exception as e:
76 self.assertEqual(e.message, 'whoops')
77 finally:
78 self.assertEqual(prof.current_phase.name, 'overall profile')
79 self.assertEqual(prof.top_phase.subphases[0].subphases[1].name, 'baz')
80
81 def testSpanJson(self):
82 mock_trace_api = MockCloudTraceApi()
83 mock_trace_context = '1234/5678;xxxxx'
84
85 prof = profiler.Profiler(mock_trace_context, mock_trace_api)
86 with prof.Phase('foo'):
87 with prof.Phase('bar'):
88 pass
89 with prof.Phase('baz'):
90 pass
91
92 # Shouldn't this be automatic?
93 prof.current_phase.End()
94
95 self.assertEqual(prof.current_phase.name, 'overall profile')
96 self.assertEqual(prof.top_phase.subphases[0].subphases[1].name, 'baz')
97 span_json = prof.top_phase.SpanJson()
98 self.assertEqual(len(span_json), 4)
99
100 for span in span_json:
101 self.assertTrue(span['endTime'] > span['startTime'])
102
103 # pylint: disable=unbalanced-tuple-unpacking
104 span1, span2, span3, span4 = span_json
105
106 self.assertEqual(span1['name'], 'overall profile')
107 self.assertEqual(span2['name'], 'foo')
108 self.assertEqual(span3['name'], 'bar')
109 self.assertEqual(span4['name'], 'baz')
110
111 self.assertTrue(span1['startTime'] < span2['startTime'])
112 self.assertTrue(span1['startTime'] < span3['startTime'])
113 self.assertTrue(span1['startTime'] < span4['startTime'])
114
115 self.assertTrue(span1['endTime'] > span2['endTime'])
116 self.assertTrue(span1['endTime'] > span3['endTime'])
117 self.assertTrue(span1['endTime'] > span4['endTime'])
118
119
120 def testReportCloudTrace(self):
121 mock_trace_api = MockCloudTraceApi()
122 mock_trace_context = '1234/5678;xxxxx'
123
124 prof = profiler.Profiler(mock_trace_context, mock_trace_api)
125 with prof.Phase('foo'):
126 with prof.Phase('bar'):
127 pass
128 with prof.Phase('baz'):
129 pass
130
131 # Shouldn't this be automatic?
132 prof.current_phase.End()
133
134 self.assertEqual(prof.current_phase.name, 'overall profile')
135 self.assertEqual(prof.top_phase.subphases[0].subphases[1].name, 'baz')
136
137 prof.ReportTrace()
138 self.assertEqual(mock_trace_api.mock_projects.project_id, 'testing-app')