| 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 25 matching lines...) Expand all Loading... |
| 36 CompareOp op; | 36 CompareOp op; |
| 37 }; | 37 }; |
| 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 DictionaryValue* FindMatchingTraceEntry(const JsonKeyValue* key_values); |
| 47 bool FindNamePhase(const char* name, const char* phase); | 47 DictionaryValue* FindNamePhase(const char* name, const char* phase); |
| 48 DictionaryValue* 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 DictionaryValue* dict) { | 147 DictionaryValue* dict) { |
| 144 // Scan all key_values, they must all be present and equal. | 148 // Scan all key_values, they must all be present and equal. |
| 145 while (key_values && key_values->key) { | 149 while (key_values && key_values->key) { |
| 146 if (!IsKeyValueInDict(key_values, dict)) | 150 if (!IsKeyValueInDict(key_values, dict)) |
| 147 return false; | 151 return false; |
| 148 ++key_values; | 152 ++key_values; |
| 149 } | 153 } |
| 150 return true; | 154 return true; |
| 151 } | 155 } |
| 152 | 156 |
| 153 bool TraceEventTestFixture::FindMatchingTraceEntry( | 157 DictionaryValue* TraceEventTestFixture::FindMatchingTraceEntry( |
| 154 const JsonKeyValue* key_values) { | 158 const JsonKeyValue* key_values) { |
| 155 // Scan all items | 159 // Scan all items |
| 156 size_t trace_parsed_count = trace_parsed_.GetSize(); | 160 size_t trace_parsed_count = trace_parsed_.GetSize(); |
| 157 for (size_t i = 0; i < trace_parsed_count; i++) { | 161 for (size_t i = 0; i < trace_parsed_count; i++) { |
| 158 Value* value = NULL; | 162 Value* value = NULL; |
| 159 trace_parsed_.Get(i, &value); | 163 trace_parsed_.Get(i, &value); |
| 160 if (!value || value->GetType() != Value::TYPE_DICTIONARY) | 164 if (!value || value->GetType() != Value::TYPE_DICTIONARY) |
| 161 continue; | 165 continue; |
| 162 DictionaryValue* dict = static_cast<DictionaryValue*>(value); | 166 DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 163 | 167 |
| 164 if (IsAllKeyValueInDict(key_values, dict)) | 168 if (IsAllKeyValueInDict(key_values, dict)) |
| 165 return true; | 169 return dict; |
| 166 } | 170 } |
| 167 return false; | 171 return NULL; |
| 168 } | 172 } |
| 169 | 173 |
| 170 bool TraceEventTestFixture::FindNamePhase(const char* name, const char* phase) { | 174 DictionaryValue* TraceEventTestFixture::FindNamePhase(const char* name, |
| 175 const char* phase) { |
| 171 JsonKeyValue key_values[] = { | 176 JsonKeyValue key_values[] = { |
| 172 {"name", name, IS_EQUAL}, | 177 {"name", name, IS_EQUAL}, |
| 173 {"ph", phase, IS_EQUAL}, | 178 {"ph", phase, IS_EQUAL}, |
| 174 {0, 0, IS_EQUAL} | 179 {0, 0, IS_EQUAL} |
| 175 }; | 180 }; |
| 176 return FindMatchingTraceEntry(key_values); | 181 return FindMatchingTraceEntry(key_values); |
| 177 } | 182 } |
| 178 | 183 |
| 184 DictionaryValue* TraceEventTestFixture::FindNamePhaseKeyValue( |
| 185 const char* name, |
| 186 const char* phase, |
| 187 const char* key, |
| 188 const char* value) { |
| 189 JsonKeyValue key_values[] = { |
| 190 {"name", name, IS_EQUAL}, |
| 191 {"ph", phase, IS_EQUAL}, |
| 192 {key, value, IS_EQUAL}, |
| 193 {0, 0, IS_EQUAL} |
| 194 }; |
| 195 return FindMatchingTraceEntry(key_values); |
| 196 } |
| 197 |
| 179 bool TraceEventTestFixture::FindMatchingValue(const char* key, | 198 bool TraceEventTestFixture::FindMatchingValue(const char* key, |
| 180 const char* value) { | 199 const char* value) { |
| 181 JsonKeyValue key_values[] = { | 200 JsonKeyValue key_values[] = { |
| 182 {key, value, IS_EQUAL}, | 201 {key, value, IS_EQUAL}, |
| 183 {0, 0, IS_EQUAL} | 202 {0, 0, IS_EQUAL} |
| 184 }; | 203 }; |
| 185 return FindMatchingTraceEntry(key_values); | 204 return FindMatchingTraceEntry(key_values); |
| 186 } | 205 } |
| 187 | 206 |
| 188 bool TraceEventTestFixture::FindNonMatchingValue(const char* key, | 207 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); | 274 DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 256 | 275 |
| 257 if (IsStringInDict(string_to_match, dict)) | 276 if (IsStringInDict(string_to_match, dict)) |
| 258 hits.push_back(dict); | 277 hits.push_back(dict); |
| 259 } | 278 } |
| 260 return hits; | 279 return hits; |
| 261 } | 280 } |
| 262 | 281 |
| 263 void TraceWithAllMacroVariants(WaitableEvent* task_complete_event) { | 282 void TraceWithAllMacroVariants(WaitableEvent* task_complete_event) { |
| 264 { | 283 { |
| 265 TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 1122, "extrastring1"); | 284 TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 0x1122, "extrastring1"); |
| 266 TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 3344, "extrastring2"); | 285 TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 0x3344, "extrastring2"); |
| 267 TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", | 286 TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", |
| 268 5566, "extrastring3"); | 287 0x5566, "extrastring3"); |
| 269 | 288 |
| 270 TRACE_EVENT0("all", "TRACE_EVENT0 call"); | 289 TRACE_EVENT0("all", "TRACE_EVENT0 call"); |
| 271 TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); | 290 TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); |
| 272 TRACE_EVENT2("all", "TRACE_EVENT2 call", | 291 TRACE_EVENT2("all", "TRACE_EVENT2 call", |
| 273 "name1", "\"value1\"", | 292 "name1", "\"value1\"", |
| 274 "name2", "value\\2"); | 293 "name2", "value\\2"); |
| 275 | 294 |
| 276 TRACE_EVENT_INSTANT0("all", "TRACE_EVENT_INSTANT0 call"); | 295 TRACE_EVENT_INSTANT0("all", "TRACE_EVENT_INSTANT0 call"); |
| 277 TRACE_EVENT_INSTANT1("all", "TRACE_EVENT_INSTANT1 call", "name1", "value1"); | 296 TRACE_EVENT_INSTANT1("all", "TRACE_EVENT_INSTANT1 call", "name1", "value1"); |
| 278 TRACE_EVENT_INSTANT2("all", "TRACE_EVENT_INSTANT2 call", | 297 TRACE_EVENT_INSTANT2("all", "TRACE_EVENT_INSTANT2 call", |
| (...skipping 29 matching lines...) Expand all Loading... |
| 308 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); | 327 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); |
| 309 #define EXPECT_NOT_FIND_(string) \ | 328 #define EXPECT_NOT_FIND_(string) \ |
| 310 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); | 329 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); |
| 311 #define EXPECT_SUB_FIND_(string) \ | 330 #define EXPECT_SUB_FIND_(string) \ |
| 312 if (item) EXPECT_TRUE((IsStringInDict(string, item))); | 331 if (item) EXPECT_TRUE((IsStringInDict(string, item))); |
| 313 | 332 |
| 314 EXPECT_FIND_("ETW Trace Event"); | 333 EXPECT_FIND_("ETW Trace Event"); |
| 315 EXPECT_FIND_("all"); | 334 EXPECT_FIND_("all"); |
| 316 EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); | 335 EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); |
| 317 { | 336 { |
| 318 int int_val = 0; | 337 std::string str_val; |
| 319 EXPECT_TRUE(item && item->GetInteger("args.id", &int_val)); | 338 EXPECT_TRUE(item && item->GetString("args.id", &str_val)); |
| 320 EXPECT_EQ(1122, int_val); | 339 EXPECT_STREQ("1122", str_val.c_str()); |
| 321 } | 340 } |
| 322 EXPECT_SUB_FIND_("extrastring1"); | 341 EXPECT_SUB_FIND_("extrastring1"); |
| 323 EXPECT_FIND_("TRACE_EVENT_END_ETW call"); | 342 EXPECT_FIND_("TRACE_EVENT_END_ETW call"); |
| 324 EXPECT_FIND_("TRACE_EVENT_INSTANT_ETW call"); | 343 EXPECT_FIND_("TRACE_EVENT_INSTANT_ETW call"); |
| 325 EXPECT_FIND_("TRACE_EVENT0 call"); | 344 EXPECT_FIND_("TRACE_EVENT0 call"); |
| 326 { | 345 { |
| 327 std::string ph_begin; | 346 std::string ph_begin; |
| 328 std::string ph_end; | 347 std::string ph_end; |
| 329 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, "TRACE_EVENT0 call"))); | 348 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, "TRACE_EVENT0 call"))); |
| 330 EXPECT_TRUE((item && item->GetString("ph", &ph_begin))); | 349 EXPECT_TRUE((item && item->GetString("ph", &ph_begin))); |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 EXPECT_NOT_FIND_BE_("3thresholdlong2"); | 676 EXPECT_NOT_FIND_BE_("3thresholdlong2"); |
| 658 | 677 |
| 659 EXPECT_FIND_BE_("nonthreshold4"); | 678 EXPECT_FIND_BE_("nonthreshold4"); |
| 660 EXPECT_FIND_BE_("4threshold100"); | 679 EXPECT_FIND_BE_("4threshold100"); |
| 661 EXPECT_FIND_BE_("4threshold1000"); | 680 EXPECT_FIND_BE_("4threshold1000"); |
| 662 EXPECT_FIND_BE_("4threshold10000"); | 681 EXPECT_FIND_BE_("4threshold10000"); |
| 663 EXPECT_NOT_FIND_BE_("4thresholdlong1"); | 682 EXPECT_NOT_FIND_BE_("4thresholdlong1"); |
| 664 EXPECT_NOT_FIND_BE_("4thresholdlong2"); | 683 EXPECT_NOT_FIND_BE_("4thresholdlong2"); |
| 665 } | 684 } |
| 666 | 685 |
| 686 // Test Start/Finish events |
| 687 TEST_F(TraceEventTestFixture, StartFinishEvents) { |
| 688 ManualTestSetUp(); |
| 689 TraceLog::GetInstance()->SetEnabled(true); |
| 690 |
| 691 unsigned long long id = 0xfeedbeeffeedbeefull; |
| 692 TRACE_EVENT_START0( "cat", "name1", id); |
| 693 TRACE_EVENT_FINISH0("cat", "name1", id); |
| 694 TRACE_EVENT_BEGIN0( "cat", "name2"); |
| 695 TRACE_EVENT_START0( "cat", "name3", 0); |
| 696 |
| 697 TraceLog::GetInstance()->SetEnabled(false); |
| 698 |
| 699 EXPECT_TRUE(FindNamePhase("name1", "S")); |
| 700 EXPECT_TRUE(FindNamePhase("name1", "F")); |
| 701 |
| 702 std::string id_str; |
| 703 StringAppendF(&id_str, "%llx", id); |
| 704 |
| 705 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "S", "id", id_str.c_str())); |
| 706 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "F", "id", id_str.c_str())); |
| 707 EXPECT_TRUE(FindNamePhaseKeyValue("name3", "S", "id", "0")); |
| 708 |
| 709 // BEGIN events should not have id |
| 710 EXPECT_FALSE(FindNamePhaseKeyValue("name2", "B", "id", "0")); |
| 711 } |
| 712 |
| 713 // Test Start/Finish events |
| 714 TEST_F(TraceEventTestFixture, StartFinishPointerMangling) { |
| 715 ManualTestSetUp(); |
| 716 |
| 717 void* ptr = this; |
| 718 |
| 719 TraceLog::GetInstance()->SetProcessID(100); |
| 720 TraceLog::GetInstance()->SetEnabled(true); |
| 721 TRACE_EVENT_START0( "cat", "name1", ptr); |
| 722 TRACE_EVENT_START0( "cat", "name2", ptr); |
| 723 TraceLog::GetInstance()->SetEnabled(false); |
| 724 |
| 725 TraceLog::GetInstance()->SetProcessID(200); |
| 726 TraceLog::GetInstance()->SetEnabled(true); |
| 727 TRACE_EVENT_FINISH0( "cat", "name1", ptr); |
| 728 TraceLog::GetInstance()->SetEnabled(false); |
| 729 |
| 730 DictionaryValue* start = FindNamePhase("name1", "S"); |
| 731 DictionaryValue* start2 = FindNamePhase("name2", "S"); |
| 732 DictionaryValue* finish = FindNamePhase("name1", "F"); |
| 733 EXPECT_TRUE(start); |
| 734 EXPECT_TRUE(start2); |
| 735 EXPECT_TRUE(finish); |
| 736 |
| 737 Value* value = NULL; |
| 738 std::string start_id_str; |
| 739 std::string start2_id_str; |
| 740 std::string finish_id_str; |
| 741 ASSERT_TRUE(start->Get("id", &value)); |
| 742 ASSERT_TRUE(value->GetAsString(&start_id_str)); |
| 743 ASSERT_TRUE(start2->Get("id", &value)); |
| 744 ASSERT_TRUE(value->GetAsString(&start2_id_str)); |
| 745 ASSERT_TRUE(finish->Get("id", &value)); |
| 746 ASSERT_TRUE(value->GetAsString(&finish_id_str)); |
| 747 |
| 748 EXPECT_STREQ(start_id_str.c_str(), start2_id_str.c_str()); |
| 749 EXPECT_STRNE(start_id_str.c_str(), finish_id_str.c_str()); |
| 750 } |
| 751 |
| 667 // Test that static strings are not copied. | 752 // Test that static strings are not copied. |
| 668 TEST_F(TraceEventTestFixture, StaticStringVsString) { | 753 TEST_F(TraceEventTestFixture, StaticStringVsString) { |
| 669 ManualTestSetUp(); | 754 ManualTestSetUp(); |
| 670 TraceLog* tracer = TraceLog::GetInstance(); | 755 TraceLog* tracer = TraceLog::GetInstance(); |
| 671 // Make sure old events are flushed: | 756 // Make sure old events are flushed: |
| 672 tracer->SetEnabled(false); | 757 tracer->SetEnabled(false); |
| 673 EXPECT_EQ(0u, tracer->GetEventsSize()); | 758 EXPECT_EQ(0u, tracer->GetEventsSize()); |
| 674 | 759 |
| 675 { | 760 { |
| 676 tracer->SetEnabled(true); | 761 tracer->SetEnabled(true); |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 Clear(); | 1110 Clear(); |
| 1026 | 1111 |
| 1027 trace_buffer_.Start(); | 1112 trace_buffer_.Start(); |
| 1028 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); | 1113 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); |
| 1029 trace_buffer_.Finish(); | 1114 trace_buffer_.Finish(); |
| 1030 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); | 1115 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); |
| 1031 } | 1116 } |
| 1032 | 1117 |
| 1033 } // namespace debug | 1118 } // namespace debug |
| 1034 } // namespace base | 1119 } // namespace base |
| OLD | NEW |