| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 class TraceEventTestFixture : public testing::Test { | 39 class TraceEventTestFixture : public testing::Test { |
| 40 public: | 40 public: |
| 41 // This fixture does not use SetUp() because the fixture must be manually set | 41 // This fixture does not use SetUp() because the fixture must be manually set |
| 42 // up multiple times when testing AtExit. Use ManualTestSetUp for this. | 42 // up multiple times when testing AtExit. Use ManualTestSetUp for this. |
| 43 void ManualTestSetUp(); | 43 void ManualTestSetUp(); |
| 44 void OnTraceDataCollected( | 44 void OnTraceDataCollected( |
| 45 scoped_refptr<TraceLog::RefCountedString> events_str); | 45 scoped_refptr<TraceLog::RefCountedString> events_str); |
| 46 bool FindMatchingTraceEntry(const JsonKeyValue* key_values); | 46 bool FindMatchingTraceEntry(const JsonKeyValue* key_values); |
| 47 bool FindNamePhase(const char* name, const char* phase); | 47 bool FindNamePhase(const char* name, const char* phase); |
| 48 bool FindNamePhaseKeyValue(const char* name, |
| 49 const char* phase, |
| 50 const char* key, |
| 51 const char* value); |
| 48 bool FindMatchingValue(const char* key, | 52 bool FindMatchingValue(const char* key, |
| 49 const char* value); | 53 const char* value); |
| 50 bool FindNonMatchingValue(const char* key, | 54 bool FindNonMatchingValue(const char* key, |
| 51 const char* value); | 55 const char* value); |
| 52 void Clear() { | 56 void Clear() { |
| 53 trace_parsed_.Clear(); | 57 trace_parsed_.Clear(); |
| 54 json_output_.json_output.clear(); | 58 json_output_.json_output.clear(); |
| 55 } | 59 } |
| 56 | 60 |
| 57 virtual void SetUp() { | 61 virtual void SetUp() { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 | 173 |
| 170 bool TraceEventTestFixture::FindNamePhase(const char* name, const char* phase) { | 174 bool TraceEventTestFixture::FindNamePhase(const char* name, const char* phase) { |
| 171 JsonKeyValue key_values[] = { | 175 JsonKeyValue key_values[] = { |
| 172 {"name", name, IS_EQUAL}, | 176 {"name", name, IS_EQUAL}, |
| 173 {"ph", phase, IS_EQUAL}, | 177 {"ph", phase, IS_EQUAL}, |
| 174 {0, 0, IS_EQUAL} | 178 {0, 0, IS_EQUAL} |
| 175 }; | 179 }; |
| 176 return FindMatchingTraceEntry(key_values); | 180 return FindMatchingTraceEntry(key_values); |
| 177 } | 181 } |
| 178 | 182 |
| 183 bool TraceEventTestFixture::FindNamePhaseKeyValue(const char* name, |
| 184 const char* phase, |
| 185 const char* key, |
| 186 const char* value) { |
| 187 JsonKeyValue key_values[] = { |
| 188 {"name", name, IS_EQUAL}, |
| 189 {"ph", phase, IS_EQUAL}, |
| 190 {key, value, IS_EQUAL}, |
| 191 {0, 0, IS_EQUAL} |
| 192 }; |
| 193 return FindMatchingTraceEntry(key_values); |
| 194 } |
| 195 |
| 179 bool TraceEventTestFixture::FindMatchingValue(const char* key, | 196 bool TraceEventTestFixture::FindMatchingValue(const char* key, |
| 180 const char* value) { | 197 const char* value) { |
| 181 JsonKeyValue key_values[] = { | 198 JsonKeyValue key_values[] = { |
| 182 {key, value, IS_EQUAL}, | 199 {key, value, IS_EQUAL}, |
| 183 {0, 0, IS_EQUAL} | 200 {0, 0, IS_EQUAL} |
| 184 }; | 201 }; |
| 185 return FindMatchingTraceEntry(key_values); | 202 return FindMatchingTraceEntry(key_values); |
| 186 } | 203 } |
| 187 | 204 |
| 188 bool TraceEventTestFixture::FindNonMatchingValue(const char* key, | 205 bool TraceEventTestFixture::FindNonMatchingValue(const char* key, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 DictionaryValue* dict = static_cast<DictionaryValue*>(value); | 272 DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 256 | 273 |
| 257 if (IsStringInDict(string_to_match, dict)) | 274 if (IsStringInDict(string_to_match, dict)) |
| 258 hits.push_back(dict); | 275 hits.push_back(dict); |
| 259 } | 276 } |
| 260 return hits; | 277 return hits; |
| 261 } | 278 } |
| 262 | 279 |
| 263 void TraceWithAllMacroVariants(WaitableEvent* task_complete_event) { | 280 void TraceWithAllMacroVariants(WaitableEvent* task_complete_event) { |
| 264 { | 281 { |
| 265 TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 1122, "extrastring1"); | 282 TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 0x1122, "extrastring1"); |
| 266 TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 3344, "extrastring2"); | 283 TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 0x3344, "extrastring2"); |
| 267 TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", | 284 TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", |
| 268 5566, "extrastring3"); | 285 0x5566, "extrastring3"); |
| 269 | 286 |
| 270 TRACE_EVENT0("all", "TRACE_EVENT0 call"); | 287 TRACE_EVENT0("all", "TRACE_EVENT0 call"); |
| 271 TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); | 288 TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); |
| 272 TRACE_EVENT2("all", "TRACE_EVENT2 call", | 289 TRACE_EVENT2("all", "TRACE_EVENT2 call", |
| 273 "name1", "\"value1\"", | 290 "name1", "\"value1\"", |
| 274 "name2", "value\\2"); | 291 "name2", "value\\2"); |
| 275 | 292 |
| 276 TRACE_EVENT_INSTANT0("all", "TRACE_EVENT_INSTANT0 call"); | 293 TRACE_EVENT_INSTANT0("all", "TRACE_EVENT_INSTANT0 call"); |
| 277 TRACE_EVENT_INSTANT1("all", "TRACE_EVENT_INSTANT1 call", "name1", "value1"); | 294 TRACE_EVENT_INSTANT1("all", "TRACE_EVENT_INSTANT1 call", "name1", "value1"); |
| 278 TRACE_EVENT_INSTANT2("all", "TRACE_EVENT_INSTANT2 call", | 295 TRACE_EVENT_INSTANT2("all", "TRACE_EVENT_INSTANT2 call", |
| (...skipping 24 matching lines...) Expand all Loading... |
| 303 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); | 320 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); |
| 304 #define EXPECT_NOT_FIND_(string) \ | 321 #define EXPECT_NOT_FIND_(string) \ |
| 305 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); | 322 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); |
| 306 #define EXPECT_SUB_FIND_(string) \ | 323 #define EXPECT_SUB_FIND_(string) \ |
| 307 if (item) EXPECT_TRUE((IsStringInDict(string, item))); | 324 if (item) EXPECT_TRUE((IsStringInDict(string, item))); |
| 308 | 325 |
| 309 EXPECT_FIND_("ETW Trace Event"); | 326 EXPECT_FIND_("ETW Trace Event"); |
| 310 EXPECT_FIND_("all"); | 327 EXPECT_FIND_("all"); |
| 311 EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); | 328 EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); |
| 312 { | 329 { |
| 313 int int_val = 0; | 330 std::string str_val; |
| 314 EXPECT_TRUE(item && item->GetInteger("args.id", &int_val)); | 331 EXPECT_TRUE(item && item->GetString("args.id", &str_val)); |
| 315 EXPECT_EQ(1122, int_val); | 332 EXPECT_STREQ("1122", str_val.c_str()); |
| 316 } | 333 } |
| 317 EXPECT_SUB_FIND_("extrastring1"); | 334 EXPECT_SUB_FIND_("extrastring1"); |
| 318 EXPECT_FIND_("TRACE_EVENT_END_ETW call"); | 335 EXPECT_FIND_("TRACE_EVENT_END_ETW call"); |
| 319 EXPECT_FIND_("TRACE_EVENT_INSTANT_ETW call"); | 336 EXPECT_FIND_("TRACE_EVENT_INSTANT_ETW call"); |
| 320 EXPECT_FIND_("TRACE_EVENT0 call"); | 337 EXPECT_FIND_("TRACE_EVENT0 call"); |
| 321 { | 338 { |
| 322 std::string ph_begin; | 339 std::string ph_begin; |
| 323 std::string ph_end; | 340 std::string ph_end; |
| 324 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, "TRACE_EVENT0 call"))); | 341 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, "TRACE_EVENT0 call"))); |
| 325 EXPECT_TRUE((item && item->GetString("ph", &ph_begin))); | 342 EXPECT_TRUE((item && item->GetString("ph", &ph_begin))); |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 EXPECT_NOT_FIND_BE_("3thresholdlong2"); | 643 EXPECT_NOT_FIND_BE_("3thresholdlong2"); |
| 627 | 644 |
| 628 EXPECT_FIND_BE_("nonthreshold4"); | 645 EXPECT_FIND_BE_("nonthreshold4"); |
| 629 EXPECT_FIND_BE_("4threshold100"); | 646 EXPECT_FIND_BE_("4threshold100"); |
| 630 EXPECT_FIND_BE_("4threshold1000"); | 647 EXPECT_FIND_BE_("4threshold1000"); |
| 631 EXPECT_FIND_BE_("4threshold10000"); | 648 EXPECT_FIND_BE_("4threshold10000"); |
| 632 EXPECT_NOT_FIND_BE_("4thresholdlong1"); | 649 EXPECT_NOT_FIND_BE_("4thresholdlong1"); |
| 633 EXPECT_NOT_FIND_BE_("4thresholdlong2"); | 650 EXPECT_NOT_FIND_BE_("4thresholdlong2"); |
| 634 } | 651 } |
| 635 | 652 |
| 653 // Test Start/Finish events |
| 654 TEST_F(TraceEventTestFixture, StartFinishEvents) { |
| 655 ManualTestSetUp(); |
| 656 TraceLog::GetInstance()->SetEnabled(true); |
| 657 |
| 658 unsigned long long id = 0xfeedbeeffeedbeefull; |
| 659 TRACE_EVENT_START0( "cat", "name1", id); |
| 660 TRACE_EVENT_FINISH0("cat", "name1", id); |
| 661 TRACE_EVENT_BEGIN0( "cat", "name2"); |
| 662 TRACE_EVENT_START0( "cat", "name3", 0); |
| 663 |
| 664 TraceLog::GetInstance()->SetEnabled(false); |
| 665 |
| 666 EXPECT_TRUE(FindNamePhase("name1", "S")); |
| 667 EXPECT_TRUE(FindNamePhase("name1", "F")); |
| 668 |
| 669 std::string id_str; |
| 670 StringAppendF(&id_str, "%llx", id); |
| 671 |
| 672 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "S", "ext", id_str.c_str())); |
| 673 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "F", "ext", id_str.c_str())); |
| 674 |
| 675 // ext parameter should not be present when zero: |
| 676 EXPECT_FALSE(FindNamePhaseKeyValue("name2", "B", "ext", "0")); |
| 677 EXPECT_FALSE(FindNamePhaseKeyValue("name3", "S", "ext", "0")); |
| 678 } |
| 679 |
| 636 // Test that static strings are not copied. | 680 // Test that static strings are not copied. |
| 637 TEST_F(TraceEventTestFixture, StaticStringVsString) { | 681 TEST_F(TraceEventTestFixture, StaticStringVsString) { |
| 638 ManualTestSetUp(); | 682 ManualTestSetUp(); |
| 639 TraceLog* tracer = TraceLog::GetInstance(); | 683 TraceLog* tracer = TraceLog::GetInstance(); |
| 640 // Make sure old events are flushed: | 684 // Make sure old events are flushed: |
| 641 tracer->SetEnabled(false); | 685 tracer->SetEnabled(false); |
| 642 EXPECT_EQ(0u, tracer->GetEventsSize()); | 686 EXPECT_EQ(0u, tracer->GetEventsSize()); |
| 643 | 687 |
| 644 { | 688 { |
| 645 tracer->SetEnabled(true); | 689 tracer->SetEnabled(true); |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 994 Clear(); | 1038 Clear(); |
| 995 | 1039 |
| 996 trace_buffer_.Start(); | 1040 trace_buffer_.Start(); |
| 997 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); | 1041 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); |
| 998 trace_buffer_.Finish(); | 1042 trace_buffer_.Finish(); |
| 999 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); | 1043 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); |
| 1000 } | 1044 } |
| 1001 | 1045 |
| 1002 } // namespace debug | 1046 } // namespace debug |
| 1003 } // namespace base | 1047 } // namespace base |
| OLD | NEW |