OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 | 6 |
7 #include "vm/dart_api_impl.h" | 7 #include "vm/dart_api_impl.h" |
8 #include "vm/dart_api_state.h" | 8 #include "vm/dart_api_state.h" |
9 #include "vm/globals.h" | 9 #include "vm/globals.h" |
10 #include "vm/timeline.h" | 10 #include "vm/timeline.h" |
11 #include "vm/unit_test.h" | 11 #include "vm/unit_test.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
| 15 class TimelineTestHelper : public AllStatic { |
| 16 public: |
| 17 static void SetStream(TimelineEvent* event, TimelineStream* stream) { |
| 18 event->StreamInit(stream); |
| 19 } |
| 20 }; |
| 21 |
| 22 |
15 TEST_CASE(TimelineEventIsValid) { | 23 TEST_CASE(TimelineEventIsValid) { |
16 // Create a test stream. | 24 // Create a test stream. |
17 TimelineStream stream; | 25 TimelineStream stream; |
18 stream.Init("testStream", true); | 26 stream.Init("testStream", true); |
19 | 27 |
20 TimelineEvent event; | 28 TimelineEvent event; |
| 29 TimelineTestHelper::SetStream(&event, &stream); |
21 | 30 |
22 // Starts invalid. | 31 // Starts invalid. |
23 EXPECT(!event.IsValid()); | 32 EXPECT(!event.IsValid()); |
24 | 33 |
25 // Becomes valid. | 34 // Becomes valid. |
26 event.Instant(&stream, "hello"); | 35 event.Instant("hello"); |
27 EXPECT(event.IsValid()); | 36 EXPECT(event.IsValid()); |
28 | 37 |
29 // Becomes invalid. | 38 // Becomes invalid. |
30 event.Reset(); | 39 event.Reset(); |
31 EXPECT(!event.IsValid()); | 40 EXPECT(!event.IsValid()); |
32 } | 41 } |
33 | 42 |
34 | 43 |
35 TEST_CASE(TimelineEventDuration) { | 44 TEST_CASE(TimelineEventDuration) { |
36 // Create a test stream. | 45 // Create a test stream. |
37 TimelineStream stream; | 46 TimelineStream stream; |
38 stream.Init("testStream", true); | 47 stream.Init("testStream", true); |
39 | 48 |
40 // Create a test event. | 49 // Create a test event. |
41 TimelineEvent event; | 50 TimelineEvent event; |
42 event.DurationBegin(&stream, "apple"); | 51 TimelineTestHelper::SetStream(&event, &stream); |
| 52 event.DurationBegin("apple"); |
43 // Measure the duration. | 53 // Measure the duration. |
44 int64_t current_duration = event.TimeDuration(); | 54 int64_t current_duration = event.TimeDuration(); |
45 event.DurationEnd(); | 55 event.DurationEnd(); |
46 // Verify that duration is larger. | 56 // Verify that duration is larger. |
47 EXPECT_GE(event.TimeDuration(), current_duration); | 57 EXPECT_GE(event.TimeDuration(), current_duration); |
48 } | 58 } |
49 | 59 |
50 | 60 |
51 TEST_CASE(TimelineEventDurationPrintJSON) { | 61 TEST_CASE(TimelineEventDurationPrintJSON) { |
52 // Create a test stream. | 62 // Create a test stream. |
53 TimelineStream stream; | 63 TimelineStream stream; |
54 stream.Init("testStream", true); | 64 stream.Init("testStream", true); |
55 | 65 |
56 // Create a test event. | 66 // Create a test event. |
57 TimelineEvent event; | 67 TimelineEvent event; |
58 event.DurationBegin(&stream, "apple"); | 68 TimelineTestHelper::SetStream(&event, &stream); |
| 69 event.DurationBegin("apple"); |
59 { | 70 { |
60 // Test printing to JSON. | 71 // Test printing to JSON. |
61 JSONStream js; | 72 JSONStream js; |
62 event.PrintJSON(&js); | 73 event.PrintJSON(&js); |
63 // Check category | 74 // Check category |
64 EXPECT_SUBSTRING("\"cat\":\"testStream\"", js.ToCString()); | 75 EXPECT_SUBSTRING("\"cat\":\"testStream\"", js.ToCString()); |
65 // Check name. | 76 // Check name. |
66 EXPECT_SUBSTRING("\"name\":\"apple\"", js.ToCString()); | 77 EXPECT_SUBSTRING("\"name\":\"apple\"", js.ToCString()); |
67 // Check phase. | 78 // Check phase. |
68 EXPECT_SUBSTRING("\"ph\":\"X\"", js.ToCString()); | 79 EXPECT_SUBSTRING("\"ph\":\"X\"", js.ToCString()); |
69 // Check that ts key is present. | 80 // Check that ts key is present. |
70 EXPECT_SUBSTRING("\"ts\":", js.ToCString()); | 81 EXPECT_SUBSTRING("\"ts\":", js.ToCString()); |
71 // Check that dur key is present. | 82 // Check that dur key is present. |
72 EXPECT_SUBSTRING("\"dur\":", js.ToCString()); | 83 EXPECT_SUBSTRING("\"dur\":", js.ToCString()); |
73 } | 84 } |
74 event.DurationEnd(); | 85 event.DurationEnd(); |
75 } | 86 } |
76 | 87 |
77 | 88 |
78 TEST_CASE(TimelineEventArguments) { | 89 TEST_CASE(TimelineEventArguments) { |
79 // Create a test stream. | 90 // Create a test stream. |
80 TimelineStream stream; | 91 TimelineStream stream; |
81 stream.Init("testStream", true); | 92 stream.Init("testStream", true); |
82 | 93 |
83 // Create a test event. | 94 // Create a test event. |
84 TimelineEvent event; | 95 TimelineEvent event; |
| 96 TimelineTestHelper::SetStream(&event, &stream); |
85 | 97 |
86 // Allocate room for four arguments. | 98 // Allocate room for four arguments. |
87 event.SetNumArguments(4); | 99 event.SetNumArguments(4); |
88 // Reset. | 100 // Reset. |
89 event.Reset(); | 101 event.Reset(); |
90 | 102 |
91 event.DurationBegin(&stream, "apple"); | 103 event.DurationBegin("apple"); |
92 event.SetNumArguments(2); | 104 event.SetNumArguments(2); |
93 event.CopyArgument(0, "arg1", "value1"); | 105 event.CopyArgument(0, "arg1", "value1"); |
94 event.CopyArgument(1, "arg2", "value2"); | 106 event.CopyArgument(1, "arg2", "value2"); |
95 event.DurationEnd(); | 107 event.DurationEnd(); |
96 } | 108 } |
97 | 109 |
98 | 110 |
99 TEST_CASE(TimelineEventArgumentsPrintJSON) { | 111 TEST_CASE(TimelineEventArgumentsPrintJSON) { |
100 // Create a test stream. | 112 // Create a test stream. |
101 TimelineStream stream; | 113 TimelineStream stream; |
102 stream.Init("testStream", true); | 114 stream.Init("testStream", true); |
103 | 115 |
104 // Create a test event. | 116 // Create a test event. |
105 TimelineEvent event; | 117 TimelineEvent event; |
| 118 TimelineTestHelper::SetStream(&event, &stream); |
106 | 119 |
107 event.DurationBegin(&stream, "apple"); | 120 event.DurationBegin("apple"); |
108 event.SetNumArguments(2); | 121 event.SetNumArguments(2); |
109 event.CopyArgument(0, "arg1", "value1"); | 122 event.CopyArgument(0, "arg1", "value1"); |
110 event.CopyArgument(1, "arg2", "value2"); | 123 event.CopyArgument(1, "arg2", "value2"); |
111 event.DurationEnd(); | 124 event.DurationEnd(); |
112 | 125 |
113 { | 126 { |
114 // Test printing to JSON. | 127 // Test printing to JSON. |
115 JSONStream js; | 128 JSONStream js; |
116 event.PrintJSON(&js); | 129 event.PrintJSON(&js); |
117 | 130 |
118 // Check both arguments. | 131 // Check both arguments. |
119 EXPECT_SUBSTRING("\"arg1\":\"value1\"", js.ToCString()); | 132 EXPECT_SUBSTRING("\"arg1\":\"value1\"", js.ToCString()); |
120 EXPECT_SUBSTRING("\"arg2\":\"value2\"", js.ToCString()); | 133 EXPECT_SUBSTRING("\"arg2\":\"value2\"", js.ToCString()); |
121 } | 134 } |
122 } | 135 } |
123 | 136 |
124 | 137 |
125 TEST_CASE(TimelineEventBufferPrintJSON) { | 138 TEST_CASE(TimelineEventBufferPrintJSON) { |
126 Isolate* isolate = Isolate::Current(); | 139 Isolate* isolate = Isolate::Current(); |
127 TimelineEventBuffer* buffer = isolate->timeline_event_buffer(); | 140 TimelineEventRecorder* recorder = isolate->timeline_event_recorder(); |
128 JSONStream js; | 141 JSONStream js; |
129 buffer->PrintJSON(&js); | 142 recorder->PrintJSON(&js); |
130 // Check the type. This test will fail if we ever make Timeline public. | 143 // Check the type. This test will fail if we ever make Timeline public. |
131 EXPECT_SUBSTRING("\"type\":\"_Timeline\"", js.ToCString()); | 144 EXPECT_SUBSTRING("\"type\":\"_Timeline\"", js.ToCString()); |
132 // Check that there is a traceEvents array. | 145 // Check that there is a traceEvents array. |
133 EXPECT_SUBSTRING("\"traceEvents\":[", js.ToCString()); | 146 EXPECT_SUBSTRING("\"traceEvents\":[", js.ToCString()); |
134 } | 147 } |
135 | 148 |
| 149 |
| 150 // Count the number of each event type seen. |
| 151 class EventCounterRecorder : public TimelineEventStreamingRecorder { |
| 152 public: |
| 153 EventCounterRecorder() { |
| 154 for (intptr_t i = 0; i < TimelineEvent::kNumEventTypes; i++) { |
| 155 counts_[i] = 0; |
| 156 } |
| 157 } |
| 158 |
| 159 void StreamEvent(TimelineEvent* event) { |
| 160 counts_[event->event_type()]++; |
| 161 } |
| 162 |
| 163 intptr_t CountFor(TimelineEvent::EventType type) { |
| 164 return counts_[type]; |
| 165 } |
| 166 |
| 167 private: |
| 168 intptr_t counts_[TimelineEvent::kNumEventTypes]; |
| 169 }; |
| 170 |
| 171 |
| 172 TEST_CASE(TimelineEventStreamingRecorderBasic) { |
| 173 EventCounterRecorder* recorder = new EventCounterRecorder(); |
| 174 |
| 175 // Initial counts are all zero. |
| 176 for (intptr_t i = TimelineEvent::kNone + 1; |
| 177 i < TimelineEvent::kNumEventTypes; |
| 178 i++) { |
| 179 EXPECT_EQ(0, recorder->CountFor(static_cast<TimelineEvent::EventType>(i))); |
| 180 } |
| 181 |
| 182 // Create a test stream. |
| 183 TimelineStream stream; |
| 184 stream.Init("testStream", true); |
| 185 stream.set_recorder(recorder); |
| 186 |
| 187 TimelineEvent* event = NULL; |
| 188 |
| 189 event = stream.StartEvent(); |
| 190 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kDuration)); |
| 191 event->DurationBegin("cabbage"); |
| 192 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kDuration)); |
| 193 event->DurationEnd(); |
| 194 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kDuration)); |
| 195 event->Complete(); |
| 196 EXPECT_EQ(1, recorder->CountFor(TimelineEvent::kDuration)); |
| 197 |
| 198 event = stream.StartEvent(); |
| 199 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kInstant)); |
| 200 event->Instant("instantCabbage"); |
| 201 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kInstant)); |
| 202 event->Complete(); |
| 203 EXPECT_EQ(1, recorder->CountFor(TimelineEvent::kInstant)); |
| 204 |
| 205 event = stream.StartEvent(); |
| 206 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncBegin)); |
| 207 int64_t async_id = event->AsyncBegin("asyncBeginCabbage"); |
| 208 EXPECT(async_id >= 0); |
| 209 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncBegin)); |
| 210 event->Complete(); |
| 211 EXPECT_EQ(1, recorder->CountFor(TimelineEvent::kAsyncBegin)); |
| 212 |
| 213 event = stream.StartEvent(); |
| 214 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncInstant)); |
| 215 event->AsyncInstant("asyncInstantCabbage", async_id); |
| 216 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncInstant)); |
| 217 event->Complete(); |
| 218 EXPECT_EQ(1, recorder->CountFor(TimelineEvent::kAsyncInstant)); |
| 219 |
| 220 event = stream.StartEvent(); |
| 221 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncEnd)); |
| 222 event->AsyncEnd("asyncEndCabbage", async_id); |
| 223 EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncEnd)); |
| 224 event->Complete(); |
| 225 EXPECT_EQ(1, recorder->CountFor(TimelineEvent::kAsyncEnd)); |
| 226 } |
| 227 |
136 } // namespace dart | 228 } // namespace dart |
OLD | NEW |