Chromium Code Reviews| Index: base/debug/trace_event_unittest.cc |
| diff --git a/base/debug/trace_event_unittest.cc b/base/debug/trace_event_unittest.cc |
| index a5c889f5608f4f7758174ac835a3af64e9beb60b..7cb2bef9978092361c22ce8cb7cb96d6c402dd46 100644 |
| --- a/base/debug/trace_event_unittest.cc |
| +++ b/base/debug/trace_event_unittest.cc |
| @@ -6,6 +6,7 @@ |
| #include "base/bind.h" |
| #include "base/command_line.h" |
| +#include "base/debug/trace_event_test_utils.h" |
| #include "base/json/json_reader.h" |
| #include "base/json/json_writer.h" |
| #include "base/memory/ref_counted_memory.h" |
| @@ -406,6 +407,187 @@ TEST_F(TraceEventTestFixture, DataCaptured) { |
| ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); |
| } |
| +// Test that the TraceAnalyzer works. |
| +TEST_F(TraceEventTestFixture, TraceAnalyzerBasic) { |
| + using namespace base::debug::trace; |
| + ManualTestSetUp(); |
| + |
| + Clear(); |
| + TraceLog::GetInstance()->SetEnabled(true); |
| + { |
| + TRACE_EVENT_END0("cat3", "name7"); |
| + TRACE_EVENT2("cat1", "name1", "arg1", 1111, "arg2", "string1"); |
| + { |
| + TRACE_EVENT2("cat2", "name2", "arg1", 2222, "arg2", "string2"); |
| + TRACE_EVENT_INSTANT1("cat1", "name3", "arg1", 3333); |
| + base::PlatformThread::Sleep(200); |
| + TRACE_EVENT0("cat2", "name6"); |
| + } |
| + TRACE_EVENT_INSTANT1("cat2", "name4", "arg1", 4444); |
| + TRACE_EVENT_INSTANT1("cat2", "name5", "arg1", 1111); |
| + TRACE_EVENT_BEGIN0("cat3", "name7"); |
| + TRACE_EVENT_INSTANT2("cat4", "math1", "a", 10, "b", 5); |
| + TRACE_EVENT_INSTANT2("cat4", "math2", "a", 10, "b", 10); |
| + } |
| + TraceLog::GetInstance()->SetEnabled(false); |
| + |
| + const TestTraceEvent* event = NULL; |
| + TraceAnalyzer analyzer; |
| + analyzer.SetEvents(trace_string_); |
| + |
| + TraceAnalyzer::ConstEventPtrVector found; |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1", &found); |
| + EXPECT_EQ(3u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1" && |
| + Query(EVENT_ARG, "arg1") == 1111, &found); |
| + ASSERT_EQ(1u, found.size()); |
| + EXPECT_STREQ("name1", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg1") == 1111 || |
| + Query(EVENT_ARG, "arg1") == 2222, &found); |
| + EXPECT_EQ(3u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg1") == 1111 && |
| + Query(EVENT_NAME) != "name1", &found); |
| + ASSERT_EQ(1u, found.size()); |
| + EXPECT_STREQ("name5", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg1") == 1111 && |
| + !(Query(EVENT_NAME) != "name1"), &found); |
| + ASSERT_EQ(1u, found.size()); |
| + EXPECT_STREQ("name1", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query::MatchBeginWithEnd() && |
| + Query(EVENT_DURATION) > 180000 && |
| + (Query(EVENT_CATEGORY) == "cat1" || |
| + Query(EVENT_CATEGORY) == "cat2" || |
| + Query(EVENT_CATEGORY) == "cat3"), &found); |
| + EXPECT_EQ(2u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg1") <= 3333, &found); |
| + EXPECT_EQ(4u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg1") >= 3333, &found); |
| + EXPECT_EQ(2u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_HAS_ARG, "arg1"), &found); |
| + EXPECT_EQ(5u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg2") == "string1", &found); |
| + ASSERT_EQ(1u, found.size()); |
| + EXPECT_STREQ("name1", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "arg2") == Query::Pattern("string?"), |
| + &found); |
| + EXPECT_EQ(2u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == Query::Pattern("cat?") && |
| + Query(EVENT_NAME) != Query::Pattern("*"), |
| + &found); |
| + EXPECT_EQ(0u, found.size()); |
| + |
| + analyzer.FindEvents(!Query(EVENT_HAS_OTHER) && |
| + Query(EVENT_CATEGORY) == "cat2", &found); |
| + EXPECT_EQ(2u, found.size()); |
| + |
| + event = analyzer.FindEvent(Query(OTHER_ARG, "arg1") == 1111); |
| + ASSERT_TRUE(event); |
| + EXPECT_STREQ("name1", event->name.c_str()); |
| + |
| + // Verify that matching END..BEGIN does not get associated. |
| + ASSERT_TRUE(!analyzer.FindEvent(Query::MatchBeginName("name7"))); |
| + |
| + ASSERT_TRUE(!analyzer.FindEvent(Query(EVENT_ARG, "arg1") < 1111)); |
| + |
| + // Verify that arithmetic operators function: |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "a") + Query(EVENT_ARG, "b") == 20, |
| + &found); |
| + EXPECT_EQ(1u, found.size()); |
| + EXPECT_STREQ("math2", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "a") - Query(EVENT_ARG, "b") == 5, |
| + &found); |
| + EXPECT_EQ(1u, found.size()); |
| + EXPECT_STREQ("math1", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "a") * Query(EVENT_ARG, "b") == 50, |
| + &found); |
| + EXPECT_EQ(1u, found.size()); |
| + EXPECT_STREQ("math1", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "a") / Query(EVENT_ARG, "b") == 2, |
| + &found); |
| + EXPECT_EQ(1u, found.size()); |
| + EXPECT_STREQ("math1", found.front()->name.c_str()); |
| + |
| + analyzer.FindEvents(Query(EVENT_ARG, "a") % Query(EVENT_ARG, "b") == 0, |
| + &found); |
| + EXPECT_EQ(2u, found.size()); |
| + |
| + analyzer.FindEvents(-Query(EVENT_ARG, "b") == -10, &found); |
| + EXPECT_EQ(1u, found.size()); |
| + EXPECT_STREQ("math2", found.front()->name.c_str()); |
| +} |
| + |
| +// Test that the TraceAnalyzer custom associations work. |
| +TEST_F(TraceEventTestFixture, TraceAnalyzerAssociations) { |
| + using namespace base::debug::trace; |
| + ManualTestSetUp(); |
| + |
| + Clear(); |
| + TraceLog::GetInstance()->SetEnabled(true); |
| + { |
| + TRACE_EVENT_INSTANT1("cat1", "end", "id", 1); |
| + TRACE_EVENT_INSTANT1("cat2", "begin", "id", 2); |
| + TRACE_EVENT_INSTANT1("cat3", "begin", "id", 3); |
| + TRACE_EVENT_INSTANT1("cat4", "end", "id", 2); |
| + TRACE_EVENT_INSTANT1("cat5", "end", "id", 3); |
| + TRACE_EVENT_INSTANT1("cat6", "begin", "id", 1); |
| + } |
| + TraceLog::GetInstance()->SetEnabled(false); |
| + |
| + TraceAnalyzer analyzer; |
| + analyzer.SetEvents(trace_string_); |
| + analyzer.ClearAssociations(); |
| + |
| + Query begin(Query(EVENT_NAME) == "begin"); |
| + Query end(Query(EVENT_NAME) == "end"); |
| + Query match(Query(EVENT_ARG, "id") == Query(OTHER_ARG, "id")); |
| + analyzer.AssociateEvents(begin, end, match); |
| + |
| + TraceAnalyzer::ConstEventPtrVector found; |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1" && |
| + Query(EVENT_HAS_OTHER), &found); |
| + EXPECT_EQ(0u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat1" && |
| + !Query(EVENT_HAS_OTHER), &found); |
| + EXPECT_EQ(1u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat6" && |
| + !Query(EVENT_HAS_OTHER), &found); |
| + EXPECT_EQ(1u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat2" && |
| + Query(OTHER_CATEGORY) == "cat4", &found); |
| + EXPECT_EQ(1u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat4" && |
| + Query(OTHER_CATEGORY) == "cat2", &found); |
| + EXPECT_EQ(1u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat3" && |
| + Query(OTHER_CATEGORY) == "cat5", &found); |
| + EXPECT_EQ(1u, found.size()); |
| + |
| + analyzer.FindEvents(Query(EVENT_CATEGORY) == "cat5" && |
| + Query(OTHER_CATEGORY) == "cat3", &found); |
| + EXPECT_EQ(1u, found.size()); |
| +} |
| + |
| // 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
|
| TEST_F(TraceEventTestFixture, Categories) { |
| ManualTestSetUp(); |