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