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.
7 from telemetry
.timeline
import async_slice
8 from telemetry
.timeline
import model
as model_module
9 from telemetry
.timeline
import slice as slice_module
10 from telemetry
.web_perf
import timeline_interaction_record
as tir_module
13 class ParseTests(unittest
.TestCase
):
16 self
.assertTrue(tir_module
.IsTimelineInteractionRecord(
18 self
.assertTrue(tir_module
.IsTimelineInteractionRecord(
19 'Interaction.Foo/Bar'))
20 self
.assertFalse(tir_module
.IsTimelineInteractionRecord(
24 class TimelineInteractionRecordTests(unittest
.TestCase
):
26 def CreateSimpleRecordWithName(self
, event_name
):
27 s
= async_slice
.AsyncSlice(
29 timestamp
=0, duration
=200, thread_start
=20, thread_duration
=100)
30 return tir_module
.TimelineInteractionRecord
.FromAsyncEvent(s
)
32 def CreateTestSliceFromTimeRanges(
33 self
, parent_thread
, time_start
, time_end
, thread_start
, thread_end
):
34 duration
= time_end
- time_start
35 thread_duration
= thread_end
- thread_start
36 return slice_module
.Slice(parent_thread
, 'Test', 'foo', time_start
,
37 duration
, thread_start
, thread_duration
)
40 r
= self
.CreateSimpleRecordWithName('Interaction.LogicalName')
41 self
.assertEquals('LogicalName', r
.label
)
42 self
.assertEquals(False, r
.repeatable
)
44 r
= self
.CreateSimpleRecordWithName('Interaction.LogicalName/repeatable')
45 self
.assertEquals('LogicalName', r
.label
)
46 self
.assertEquals(True, r
.repeatable
)
48 r
= self
.CreateSimpleRecordWithName(
49 'Interaction.LogicalNameWith/Slash/repeatable')
50 self
.assertEquals('LogicalNameWith/Slash', r
.label
)
51 self
.assertEquals(True, r
.repeatable
)
53 def testGetJavaScriptMarker(self
):
54 repeatable_marker
= tir_module
.GetJavaScriptMarker(
55 'MyLabel', [tir_module
.REPEATABLE
])
56 self
.assertEquals('Interaction.MyLabel/repeatable', repeatable_marker
)
58 def testGetOverlappedThreadTimeForSliceInSameThread(self
):
59 # Create a renderer thread.
60 model
= model_module
.TimelineModel()
61 renderer_main
= model
.GetOrCreateProcess(1).GetOrCreateThread(2)
62 model
.FinalizeImport()
64 # Make a record that starts at 30ms and ends at 60ms in thread time.
65 s
= async_slice
.AsyncSlice(
66 'cat', 'Interaction.Test',
67 timestamp
=0, duration
=200, start_thread
=renderer_main
,
68 end_thread
=renderer_main
, thread_start
=30, thread_duration
=30)
69 record
= tir_module
.TimelineInteractionRecord
.FromAsyncEvent(s
)
71 # Non overlapped range on the left of event.
72 s1
= self
.CreateTestSliceFromTimeRanges(renderer_main
, 0, 100, 10, 20)
73 self
.assertEquals(0, record
.GetOverlappedThreadTimeForSlice(s1
))
75 # Non overlapped range on the right of event.
76 s2
= self
.CreateTestSliceFromTimeRanges(renderer_main
, 0, 100, 70, 90)
77 self
.assertEquals(0, record
.GetOverlappedThreadTimeForSlice(s2
))
79 # Overlapped range on the left of event.
80 s3
= self
.CreateTestSliceFromTimeRanges(renderer_main
, 0, 100, 20, 50)
81 self
.assertEquals(20, record
.GetOverlappedThreadTimeForSlice(s3
))
83 # Overlapped range in the middle of event.
84 s4
= self
.CreateTestSliceFromTimeRanges(renderer_main
, 0, 100, 40, 50)
85 self
.assertEquals(10, record
.GetOverlappedThreadTimeForSlice(s4
))
87 # Overlapped range on the left of event.
88 s5
= self
.CreateTestSliceFromTimeRanges(renderer_main
, 0, 100, 50, 90)
89 self
.assertEquals(10, record
.GetOverlappedThreadTimeForSlice(s5
))
92 # Create a renderer thread.
93 model
= model_module
.TimelineModel()
94 renderer_main
= model
.GetOrCreateProcess(1).GetOrCreateThread(2)
95 model
.FinalizeImport()
97 s
= async_slice
.AsyncSlice(
98 'cat', 'Interaction.Test/repeatable',
99 timestamp
=0, duration
=200, start_thread
=renderer_main
,
100 end_thread
=renderer_main
, thread_start
=30, thread_duration
=30)
101 record
= tir_module
.TimelineInteractionRecord
.FromAsyncEvent(s
)
103 'TimelineInteractionRecord(label=\'Test\', '
104 'start=0.000000, end=200.000000, flags=repeatable, '
105 'async_event=TimelineEvent(name=\'Interaction.Test/repeatable\','
106 ' start=0.000000, duration=200, thread_start=30, thread_duration=30))')
107 self
.assertEquals(expected_repr
, repr(record
))
110 def testGetOverlappedThreadTimeForSliceInDifferentThread(self
):
111 # Create a renderer thread and another thread.
112 model
= model_module
.TimelineModel()
113 renderer_main
= model
.GetOrCreateProcess(1).GetOrCreateThread(2)
114 another_thread
= model
.GetOrCreateProcess(1).GetOrCreateThread(3)
115 model
.FinalizeImport()
117 # Make a record that starts at 50ms and ends at 150ms in wall time, and is
118 # scheduled 75% of the time (hence thread_duration = 100ms*75% = 75ms).
119 s
= async_slice
.AsyncSlice(
120 'cat', 'Interaction.Test',
121 timestamp
=50, duration
=100, start_thread
=renderer_main
,
122 end_thread
=renderer_main
, thread_start
=55, thread_duration
=75)
123 record
= tir_module
.TimelineInteractionRecord
.FromAsyncEvent(s
)
125 # Non overlapped range on the left of event.
126 s1
= self
.CreateTestSliceFromTimeRanges(another_thread
, 25, 40, 28, 30)
127 self
.assertEquals(0, record
.GetOverlappedThreadTimeForSlice(s1
))
129 # Non overlapped range on the right of event.
130 s2
= self
.CreateTestSliceFromTimeRanges(another_thread
, 200, 300, 270, 290)
131 self
.assertEquals(0, record
.GetOverlappedThreadTimeForSlice(s2
))
133 # Overlapped range on the left of event, and slice is scheduled 50% of the
135 # The overlapped wall-time duration is 50ms.
136 # The overlapped thread-time duration is 50ms * 75% * 50% = 18.75
137 s3
= self
.CreateTestSliceFromTimeRanges(another_thread
, 0, 100, 20, 70)
138 self
.assertEquals(18.75, record
.GetOverlappedThreadTimeForSlice(s3
))
140 # Overlapped range in the middle of event, and slice is scheduled 20% of the
142 # The overlapped wall-time duration is 40ms.
143 # The overlapped thread-time duration is 40ms * 75% * 20% = 6
144 s4
= self
.CreateTestSliceFromTimeRanges(another_thread
, 100, 140, 120, 128)
145 self
.assertEquals(6, record
.GetOverlappedThreadTimeForSlice(s4
))
147 # Overlapped range on the left of event, and slice is scheduled 100% of the
149 # The overlapped wall-time duration is 32ms.
150 # The overlapped thread-time duration is 32ms * 75% * 100% = 24
151 s5
= self
.CreateTestSliceFromTimeRanges(another_thread
, 118, 170, 118, 170)
152 self
.assertEquals(24, record
.GetOverlappedThreadTimeForSlice(s5
))