Chromium Code Reviews| 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/debug/trace_event_test_utils.h" | |
| 9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/process_util.h" | 14 #include "base/process_util.h" |
| 14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 17 #include "base/values.h" | 18 #include "base/values.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 399 ManualTestSetUp(); | 400 ManualTestSetUp(); |
| 400 TraceLog::GetInstance()->SetEnabled(true); | 401 TraceLog::GetInstance()->SetEnabled(true); |
| 401 | 402 |
| 402 TraceWithAllMacroVariants(NULL); | 403 TraceWithAllMacroVariants(NULL); |
| 403 | 404 |
| 404 TraceLog::GetInstance()->SetEnabled(false); | 405 TraceLog::GetInstance()->SetEnabled(false); |
| 405 | 406 |
| 406 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); | 407 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); |
| 407 } | 408 } |
| 408 | 409 |
| 410 // Test that the TraceAnalyzer works. | |
| 411 TEST_F(TraceEventTestFixture, TraceAnalyzerBasic) { | |
| 412 using namespace base::debug::trace; | |
| 413 ManualTestSetUp(); | |
| 414 | |
| 415 Clear(); | |
| 416 TraceLog::GetInstance()->SetEnabled(true); | |
| 417 { | |
| 418 TRACE_EVENT_END0("cat3", "name7"); | |
| 419 TRACE_EVENT2("cat1", "name1", "arg1", 1111, "arg2", "string1"); | |
| 420 { | |
| 421 TRACE_EVENT2("cat2", "name2", "arg1", 2222, "arg2", "string2"); | |
| 422 TRACE_EVENT_INSTANT1("cat1", "name3", "arg1", 3333); | |
| 423 base::PlatformThread::Sleep(200); | |
| 424 TRACE_EVENT0("cat2", "name6"); | |
| 425 } | |
| 426 TRACE_EVENT_INSTANT1("cat2", "name4", "arg1", 4444); | |
| 427 TRACE_EVENT_INSTANT1("cat2", "name5", "arg1", 1111); | |
| 428 TRACE_EVENT_BEGIN0("cat3", "name7"); | |
| 429 TRACE_EVENT_INSTANT2("cat4", "math1", "a", 10, "b", 5); | |
| 430 TRACE_EVENT_INSTANT2("cat4", "math2", "a", 10, "b", 10); | |
| 431 } | |
| 432 TraceLog::GetInstance()->SetEnabled(false); | |
| 433 | |
| 434 const TestTraceEvent* event = NULL; | |
| 435 TraceAnalyzer analyzer; | |
| 436 analyzer.SetEvents(trace_string_); | |
| 437 | |
| 438 TraceAnalyzer::ConstEventPtrVector found; | |
| 439 | |
| 440 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1", &found); | |
| 441 EXPECT_EQ(3u, found.size()); | |
| 442 | |
| 443 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1" && | |
| 444 Query(EVENT_ARG, "arg1") == 1111, &found); | |
| 445 ASSERT_EQ(1u, found.size()); | |
| 446 EXPECT_STREQ("name1", found.front()->name.c_str()); | |
| 447 | |
| 448 analyzer.FindEvents(Query(EVENT_ARG, "arg1") == 1111 || | |
| 449 Query(EVENT_ARG, "arg1") == 2222, &found); | |
| 450 EXPECT_EQ(3u, found.size()); | |
| 451 | |
| 452 analyzer.FindEvents(Query(EVENT_ARG, "arg1") == 1111 && | |
| 453 Query(EVENT_NAME) != "name1", &found); | |
| 454 ASSERT_EQ(1u, found.size()); | |
| 455 EXPECT_STREQ("name5", found.front()->name.c_str()); | |
| 456 | |
| 457 analyzer.FindEvents(Query(EVENT_ARG, "arg1") == 1111 && | |
| 458 !(Query(EVENT_NAME) != "name1"), &found); | |
| 459 ASSERT_EQ(1u, found.size()); | |
| 460 EXPECT_STREQ("name1", found.front()->name.c_str()); | |
| 461 | |
| 462 analyzer.FindEvents(Query::MatchBeginWithEnd() && | |
| 463 Query(EVENT_DURATION) > 180000 && | |
| 464 (Query(EVENT_CATEGORY) == "cat1" || | |
| 465 Query(EVENT_CATEGORY) == "cat2" || | |
| 466 Query(EVENT_CATEGORY) == "cat3"), &found); | |
| 467 EXPECT_EQ(2u, found.size()); | |
| 468 | |
| 469 analyzer.FindEvents(Query(EVENT_ARG, "arg1") <= 3333, &found); | |
| 470 EXPECT_EQ(4u, found.size()); | |
| 471 | |
| 472 analyzer.FindEvents(Query(EVENT_ARG, "arg1") >= 3333, &found); | |
| 473 EXPECT_EQ(2u, found.size()); | |
| 474 | |
| 475 analyzer.FindEvents(Query(EVENT_HAS_ARG, "arg1"), &found); | |
| 476 EXPECT_EQ(5u, found.size()); | |
| 477 | |
| 478 analyzer.FindEvents(Query(EVENT_ARG, "arg2") == "string1", &found); | |
| 479 ASSERT_EQ(1u, found.size()); | |
| 480 EXPECT_STREQ("name1", found.front()->name.c_str()); | |
| 481 | |
| 482 analyzer.FindEvents(Query(EVENT_ARG, "arg2") == Query::Pattern("string?"), | |
| 483 &found); | |
| 484 EXPECT_EQ(2u, found.size()); | |
| 485 | |
| 486 analyzer.FindEvents(Query(EVENT_CATEGORY) == Query::Pattern("cat?") && | |
| 487 Query(EVENT_NAME) != Query::Pattern("*"), | |
| 488 &found); | |
| 489 EXPECT_EQ(0u, found.size()); | |
| 490 | |
| 491 analyzer.FindEvents(!Query(EVENT_HAS_OTHER) && | |
| 492 Query(EVENT_CATEGORY) == "cat2", &found); | |
| 493 EXPECT_EQ(2u, found.size()); | |
| 494 | |
| 495 event = analyzer.FindEvent(Query(OTHER_ARG, "arg1") == 1111); | |
| 496 ASSERT_TRUE(event); | |
| 497 EXPECT_STREQ("name1", event->name.c_str()); | |
| 498 | |
| 499 // Verify that matching END..BEGIN does not get associated. | |
| 500 ASSERT_TRUE(!analyzer.FindEvent(Query::MatchBeginName("name7"))); | |
| 501 | |
| 502 ASSERT_TRUE(!analyzer.FindEvent(Query(EVENT_ARG, "arg1") < 1111)); | |
| 503 | |
| 504 // Verify that arithmetic operators function: | |
| 505 | |
| 506 analyzer.FindEvents(Query(EVENT_ARG, "a") + Query(EVENT_ARG, "b") == 20, | |
| 507 &found); | |
| 508 EXPECT_EQ(1u, found.size()); | |
| 509 EXPECT_STREQ("math2", found.front()->name.c_str()); | |
| 510 | |
| 511 analyzer.FindEvents(Query(EVENT_ARG, "a") - Query(EVENT_ARG, "b") == 5, | |
| 512 &found); | |
| 513 EXPECT_EQ(1u, found.size()); | |
| 514 EXPECT_STREQ("math1", found.front()->name.c_str()); | |
| 515 | |
| 516 analyzer.FindEvents(Query(EVENT_ARG, "a") * Query(EVENT_ARG, "b") == 50, | |
| 517 &found); | |
| 518 EXPECT_EQ(1u, found.size()); | |
| 519 EXPECT_STREQ("math1", found.front()->name.c_str()); | |
| 520 | |
| 521 analyzer.FindEvents(Query(EVENT_ARG, "a") / Query(EVENT_ARG, "b") == 2, | |
| 522 &found); | |
| 523 EXPECT_EQ(1u, found.size()); | |
| 524 EXPECT_STREQ("math1", found.front()->name.c_str()); | |
| 525 | |
| 526 analyzer.FindEvents(Query(EVENT_ARG, "a") % Query(EVENT_ARG, "b") == 0, | |
| 527 &found); | |
| 528 EXPECT_EQ(2u, found.size()); | |
| 529 | |
| 530 analyzer.FindEvents(-Query(EVENT_ARG, "b") == -10, &found); | |
| 531 EXPECT_EQ(1u, found.size()); | |
| 532 EXPECT_STREQ("math2", found.front()->name.c_str()); | |
| 533 } | |
| 534 | |
| 535 // Test that the TraceAnalyzer custom associations work. | |
| 536 TEST_F(TraceEventTestFixture, TraceAnalyzerAssociations) { | |
| 537 using namespace base::debug::trace; | |
| 538 ManualTestSetUp(); | |
| 539 | |
| 540 Clear(); | |
| 541 TraceLog::GetInstance()->SetEnabled(true); | |
| 542 { | |
| 543 TRACE_EVENT_INSTANT1("cat1", "end", "id", 1); | |
| 544 TRACE_EVENT_INSTANT1("cat2", "begin", "id", 2); | |
| 545 TRACE_EVENT_INSTANT1("cat3", "begin", "id", 3); | |
| 546 TRACE_EVENT_INSTANT1("cat4", "end", "id", 2); | |
| 547 TRACE_EVENT_INSTANT1("cat5", "end", "id", 3); | |
| 548 TRACE_EVENT_INSTANT1("cat6", "begin", "id", 1); | |
| 549 } | |
| 550 TraceLog::GetInstance()->SetEnabled(false); | |
| 551 | |
| 552 TraceAnalyzer analyzer; | |
| 553 analyzer.SetEvents(trace_string_); | |
| 554 analyzer.ClearAssociations(); | |
| 555 | |
| 556 Query begin(Query(EVENT_NAME) == "begin"); | |
| 557 Query end(Query(EVENT_NAME) == "end"); | |
| 558 Query match(Query(EVENT_ARG, "id") == Query(OTHER_ARG, "id")); | |
| 559 analyzer.AssociateEvents(begin, end, match); | |
| 560 | |
| 561 TraceAnalyzer::ConstEventPtrVector found; | |
| 562 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1" && | |
| 563 Query(EVENT_HAS_OTHER), &found); | |
| 564 EXPECT_EQ(0u, found.size()); | |
| 565 | |
| 566 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1" && | |
| 567 !Query(EVENT_HAS_OTHER), &found); | |
| 568 EXPECT_EQ(1u, found.size()); | |
| 569 | |
| 570 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat6" && | |
| 571 !Query(EVENT_HAS_OTHER), &found); | |
| 572 EXPECT_EQ(1u, found.size()); | |
| 573 | |
| 574 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat2" && | |
| 575 Query(OTHER_CATEGORY) == "cat4", &found); | |
| 576 EXPECT_EQ(1u, found.size()); | |
| 577 | |
| 578 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat4" && | |
| 579 Query(OTHER_CATEGORY) == "cat2", &found); | |
| 580 EXPECT_EQ(1u, found.size()); | |
| 581 | |
| 582 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat3" && | |
| 583 Query(OTHER_CATEGORY) == "cat5", &found); | |
| 584 EXPECT_EQ(1u, found.size()); | |
| 585 | |
| 586 analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat5" && | |
| 587 Query(OTHER_CATEGORY) == "cat3", &found); | |
| 588 EXPECT_EQ(1u, found.size()); | |
| 589 } | |
| 590 | |
| 409 // Test that categories work. | 591 // Test that categories work. |
|
nduca
2011/10/11 20:33:37
Rewrite the existing tests to use trace analyzer?
jbates
2011/10/12 22:35:20
I'll leave that as an exercise for someone else to
| |
| 410 TEST_F(TraceEventTestFixture, Categories) { | 592 TEST_F(TraceEventTestFixture, Categories) { |
| 411 ManualTestSetUp(); | 593 ManualTestSetUp(); |
| 412 | 594 |
| 413 // Test that categories that are used can be retrieved whether trace was | 595 // Test that categories that are used can be retrieved whether trace was |
| 414 // enabled or disabled when the trace event was encountered. | 596 // enabled or disabled when the trace event was encountered. |
| 415 TRACE_EVENT_INSTANT0("c1", "name"); | 597 TRACE_EVENT_INSTANT0("c1", "name"); |
| 416 TRACE_EVENT_INSTANT0("c2", "name"); | 598 TRACE_EVENT_INSTANT0("c2", "name"); |
| 417 TraceLog::GetInstance()->SetEnabled(true); | 599 TraceLog::GetInstance()->SetEnabled(true); |
| 418 TRACE_EVENT_INSTANT0("c3", "name"); | 600 TRACE_EVENT_INSTANT0("c3", "name"); |
| 419 TRACE_EVENT_INSTANT0("c4", "name"); | 601 TRACE_EVENT_INSTANT0("c4", "name"); |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 917 | 1099 |
| 918 std::string s; | 1100 std::string s; |
| 919 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); | 1101 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); |
| 920 EXPECT_EQ("val1", s); | 1102 EXPECT_EQ("val1", s); |
| 921 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); | 1103 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); |
| 922 EXPECT_EQ("val2", s); | 1104 EXPECT_EQ("val2", s); |
| 923 } | 1105 } |
| 924 | 1106 |
| 925 } // namespace debug | 1107 } // namespace debug |
| 926 } // namespace base | 1108 } // namespace base |
| OLD | NEW |