1 // Copyright 2015 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.
8 #include "base/time/time.h"
9 #include "base/trace_event/trace_event_argument.h"
10 #include "base/values.h"
11 #include "cc/debug/frame_timing_tracker.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 std::string
CompositeToString(
18 scoped_ptr
<FrameTimingTracker::CompositeTimingSet
> timingset
) {
19 scoped_refptr
<base::trace_event::TracedValue
> value
=
20 new base::trace_event::TracedValue();
21 value
->BeginArray("values");
22 std::set
<int> rect_ids
;
23 for (const auto& pair
: *timingset
)
24 rect_ids
.insert(pair
.first
);
26 for (const auto& rect_id
: rect_ids
) {
27 auto& events
= (*timingset
)[rect_id
];
28 value
->BeginDictionary();
29 value
->SetInteger("rect_id", rect_id
);
30 value
->BeginArray("events");
31 for (const auto& event
: events
) {
32 value
->BeginDictionary();
33 value
->SetInteger("frame_id", event
.frame_id
);
34 value
->SetInteger("timestamp", event
.timestamp
.ToInternalValue());
35 value
->EndDictionary();
38 value
->EndDictionary();
41 return value
->ToString();
44 std::string
MainFrameToString(
45 scoped_ptr
<FrameTimingTracker::MainFrameTimingSet
> timingset
) {
46 scoped_refptr
<base::trace_event::TracedValue
> value
=
47 new base::trace_event::TracedValue();
48 value
->BeginArray("values");
49 std::set
<int> rect_ids
;
50 for (const auto& pair
: *timingset
)
51 rect_ids
.insert(pair
.first
);
53 for (const auto& rect_id
: rect_ids
) {
54 auto& events
= (*timingset
)[rect_id
];
55 value
->BeginDictionary();
56 value
->SetInteger("rect_id", rect_id
);
57 value
->BeginArray("events");
58 for (const auto& event
: events
) {
59 value
->BeginDictionary();
60 value
->SetInteger("end_time", event
.end_time
.ToInternalValue());
61 value
->SetInteger("frame_id", event
.frame_id
);
62 value
->SetInteger("timestamp", event
.timestamp
.ToInternalValue());
63 value
->EndDictionary();
66 value
->EndDictionary();
69 return value
->ToString();
72 TEST(FrameTimingTrackerTest
, DefaultTrackerIsEmpty
) {
73 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
74 EXPECT_EQ("{\"values\":[]}",
75 CompositeToString(tracker
->GroupCompositeCountsByRectId()));
76 EXPECT_EQ("{\"values\":[]}",
77 MainFrameToString(tracker
->GroupMainFrameCountsByRectId()));
80 TEST(FrameTimingTrackerTest
, NoFrameIdsIsEmpty
) {
81 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
82 std::vector
<std::pair
<int, int64_t>> ids
;
83 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids
);
84 EXPECT_EQ("{\"values\":[]}",
85 CompositeToString(tracker
->GroupCompositeCountsByRectId()));
88 TEST(FrameTimingTrackerTest
, NoRectIdsYieldsNoMainFrameEvents
) {
89 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
90 tracker
->SaveMainFrameTimeStamps(std::vector
<int64_t>(),
91 base::TimeTicks::FromInternalValue(100),
92 base::TimeTicks::FromInternalValue(110), 1);
93 EXPECT_EQ("{\"values\":[]}",
94 MainFrameToString(tracker
->GroupMainFrameCountsByRectId()));
97 TEST(FrameTimingTrackerTest
, OneFrameId
) {
98 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
99 std::vector
<std::pair
<int, int64_t>> ids
;
100 ids
.push_back(std::make_pair(1, 2));
101 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids
);
103 "{\"values\":[{\"events\":["
104 "{\"frame_id\":1,\"timestamp\":100}],\"rect_id\":2}]}",
105 CompositeToString(tracker
->GroupCompositeCountsByRectId()));
108 TEST(FrameTimingTrackerTest
, OneMainFrameRect
) {
109 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
110 std::vector
<int64_t> rect_ids
;
111 rect_ids
.push_back(1);
112 tracker
->SaveMainFrameTimeStamps(rect_ids
,
113 base::TimeTicks::FromInternalValue(100),
114 base::TimeTicks::FromInternalValue(110), 2);
116 "{\"values\":[{\"events\":["
117 "{\"end_time\":110,\"frame_id\":2,\"timestamp\":100}],\"rect_id\":1}]}",
118 MainFrameToString(tracker
->GroupMainFrameCountsByRectId()));
121 TEST(FrameTimingTrackerTest
, UnsortedTimestampsIds
) {
122 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
123 std::vector
<std::pair
<int, int64_t>> ids
;
124 ids
.push_back(std::make_pair(1, 2));
125 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(200), ids
);
126 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(400), ids
);
127 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids
);
129 "{\"values\":[{\"events\":["
130 "{\"frame_id\":1,\"timestamp\":100},"
131 "{\"frame_id\":1,\"timestamp\":200},"
132 "{\"frame_id\":1,\"timestamp\":400}],\"rect_id\":2}]}",
133 CompositeToString(tracker
->GroupCompositeCountsByRectId()));
136 TEST(FrameTimingTrackerTest
, MainFrameUnsortedTimestamps
) {
137 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
138 std::vector
<int64_t> rect_ids
;
139 rect_ids
.push_back(2);
140 tracker
->SaveMainFrameTimeStamps(rect_ids
,
141 base::TimeTicks::FromInternalValue(200),
142 base::TimeTicks::FromInternalValue(280), 1);
143 tracker
->SaveMainFrameTimeStamps(rect_ids
,
144 base::TimeTicks::FromInternalValue(400),
145 base::TimeTicks::FromInternalValue(470), 1);
146 tracker
->SaveMainFrameTimeStamps(rect_ids
,
147 base::TimeTicks::FromInternalValue(100),
148 base::TimeTicks::FromInternalValue(160), 1);
150 "{\"values\":[{\"events\":["
151 "{\"end_time\":160,\"frame_id\":1,\"timestamp\":100},"
152 "{\"end_time\":280,\"frame_id\":1,\"timestamp\":200},"
153 "{\"end_time\":470,\"frame_id\":1,\"timestamp\":400}],\"rect_id\":2}]}",
154 MainFrameToString(tracker
->GroupMainFrameCountsByRectId()));
157 TEST(FrameTimingTrackerTest
, MultipleFrameIds
) {
158 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
160 std::vector
<std::pair
<int, int64_t>> ids200
;
161 ids200
.push_back(std::make_pair(1, 2));
162 ids200
.push_back(std::make_pair(1, 3));
163 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(200), ids200
);
165 std::vector
<std::pair
<int, int64_t>> ids400
;
166 ids400
.push_back(std::make_pair(2, 2));
167 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(400), ids400
);
169 std::vector
<std::pair
<int, int64_t>> ids100
;
170 ids100
.push_back(std::make_pair(3, 2));
171 ids100
.push_back(std::make_pair(2, 3));
172 ids100
.push_back(std::make_pair(3, 4));
173 tracker
->SaveTimeStamps(base::TimeTicks::FromInternalValue(100), ids100
);
176 "{\"values\":[{\"events\":["
177 "{\"frame_id\":3,\"timestamp\":100},"
178 "{\"frame_id\":1,\"timestamp\":200},"
179 "{\"frame_id\":2,\"timestamp\":400}],\"rect_id\":2},"
181 "{\"frame_id\":2,\"timestamp\":100},"
182 "{\"frame_id\":1,\"timestamp\":200}],\"rect_id\":3},"
184 "{\"frame_id\":3,\"timestamp\":100}],\"rect_id\":4}"
186 CompositeToString(tracker
->GroupCompositeCountsByRectId()));
189 TEST(FrameTimingTrackerTest
, MultipleMainFrameEvents
) {
190 scoped_ptr
<FrameTimingTracker
> tracker(FrameTimingTracker::Create());
192 std::vector
<int64_t> rect_ids200
;
193 rect_ids200
.push_back(2);
194 rect_ids200
.push_back(3);
195 tracker
->SaveMainFrameTimeStamps(rect_ids200
,
196 base::TimeTicks::FromInternalValue(200),
197 base::TimeTicks::FromInternalValue(220), 1);
199 std::vector
<int64_t> rect_ids400
;
200 rect_ids400
.push_back(2);
201 tracker
->SaveMainFrameTimeStamps(rect_ids400
,
202 base::TimeTicks::FromInternalValue(400),
203 base::TimeTicks::FromInternalValue(440), 2);
205 std::vector
<int64_t> rect_ids100
;
206 rect_ids100
.push_back(2);
207 rect_ids100
.push_back(3);
208 rect_ids100
.push_back(4);
209 tracker
->SaveMainFrameTimeStamps(rect_ids100
,
210 base::TimeTicks::FromInternalValue(100),
211 base::TimeTicks::FromInternalValue(110), 3);
214 "{\"values\":[{\"events\":["
215 "{\"end_time\":110,\"frame_id\":3,\"timestamp\":100},"
216 "{\"end_time\":220,\"frame_id\":1,\"timestamp\":200},"
217 "{\"end_time\":440,\"frame_id\":2,\"timestamp\":400}],\"rect_id\":2},"
219 "{\"end_time\":110,\"frame_id\":3,\"timestamp\":100},"
220 "{\"end_time\":220,\"frame_id\":1,\"timestamp\":200}],\"rect_id\":3},"
222 "{\"end_time\":110,\"frame_id\":3,\"timestamp\":100}],\"rect_id\":4}"
224 MainFrameToString(tracker
->GroupMainFrameCountsByRectId()));