Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2231)

Unified Diff: base/test/trace_event_analyzer_unittest.cc

Issue 7981004: add classes trace_analyzer::Query and TraceAnalyzer to make it easy to search through trace data (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments updated Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/test/trace_event_analyzer.cc ('K') | « base/test/trace_event_analyzer.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/trace_event_analyzer_unittest.cc
diff --git a/base/test/trace_event_analyzer_unittest.cc b/base/test/trace_event_analyzer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fcf1a4dcf2980f0e0c35575a379dc11c37f21fc8
--- /dev/null
+++ b/base/test/trace_event_analyzer_unittest.cc
@@ -0,0 +1,257 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/bind.h"
+#include "base/test/trace_event_analyzer.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace trace_analyzer {
+
+namespace {
+
+class TraceEventAnalyzerTest : public testing::Test {
+ public:
+ void ManualSetUp();
+ void OnTraceDataCollected(
+ scoped_refptr<base::debug::TraceLog::RefCountedString> json_events_str);
+ void BeginTracing();
+ void EndTracing();
+
+ base::debug::TraceResultBuffer::SimpleOutput output_;
+ base::debug::TraceResultBuffer buffer_;
+};
+
+void TraceEventAnalyzerTest::ManualSetUp() {
+ base::debug::TraceLog::Resurrect();
+ base::debug::TraceLog* tracelog = base::debug::TraceLog::GetInstance();
+ ASSERT_TRUE(tracelog);
+ tracelog->SetOutputCallback(
+ base::Bind(&TraceEventAnalyzerTest::OnTraceDataCollected,
+ base::Unretained(this)));
+ buffer_.SetOutputCallback(output_.GetCallback());
+}
+
+void TraceEventAnalyzerTest::OnTraceDataCollected(
+ scoped_refptr<base::debug::TraceLog::RefCountedString> json_events_str) {
+ buffer_.AddFragment(json_events_str->data);
+}
+
+void TraceEventAnalyzerTest::BeginTracing() {
+ output_.json_output.clear();
+ buffer_.Start();
+ base::debug::TraceLog::GetInstance()->SetEnabled(true);
+}
+
+void TraceEventAnalyzerTest::EndTracing() {
+ base::debug::TraceLog::GetInstance()->SetEnabled(false);
+ buffer_.Finish();
+}
+
+} // namespace
+
+// Test that the TraceAnalyzer works.
jar (doing other things) 2011/10/26 19:35:20 It is much nicer to test components, and verify th
jbates 2011/10/26 21:51:54 In hindsight, I agree that would be easier to main
jbates 2011/10/27 20:36:35 Done.
+TEST_F(TraceEventAnalyzerTest, TraceAnalyzerBasic) {
+ using namespace trace_analyzer;
+ ManualSetUp();
+
+ BeginTracing();
+ {
+ TRACE_EVENT_END0("cat3", "name7");
jar (doing other things) 2011/10/26 19:35:20 nit: for readability, it is much nicer to use name
jbates 2011/10/26 21:51:54 I agree that would be clearer. These trace events
+ 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);
+ }
+ EndTracing();
+
+ const TraceEvent* event = NULL;
+ scoped_ptr<TraceAnalyzer>
+ analyzer(TraceAnalyzer::Create(output_.json_output));
+ ASSERT_TRUE(!!analyzer.get());
+ analyzer->AssociateBeginEndEvents();
+
+ TraceAnalyzer::TraceEventVector found;
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1"), &found);
+ EXPECT_EQ(3u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1") &&
+ Query(EVENT_ARG, "arg1") == Query::Int(1111), &found);
+ ASSERT_EQ(1u, found.size());
+ EXPECT_STREQ("name1", found.front()->name.c_str());
+
+ analyzer->FindEvents(Query(EVENT_ARG, "arg1") == Query::Int(1111) ||
+ Query(EVENT_ARG, "arg1") == Query::Int(2222), &found);
+ EXPECT_EQ(3u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_ARG, "arg1") == Query::Int(1111) &&
+ Query(EVENT_NAME) != Query::String("name1"), &found);
+ ASSERT_EQ(1u, found.size());
+ EXPECT_STREQ("name5", found.front()->name.c_str());
+
+ analyzer->FindEvents(Query(EVENT_ARG, "arg1") == Query::Int(1111) &&
+ !(Query(EVENT_NAME) != Query::String("name1")), &found);
+ ASSERT_EQ(1u, found.size());
+ EXPECT_STREQ("name1", found.front()->name.c_str());
+
+ analyzer->FindEvents(Query::MatchBeginWithEnd() &&
+ Query(EVENT_DURATION) > Query::Int(180000) &&
+ (Query(EVENT_CATEGORY) == Query::String("cat1") ||
+ Query(EVENT_CATEGORY) == Query::String("cat2") ||
+ Query(EVENT_CATEGORY) == Query::String("cat3")), &found);
+ EXPECT_EQ(2u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_ARG, "arg1") <= Query::Int(3333), &found);
+ EXPECT_EQ(4u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_ARG, "arg1") >= Query::Int(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") == Query::String("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) == Query::String("cat2"), &found);
+ EXPECT_EQ(2u, found.size());
+
+ event = analyzer->FindOneEvent(Query(OTHER_ARG, "arg1") == Query::Int(1111));
+ ASSERT_TRUE(event);
+ EXPECT_STREQ("name1", event->name.c_str());
+
+ // Verify that matching END..BEGIN does not get associated.
+ ASSERT_TRUE(!analyzer->FindOneEvent(Query::MatchBeginName("name7")));
+
+ ASSERT_TRUE(!analyzer->FindOneEvent(Query(EVENT_ARG, "arg1") <
+ Query::Int(1111)));
+
+ // Verify that arithmetic operators function:
+
+ analyzer->FindEvents(Query(EVENT_ARG, "a") +
+ Query(EVENT_ARG, "b") == Query::Int(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") ==
+ Query::Int(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") ==
+ Query::Int(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") ==
+ Query::Int(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") ==
+ Query::Int(0), &found);
+ EXPECT_EQ(2u, found.size());
+
+ analyzer->FindEvents(-Query(EVENT_ARG, "b") == Query::Int(-10), &found);
+ EXPECT_EQ(1u, found.size());
+ EXPECT_STREQ("math2", found.front()->name.c_str());
+}
+
+// Test that the TraceAnalyzer custom associations work.
+TEST_F(TraceEventAnalyzerTest, TraceAnalyzerAssociations) {
+ using namespace trace_analyzer;
+ ManualSetUp();
+
+ BeginTracing();
+ {
+ 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);
+ }
+ EndTracing();
+
+ scoped_ptr<TraceAnalyzer>
+ analyzer(TraceAnalyzer::Create(output_.json_output));
+ ASSERT_TRUE(!!analyzer.get());
+
+ Query begin(Query(EVENT_NAME) == Query::String("begin"));
+ Query end(Query(EVENT_NAME) == Query::String("end"));
+ Query match(Query(EVENT_ARG, "id") == Query(OTHER_ARG, "id"));
+ analyzer->AssociateEvents(begin, end, match);
+
+ TraceAnalyzer::TraceEventVector found;
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1") &&
+ Query(EVENT_HAS_OTHER), &found);
+ EXPECT_EQ(0u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat1") &&
+ !Query(EVENT_HAS_OTHER), &found);
+ EXPECT_EQ(1u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat6") &&
+ !Query(EVENT_HAS_OTHER), &found);
+ EXPECT_EQ(1u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat2") &&
+ Query(OTHER_CATEGORY) == Query::String("cat4"), &found);
+ EXPECT_EQ(1u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat4") &&
+ Query(OTHER_CATEGORY) == Query::String("cat2"), &found);
+ EXPECT_EQ(1u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat3") &&
+ Query(OTHER_CATEGORY) == Query::String("cat5"), &found);
+ EXPECT_EQ(1u, found.size());
+
+ analyzer->FindEvents(Query(EVENT_CATEGORY) == Query::String("cat5") &&
+ Query(OTHER_CATEGORY) == Query::String("cat3"), &found);
+ EXPECT_EQ(1u, found.size());
+
+ // Verify that literals and types are properly casted.
+
+ // Since these queries don't refer to the event data, the dummy event below
+ // will never be accessed.
+ TraceEvent dummy;
+ char char_num = 5;
+ short short_num = -5;
+ EXPECT_TRUE((Query::Double(5.0) == Query::Int(char_num)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Double(-5.0) == Query::Int(short_num)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Double(1.0) == Query::Uint(1u)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Double(1.0) == Query::Int(1)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Double(-1.0) == Query::Int(-1)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Double(1.0) == Query::Double(1.0f)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Bool(true) == Query::Int(1)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Bool(false) == Query::Int(0)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Bool(true) == Query::Double(1.0f)).Evaluate(dummy));
+ EXPECT_TRUE((Query::Bool(false) == Query::Double(0.0f)).Evaluate(dummy));
+}
+
+
+} // namespace trace_analyzer
+
« base/test/trace_event_analyzer.cc ('K') | « base/test/trace_event_analyzer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698