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

Side by Side Diff: base/debug/trace_event_unittest.cc

Issue 10837082: implement SetWatchEvent and WaitForEvent for trace-based-tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleaned up and fixed a bug Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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
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 const scoped_refptr<base::RefCountedString>& events_str); 45 const scoped_refptr<base::RefCountedString>& events_str);
46 void OnTraceNotification(int notification) {
47 event_watch_notification_ =
48 !!(notification & TraceLog::EVENT_WATCH_NOTIFICATION);
49 }
46 DictionaryValue* FindMatchingTraceEntry(const JsonKeyValue* key_values); 50 DictionaryValue* FindMatchingTraceEntry(const JsonKeyValue* key_values);
47 DictionaryValue* FindNamePhase(const char* name, const char* phase); 51 DictionaryValue* FindNamePhase(const char* name, const char* phase);
48 DictionaryValue* FindNamePhaseKeyValue(const char* name, 52 DictionaryValue* FindNamePhaseKeyValue(const char* name,
49 const char* phase, 53 const char* phase,
50 const char* key, 54 const char* key,
51 const char* value); 55 const char* value);
52 bool FindMatchingValue(const char* key, 56 bool FindMatchingValue(const char* key,
53 const char* value); 57 const char* value);
54 bool FindNonMatchingValue(const char* key, 58 bool FindNonMatchingValue(const char* key,
55 const char* value); 59 const char* value);
56 void Clear() { 60 void Clear() {
57 trace_parsed_.Clear(); 61 trace_parsed_.Clear();
58 json_output_.json_output.clear(); 62 json_output_.json_output.clear();
59 } 63 }
60 64
65 void BeginTrace() {
66 event_watch_notification_ = false;
67 TraceLog::GetInstance()->SetEnabled("*");
68 }
69
70 void EndTraceAndFlush() {
71 TraceLog::GetInstance()->SetDisabled();
72 TraceLog::GetInstance()->Flush(
73 base::Bind(&TraceEventTestFixture::OnTraceDataCollected,
74 base::Unretained(this)));
75 }
76
61 virtual void SetUp() OVERRIDE { 77 virtual void SetUp() OVERRIDE {
62 old_thread_name_ = PlatformThread::GetName(); 78 old_thread_name_ = PlatformThread::GetName();
63 } 79 }
64 virtual void TearDown() OVERRIDE { 80 virtual void TearDown() OVERRIDE {
65 if (TraceLog::GetInstance()) 81 if (TraceLog::GetInstance())
66 EXPECT_FALSE(TraceLog::GetInstance()->IsEnabled()); 82 EXPECT_FALSE(TraceLog::GetInstance()->IsEnabled());
67 PlatformThread::SetName(old_thread_name_ ? old_thread_name_ : ""); 83 PlatformThread::SetName(old_thread_name_ ? old_thread_name_ : "");
68 } 84 }
69 85
70 const char* old_thread_name_; 86 const char* old_thread_name_;
71 ListValue trace_parsed_; 87 ListValue trace_parsed_;
72 base::debug::TraceResultBuffer trace_buffer_; 88 base::debug::TraceResultBuffer trace_buffer_;
73 base::debug::TraceResultBuffer::SimpleOutput json_output_; 89 base::debug::TraceResultBuffer::SimpleOutput json_output_;
90 bool event_watch_notification_;
74 91
75 private: 92 private:
76 // We want our singleton torn down after each test. 93 // We want our singleton torn down after each test.
77 ShadowingAtExitManager at_exit_manager_; 94 ShadowingAtExitManager at_exit_manager_;
78 Lock lock_; 95 Lock lock_;
79 }; 96 };
80 97
81 void TraceEventTestFixture::ManualTestSetUp() { 98 void TraceEventTestFixture::ManualTestSetUp() {
82 TraceLog::DeleteForTesting(); 99 TraceLog::DeleteForTesting();
83 TraceLog::Resurrect(); 100 TraceLog::Resurrect();
84 TraceLog* tracelog = TraceLog::GetInstance(); 101 TraceLog* tracelog = TraceLog::GetInstance();
85 ASSERT_TRUE(tracelog); 102 ASSERT_TRUE(tracelog);
86 ASSERT_FALSE(tracelog->IsEnabled()); 103 ASSERT_FALSE(tracelog->IsEnabled());
87 tracelog->SetOutputCallback( 104 tracelog->SetNotificationCallback(
88 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, 105 base::Bind(&TraceEventTestFixture::OnTraceNotification,
89 base::Unretained(this))); 106 base::Unretained(this)));
90 trace_buffer_.SetOutputCallback(json_output_.GetCallback()); 107 trace_buffer_.SetOutputCallback(json_output_.GetCallback());
91 } 108 }
92 109
93 void TraceEventTestFixture::OnTraceDataCollected( 110 void TraceEventTestFixture::OnTraceDataCollected(
94 const scoped_refptr<base::RefCountedString>& events_str) { 111 const scoped_refptr<base::RefCountedString>& events_str) {
95 AutoLock lock(lock_); 112 AutoLock lock(lock_);
96 json_output_.json_output.clear(); 113 json_output_.json_output.clear();
97 trace_buffer_.Start(); 114 trace_buffer_.Start();
98 trace_buffer_.AddFragment(events_str->data()); 115 trace_buffer_.AddFragment(events_str->data());
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 650
634 } // namespace 651 } // namespace
635 652
636 // Simple Test for emitting data and validating it was received. 653 // Simple Test for emitting data and validating it was received.
637 TEST_F(TraceEventTestFixture, DataCaptured) { 654 TEST_F(TraceEventTestFixture, DataCaptured) {
638 ManualTestSetUp(); 655 ManualTestSetUp();
639 TraceLog::GetInstance()->SetEnabled(true); 656 TraceLog::GetInstance()->SetEnabled(true);
640 657
641 TraceWithAllMacroVariants(NULL); 658 TraceWithAllMacroVariants(NULL);
642 659
643 TraceLog::GetInstance()->SetEnabled(false); 660 EndTraceAndFlush();
644 661
645 ValidateAllTraceMacrosCreatedData(trace_parsed_); 662 ValidateAllTraceMacrosCreatedData(trace_parsed_);
646 } 663 }
647 664
648 class MockEnabledStateChangedObserver : 665 class MockEnabledStateChangedObserver :
649 public base::debug::TraceLog::EnabledStateChangedObserver { 666 public base::debug::TraceLog::EnabledStateChangedObserver {
650 public: 667 public:
651 MOCK_METHOD0(OnTraceLogWillEnable, void()); 668 MOCK_METHOD0(OnTraceLogWillEnable, void());
652 MOCK_METHOD0(OnTraceLogWillDisable, void()); 669 MOCK_METHOD0(OnTraceLogWillDisable, void());
653 }; 670 };
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 } 741 }
725 742
726 // Test that categories work. 743 // Test that categories work.
727 TEST_F(TraceEventTestFixture, Categories) { 744 TEST_F(TraceEventTestFixture, Categories) {
728 ManualTestSetUp(); 745 ManualTestSetUp();
729 746
730 // Test that categories that are used can be retrieved whether trace was 747 // Test that categories that are used can be retrieved whether trace was
731 // enabled or disabled when the trace event was encountered. 748 // enabled or disabled when the trace event was encountered.
732 TRACE_EVENT_INSTANT0("c1", "name"); 749 TRACE_EVENT_INSTANT0("c1", "name");
733 TRACE_EVENT_INSTANT0("c2", "name"); 750 TRACE_EVENT_INSTANT0("c2", "name");
734 TraceLog::GetInstance()->SetEnabled(true); 751 BeginTrace();
735 TRACE_EVENT_INSTANT0("c3", "name"); 752 TRACE_EVENT_INSTANT0("c3", "name");
736 TRACE_EVENT_INSTANT0("c4", "name"); 753 TRACE_EVENT_INSTANT0("c4", "name");
737 TraceLog::GetInstance()->SetEnabled(false); 754 EndTraceAndFlush();
738 std::vector<std::string> cats; 755 std::vector<std::string> cats;
739 TraceLog::GetInstance()->GetKnownCategories(&cats); 756 TraceLog::GetInstance()->GetKnownCategories(&cats);
740 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c1") != cats.end()); 757 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c1") != cats.end());
741 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c2") != cats.end()); 758 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c2") != cats.end());
742 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c3") != cats.end()); 759 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c3") != cats.end());
743 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c4") != cats.end()); 760 EXPECT_TRUE(std::find(cats.begin(), cats.end(), "c4") != cats.end());
744 761
745 const std::vector<std::string> empty_categories; 762 const std::vector<std::string> empty_categories;
746 std::vector<std::string> included_categories; 763 std::vector<std::string> included_categories;
747 std::vector<std::string> excluded_categories; 764 std::vector<std::string> excluded_categories;
748 765
749 // Test that category filtering works. 766 // Test that category filtering works.
750 767
751 // Include nonexistent category -> no events 768 // Include nonexistent category -> no events
752 Clear(); 769 Clear();
753 included_categories.clear(); 770 included_categories.clear();
754 included_categories.push_back("not_found823564786"); 771 included_categories.push_back("not_found823564786");
755 TraceLog::GetInstance()->SetEnabled(included_categories, empty_categories); 772 TraceLog::GetInstance()->SetEnabled(included_categories, empty_categories);
756 TRACE_EVENT_INSTANT0("cat1", "name"); 773 TRACE_EVENT_INSTANT0("cat1", "name");
757 TRACE_EVENT_INSTANT0("cat2", "name"); 774 TRACE_EVENT_INSTANT0("cat2", "name");
758 TraceLog::GetInstance()->SetDisabled(); 775 EndTraceAndFlush();
759 EXPECT_TRUE(trace_parsed_.empty()); 776 EXPECT_TRUE(trace_parsed_.empty());
760 777
761 // Include existent category -> only events of that category 778 // Include existent category -> only events of that category
762 Clear(); 779 Clear();
763 included_categories.clear(); 780 included_categories.clear();
764 included_categories.push_back("inc"); 781 included_categories.push_back("inc");
765 TraceLog::GetInstance()->SetEnabled(included_categories, empty_categories); 782 TraceLog::GetInstance()->SetEnabled(included_categories, empty_categories);
766 TRACE_EVENT_INSTANT0("inc", "name"); 783 TRACE_EVENT_INSTANT0("inc", "name");
767 TRACE_EVENT_INSTANT0("inc2", "name"); 784 TRACE_EVENT_INSTANT0("inc2", "name");
768 TraceLog::GetInstance()->SetDisabled(); 785 EndTraceAndFlush();
769 EXPECT_TRUE(FindMatchingValue("cat", "inc")); 786 EXPECT_TRUE(FindMatchingValue("cat", "inc"));
770 EXPECT_FALSE(FindNonMatchingValue("cat", "inc")); 787 EXPECT_FALSE(FindNonMatchingValue("cat", "inc"));
771 788
772 // Include existent wildcard -> all categories matching wildcard 789 // Include existent wildcard -> all categories matching wildcard
773 Clear(); 790 Clear();
774 included_categories.clear(); 791 included_categories.clear();
775 included_categories.push_back("inc_wildcard_*"); 792 included_categories.push_back("inc_wildcard_*");
776 included_categories.push_back("inc_wildchar_?_end"); 793 included_categories.push_back("inc_wildchar_?_end");
777 TraceLog::GetInstance()->SetEnabled(included_categories, empty_categories); 794 TraceLog::GetInstance()->SetEnabled(included_categories, empty_categories);
778 TRACE_EVENT_INSTANT0("inc_wildcard_abc", "included"); 795 TRACE_EVENT_INSTANT0("inc_wildcard_abc", "included");
779 TRACE_EVENT_INSTANT0("inc_wildcard_", "included"); 796 TRACE_EVENT_INSTANT0("inc_wildcard_", "included");
780 TRACE_EVENT_INSTANT0("inc_wildchar_x_end", "included"); 797 TRACE_EVENT_INSTANT0("inc_wildchar_x_end", "included");
781 TRACE_EVENT_INSTANT0("inc_wildchar_bla_end", "not_inc"); 798 TRACE_EVENT_INSTANT0("inc_wildchar_bla_end", "not_inc");
782 TRACE_EVENT_INSTANT0("cat1", "not_inc"); 799 TRACE_EVENT_INSTANT0("cat1", "not_inc");
783 TRACE_EVENT_INSTANT0("cat2", "not_inc"); 800 TRACE_EVENT_INSTANT0("cat2", "not_inc");
784 TraceLog::GetInstance()->SetDisabled(); 801 EndTraceAndFlush();
785 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildcard_abc")); 802 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildcard_abc"));
786 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildcard_")); 803 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildcard_"));
787 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildchar_x_end")); 804 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildchar_x_end"));
788 EXPECT_FALSE(FindMatchingValue("name", "not_inc")); 805 EXPECT_FALSE(FindMatchingValue("name", "not_inc"));
789 806
790 included_categories.clear(); 807 included_categories.clear();
791 808
792 // Exclude nonexistent category -> all events 809 // Exclude nonexistent category -> all events
793 Clear(); 810 Clear();
794 excluded_categories.clear(); 811 excluded_categories.clear();
795 excluded_categories.push_back("not_found823564786"); 812 excluded_categories.push_back("not_found823564786");
796 TraceLog::GetInstance()->SetEnabled(empty_categories, excluded_categories); 813 TraceLog::GetInstance()->SetEnabled(empty_categories, excluded_categories);
797 TRACE_EVENT_INSTANT0("cat1", "name"); 814 TRACE_EVENT_INSTANT0("cat1", "name");
798 TRACE_EVENT_INSTANT0("cat2", "name"); 815 TRACE_EVENT_INSTANT0("cat2", "name");
799 TraceLog::GetInstance()->SetDisabled(); 816 EndTraceAndFlush();
800 EXPECT_TRUE(FindMatchingValue("cat", "cat1")); 817 EXPECT_TRUE(FindMatchingValue("cat", "cat1"));
801 EXPECT_TRUE(FindMatchingValue("cat", "cat2")); 818 EXPECT_TRUE(FindMatchingValue("cat", "cat2"));
802 819
803 // Exclude existent category -> only events of other categories 820 // Exclude existent category -> only events of other categories
804 Clear(); 821 Clear();
805 excluded_categories.clear(); 822 excluded_categories.clear();
806 excluded_categories.push_back("inc"); 823 excluded_categories.push_back("inc");
807 TraceLog::GetInstance()->SetEnabled(empty_categories, excluded_categories); 824 TraceLog::GetInstance()->SetEnabled(empty_categories, excluded_categories);
808 TRACE_EVENT_INSTANT0("inc", "name"); 825 TRACE_EVENT_INSTANT0("inc", "name");
809 TRACE_EVENT_INSTANT0("inc2", "name"); 826 TRACE_EVENT_INSTANT0("inc2", "name");
810 TraceLog::GetInstance()->SetDisabled(); 827 EndTraceAndFlush();
811 EXPECT_TRUE(FindMatchingValue("cat", "inc2")); 828 EXPECT_TRUE(FindMatchingValue("cat", "inc2"));
812 EXPECT_FALSE(FindMatchingValue("cat", "inc")); 829 EXPECT_FALSE(FindMatchingValue("cat", "inc"));
813 830
814 // Exclude existent wildcard -> all categories not matching wildcard 831 // Exclude existent wildcard -> all categories not matching wildcard
815 Clear(); 832 Clear();
816 excluded_categories.clear(); 833 excluded_categories.clear();
817 excluded_categories.push_back("inc_wildcard_*"); 834 excluded_categories.push_back("inc_wildcard_*");
818 excluded_categories.push_back("inc_wildchar_?_end"); 835 excluded_categories.push_back("inc_wildchar_?_end");
819 TraceLog::GetInstance()->SetEnabled(empty_categories, excluded_categories); 836 TraceLog::GetInstance()->SetEnabled(empty_categories, excluded_categories);
820 TRACE_EVENT_INSTANT0("inc_wildcard_abc", "not_inc"); 837 TRACE_EVENT_INSTANT0("inc_wildcard_abc", "not_inc");
821 TRACE_EVENT_INSTANT0("inc_wildcard_", "not_inc"); 838 TRACE_EVENT_INSTANT0("inc_wildcard_", "not_inc");
822 TRACE_EVENT_INSTANT0("inc_wildchar_x_end", "not_inc"); 839 TRACE_EVENT_INSTANT0("inc_wildchar_x_end", "not_inc");
823 TRACE_EVENT_INSTANT0("inc_wildchar_bla_end", "included"); 840 TRACE_EVENT_INSTANT0("inc_wildchar_bla_end", "included");
824 TRACE_EVENT_INSTANT0("cat1", "included"); 841 TRACE_EVENT_INSTANT0("cat1", "included");
825 TRACE_EVENT_INSTANT0("cat2", "included"); 842 TRACE_EVENT_INSTANT0("cat2", "included");
826 TraceLog::GetInstance()->SetDisabled(); 843 EndTraceAndFlush();
827 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildchar_bla_end")); 844 EXPECT_TRUE(FindMatchingValue("cat", "inc_wildchar_bla_end"));
828 EXPECT_TRUE(FindMatchingValue("cat", "cat1")); 845 EXPECT_TRUE(FindMatchingValue("cat", "cat1"));
829 EXPECT_TRUE(FindMatchingValue("cat", "cat2")); 846 EXPECT_TRUE(FindMatchingValue("cat", "cat2"));
830 EXPECT_FALSE(FindMatchingValue("name", "not_inc")); 847 EXPECT_FALSE(FindMatchingValue("name", "not_inc"));
831 } 848 }
832 849
833 // Simple Test for time threshold events. 850 // Simple Test for time threshold events.
834 TEST_F(TraceEventTestFixture, DataCapturedThreshold) { 851 TEST_F(TraceEventTestFixture, DataCapturedThreshold) {
835 ManualTestSetUp(); 852 ManualTestSetUp();
836 TraceLog::GetInstance()->SetEnabled(true); 853 BeginTrace();
837 854
838 // Test that events at the same level are properly filtered by threshold. 855 // Test that events at the same level are properly filtered by threshold.
839 { 856 {
840 TRACE_EVENT_IF_LONGER_THAN0(100, "time", "threshold 100"); 857 TRACE_EVENT_IF_LONGER_THAN0(100, "time", "threshold 100");
841 TRACE_EVENT_IF_LONGER_THAN0(1000, "time", "threshold 1000"); 858 TRACE_EVENT_IF_LONGER_THAN0(1000, "time", "threshold 1000");
842 TRACE_EVENT_IF_LONGER_THAN0(10000, "time", "threshold 10000"); 859 TRACE_EVENT_IF_LONGER_THAN0(10000, "time", "threshold 10000");
843 // 100+ seconds to avoid flakiness. 860 // 100+ seconds to avoid flakiness.
844 TRACE_EVENT_IF_LONGER_THAN0(100000000, "time", "threshold long1"); 861 TRACE_EVENT_IF_LONGER_THAN0(100000000, "time", "threshold long1");
845 TRACE_EVENT_IF_LONGER_THAN0(200000000, "time", "threshold long2"); 862 TRACE_EVENT_IF_LONGER_THAN0(200000000, "time", "threshold long2");
846 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); 863 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 "4thresholdlong2"); 912 "4thresholdlong2");
896 base::PlatformThread::Sleep( 913 base::PlatformThread::Sleep(
897 base::TimeDelta::FromMilliseconds(20)); 914 base::TimeDelta::FromMilliseconds(20));
898 } 915 }
899 } 916 }
900 } 917 }
901 } 918 }
902 } 919 }
903 } 920 }
904 921
905 TraceLog::GetInstance()->SetEnabled(false); 922 EndTraceAndFlush();
906 923
907 #define EXPECT_FIND_BE_(str) \ 924 #define EXPECT_FIND_BE_(str) \
908 EXPECT_TRUE(FindNamePhase(str, "B")); \ 925 EXPECT_TRUE(FindNamePhase(str, "B")); \
909 EXPECT_TRUE(FindNamePhase(str, "E")) 926 EXPECT_TRUE(FindNamePhase(str, "E"))
910 #define EXPECT_NOT_FIND_BE_(str) \ 927 #define EXPECT_NOT_FIND_BE_(str) \
911 EXPECT_FALSE(FindNamePhase(str, "B")); \ 928 EXPECT_FALSE(FindNamePhase(str, "B")); \
912 EXPECT_FALSE(FindNamePhase(str, "E")) 929 EXPECT_FALSE(FindNamePhase(str, "E"))
913 930
914 EXPECT_FIND_BE_("threshold 100"); 931 EXPECT_FIND_BE_("threshold 100");
915 EXPECT_FIND_BE_("threshold 1000"); 932 EXPECT_FIND_BE_("threshold 1000");
(...skipping 12 matching lines...) Expand all
928 EXPECT_NOT_FIND_BE_("3thresholdlong2"); 945 EXPECT_NOT_FIND_BE_("3thresholdlong2");
929 946
930 EXPECT_FIND_BE_("nonthreshold4"); 947 EXPECT_FIND_BE_("nonthreshold4");
931 EXPECT_FIND_BE_("4threshold100"); 948 EXPECT_FIND_BE_("4threshold100");
932 EXPECT_FIND_BE_("4threshold1000"); 949 EXPECT_FIND_BE_("4threshold1000");
933 EXPECT_FIND_BE_("4threshold10000"); 950 EXPECT_FIND_BE_("4threshold10000");
934 EXPECT_NOT_FIND_BE_("4thresholdlong1"); 951 EXPECT_NOT_FIND_BE_("4thresholdlong1");
935 EXPECT_NOT_FIND_BE_("4thresholdlong2"); 952 EXPECT_NOT_FIND_BE_("4thresholdlong2");
936 } 953 }
937 954
955 // Test EVENT_WATCH_NOTIFICATION
956 TEST_F(TraceEventTestFixture, EventWatchNotification) {
957 ManualTestSetUp();
958
959 // Basic one occurrence.
960 BeginTrace();
961 TraceLog::GetInstance()->SetWatchEvent("cat", "event", 1);
962 TRACE_EVENT_INSTANT0("cat", "event");
963 EndTraceAndFlush();
964 EXPECT_TRUE(event_watch_notification_);
965
966 // Auto-reset after end trace.
967 BeginTrace();
968 TraceLog::GetInstance()->SetWatchEvent("cat", "event", 1);
969 EndTraceAndFlush();
970 BeginTrace();
971 TRACE_EVENT_INSTANT0("cat", "event");
972 EndTraceAndFlush();
973 EXPECT_FALSE(event_watch_notification_);
974
975 // Multiple occurrence.
976 BeginTrace();
977 int num_occurrences = 5;
978 TraceLog::GetInstance()->SetWatchEvent("cat", "event", num_occurrences);
979 for (int i = 0; i < num_occurrences; ++i)
980 TRACE_EVENT_INSTANT0("cat", "event");
981 EndTraceAndFlush();
982 EXPECT_TRUE(event_watch_notification_);
983
984 // Wrong category.
985 BeginTrace();
986 TraceLog::GetInstance()->SetWatchEvent("cat", "event", 1);
987 TRACE_EVENT_INSTANT0("wrong_cat", "event");
988 EndTraceAndFlush();
989 EXPECT_FALSE(event_watch_notification_);
990
991 // Wrong name.
992 BeginTrace();
993 TraceLog::GetInstance()->SetWatchEvent("cat", "event", 1);
994 TRACE_EVENT_INSTANT0("cat", "wrong_event");
995 EndTraceAndFlush();
996 EXPECT_FALSE(event_watch_notification_);
997
998 // Not enough occurrences.
999 BeginTrace();
1000 TraceLog::GetInstance()->SetWatchEvent("cat", "event", 2);
1001 TRACE_EVENT_INSTANT0("cat", "event");
1002 EndTraceAndFlush();
1003 EXPECT_FALSE(event_watch_notification_);
1004
1005 // Canceled.
1006 BeginTrace();
1007 TraceLog::GetInstance()->SetWatchEvent("cat", "event", 1);
1008 TraceLog::GetInstance()->CancelWatchEvent();
1009 TRACE_EVENT_INSTANT0("cat", "event");
1010 EndTraceAndFlush();
1011 EXPECT_FALSE(event_watch_notification_);
1012 }
1013
938 // Test ASYNC_BEGIN/END events 1014 // Test ASYNC_BEGIN/END events
939 TEST_F(TraceEventTestFixture, AsyncBeginEndEvents) { 1015 TEST_F(TraceEventTestFixture, AsyncBeginEndEvents) {
940 ManualTestSetUp(); 1016 ManualTestSetUp();
941 TraceLog::GetInstance()->SetEnabled(true); 1017 BeginTrace();
942 1018
943 unsigned long long id = 0xfeedbeeffeedbeefull; 1019 unsigned long long id = 0xfeedbeeffeedbeefull;
944 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name1", id); 1020 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name1", id);
945 TRACE_EVENT_ASYNC_BEGIN_STEP0( "cat", "name1", id, "step1"); 1021 TRACE_EVENT_ASYNC_BEGIN_STEP0( "cat", "name1", id, "step1");
946 TRACE_EVENT_ASYNC_END0("cat", "name1", id); 1022 TRACE_EVENT_ASYNC_END0("cat", "name1", id);
947 TRACE_EVENT_BEGIN0( "cat", "name2"); 1023 TRACE_EVENT_BEGIN0( "cat", "name2");
948 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name3", 0); 1024 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name3", 0);
949 1025
950 TraceLog::GetInstance()->SetEnabled(false); 1026 EndTraceAndFlush();
951 1027
952 EXPECT_TRUE(FindNamePhase("name1", "S")); 1028 EXPECT_TRUE(FindNamePhase("name1", "S"));
953 EXPECT_TRUE(FindNamePhase("name1", "T")); 1029 EXPECT_TRUE(FindNamePhase("name1", "T"));
954 EXPECT_TRUE(FindNamePhase("name1", "F")); 1030 EXPECT_TRUE(FindNamePhase("name1", "F"));
955 1031
956 std::string id_str; 1032 std::string id_str;
957 StringAppendF(&id_str, "%llx", id); 1033 StringAppendF(&id_str, "%llx", id);
958 1034
959 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "S", "id", id_str.c_str())); 1035 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "S", "id", id_str.c_str()));
960 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "T", "id", id_str.c_str())); 1036 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "T", "id", id_str.c_str()));
961 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "F", "id", id_str.c_str())); 1037 EXPECT_TRUE(FindNamePhaseKeyValue("name1", "F", "id", id_str.c_str()));
962 EXPECT_TRUE(FindNamePhaseKeyValue("name3", "S", "id", "0")); 1038 EXPECT_TRUE(FindNamePhaseKeyValue("name3", "S", "id", "0"));
963 1039
964 // BEGIN events should not have id 1040 // BEGIN events should not have id
965 EXPECT_FALSE(FindNamePhaseKeyValue("name2", "B", "id", "0")); 1041 EXPECT_FALSE(FindNamePhaseKeyValue("name2", "B", "id", "0"));
966 } 1042 }
967 1043
968 // Test ASYNC_BEGIN/END events 1044 // Test ASYNC_BEGIN/END events
969 TEST_F(TraceEventTestFixture, AsyncBeginEndPointerMangling) { 1045 TEST_F(TraceEventTestFixture, AsyncBeginEndPointerMangling) {
970 ManualTestSetUp(); 1046 ManualTestSetUp();
971 1047
972 void* ptr = this; 1048 void* ptr = this;
973 1049
974 TraceLog::GetInstance()->SetProcessID(100); 1050 TraceLog::GetInstance()->SetProcessID(100);
975 TraceLog::GetInstance()->SetEnabled(true); 1051 BeginTrace();
976 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name1", ptr); 1052 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name1", ptr);
977 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name2", ptr); 1053 TRACE_EVENT_ASYNC_BEGIN0( "cat", "name2", ptr);
978 TraceLog::GetInstance()->SetEnabled(false); 1054 EndTraceAndFlush();
979 1055
980 TraceLog::GetInstance()->SetProcessID(200); 1056 TraceLog::GetInstance()->SetProcessID(200);
981 TraceLog::GetInstance()->SetEnabled(true); 1057 BeginTrace();
982 TRACE_EVENT_ASYNC_END0( "cat", "name1", ptr); 1058 TRACE_EVENT_ASYNC_END0( "cat", "name1", ptr);
983 TraceLog::GetInstance()->SetEnabled(false); 1059 EndTraceAndFlush();
984 1060
985 DictionaryValue* async_begin = FindNamePhase("name1", "S"); 1061 DictionaryValue* async_begin = FindNamePhase("name1", "S");
986 DictionaryValue* async_begin2 = FindNamePhase("name2", "S"); 1062 DictionaryValue* async_begin2 = FindNamePhase("name2", "S");
987 DictionaryValue* async_end = FindNamePhase("name1", "F"); 1063 DictionaryValue* async_end = FindNamePhase("name1", "F");
988 EXPECT_TRUE(async_begin); 1064 EXPECT_TRUE(async_begin);
989 EXPECT_TRUE(async_begin2); 1065 EXPECT_TRUE(async_begin2);
990 EXPECT_TRUE(async_end); 1066 EXPECT_TRUE(async_end);
991 1067
992 Value* value = NULL; 1068 Value* value = NULL;
993 std::string async_begin_id_str; 1069 std::string async_begin_id_str;
994 std::string async_begin2_id_str; 1070 std::string async_begin2_id_str;
995 std::string async_end_id_str; 1071 std::string async_end_id_str;
996 ASSERT_TRUE(async_begin->Get("id", &value)); 1072 ASSERT_TRUE(async_begin->Get("id", &value));
997 ASSERT_TRUE(value->GetAsString(&async_begin_id_str)); 1073 ASSERT_TRUE(value->GetAsString(&async_begin_id_str));
998 ASSERT_TRUE(async_begin2->Get("id", &value)); 1074 ASSERT_TRUE(async_begin2->Get("id", &value));
999 ASSERT_TRUE(value->GetAsString(&async_begin2_id_str)); 1075 ASSERT_TRUE(value->GetAsString(&async_begin2_id_str));
1000 ASSERT_TRUE(async_end->Get("id", &value)); 1076 ASSERT_TRUE(async_end->Get("id", &value));
1001 ASSERT_TRUE(value->GetAsString(&async_end_id_str)); 1077 ASSERT_TRUE(value->GetAsString(&async_end_id_str));
1002 1078
1003 EXPECT_STREQ(async_begin_id_str.c_str(), async_begin2_id_str.c_str()); 1079 EXPECT_STREQ(async_begin_id_str.c_str(), async_begin2_id_str.c_str());
1004 EXPECT_STRNE(async_begin_id_str.c_str(), async_end_id_str.c_str()); 1080 EXPECT_STRNE(async_begin_id_str.c_str(), async_end_id_str.c_str());
1005 } 1081 }
1006 1082
1007 // Test that static strings are not copied. 1083 // Test that static strings are not copied.
1008 TEST_F(TraceEventTestFixture, StaticStringVsString) { 1084 TEST_F(TraceEventTestFixture, StaticStringVsString) {
1009 ManualTestSetUp(); 1085 ManualTestSetUp();
1010 TraceLog* tracer = TraceLog::GetInstance(); 1086 TraceLog* tracer = TraceLog::GetInstance();
1011 // Make sure old events are flushed: 1087 // Make sure old events are flushed:
1012 tracer->SetEnabled(false); 1088 EndTraceAndFlush();
1013 EXPECT_EQ(0u, tracer->GetEventsSize()); 1089 EXPECT_EQ(0u, tracer->GetEventsSize());
1014 1090
1015 { 1091 {
1016 tracer->SetEnabled(true); 1092 BeginTrace();
1017 // Test that string arguments are copied. 1093 // Test that string arguments are copied.
1018 TRACE_EVENT2("cat", "name1", 1094 TRACE_EVENT2("cat", "name1",
1019 "arg1", std::string("argval"), "arg2", std::string("argval")); 1095 "arg1", std::string("argval"), "arg2", std::string("argval"));
1020 // Test that static TRACE_STR_COPY string arguments are copied. 1096 // Test that static TRACE_STR_COPY string arguments are copied.
1021 TRACE_EVENT2("cat", "name2", 1097 TRACE_EVENT2("cat", "name2",
1022 "arg1", TRACE_STR_COPY("argval"), 1098 "arg1", TRACE_STR_COPY("argval"),
1023 "arg2", TRACE_STR_COPY("argval")); 1099 "arg2", TRACE_STR_COPY("argval"));
1024 size_t num_events = tracer->GetEventsSize(); 1100 size_t num_events = tracer->GetEventsSize();
1025 EXPECT_GT(num_events, 1u); 1101 EXPECT_GT(num_events, 1u);
1026 const TraceEvent& event1 = tracer->GetEventAt(num_events - 2); 1102 const TraceEvent& event1 = tracer->GetEventAt(num_events - 2);
1027 const TraceEvent& event2 = tracer->GetEventAt(num_events - 1); 1103 const TraceEvent& event2 = tracer->GetEventAt(num_events - 1);
1028 EXPECT_STREQ("name1", event1.name()); 1104 EXPECT_STREQ("name1", event1.name());
1029 EXPECT_STREQ("name2", event2.name()); 1105 EXPECT_STREQ("name2", event2.name());
1030 EXPECT_TRUE(event1.parameter_copy_storage() != NULL); 1106 EXPECT_TRUE(event1.parameter_copy_storage() != NULL);
1031 EXPECT_TRUE(event2.parameter_copy_storage() != NULL); 1107 EXPECT_TRUE(event2.parameter_copy_storage() != NULL);
1032 EXPECT_GT(event1.parameter_copy_storage()->size(), 0u); 1108 EXPECT_GT(event1.parameter_copy_storage()->size(), 0u);
1033 EXPECT_GT(event2.parameter_copy_storage()->size(), 0u); 1109 EXPECT_GT(event2.parameter_copy_storage()->size(), 0u);
1034 tracer->SetEnabled(false); 1110 EndTraceAndFlush();
1035 } 1111 }
1036 1112
1037 { 1113 {
1038 tracer->SetEnabled(true); 1114 BeginTrace();
1039 // Test that static literal string arguments are not copied. 1115 // Test that static literal string arguments are not copied.
1040 TRACE_EVENT2("cat", "name1", 1116 TRACE_EVENT2("cat", "name1",
1041 "arg1", "argval", "arg2", "argval"); 1117 "arg1", "argval", "arg2", "argval");
1042 // Test that static TRACE_STR_COPY NULL string arguments are not copied. 1118 // Test that static TRACE_STR_COPY NULL string arguments are not copied.
1043 const char* str1 = NULL; 1119 const char* str1 = NULL;
1044 const char* str2 = NULL; 1120 const char* str2 = NULL;
1045 TRACE_EVENT2("cat", "name2", 1121 TRACE_EVENT2("cat", "name2",
1046 "arg1", TRACE_STR_COPY(str1), 1122 "arg1", TRACE_STR_COPY(str1),
1047 "arg2", TRACE_STR_COPY(str2)); 1123 "arg2", TRACE_STR_COPY(str2));
1048 size_t num_events = tracer->GetEventsSize(); 1124 size_t num_events = tracer->GetEventsSize();
1049 EXPECT_GT(num_events, 1u); 1125 EXPECT_GT(num_events, 1u);
1050 const TraceEvent& event1 = tracer->GetEventAt(num_events - 2); 1126 const TraceEvent& event1 = tracer->GetEventAt(num_events - 2);
1051 const TraceEvent& event2 = tracer->GetEventAt(num_events - 1); 1127 const TraceEvent& event2 = tracer->GetEventAt(num_events - 1);
1052 EXPECT_STREQ("name1", event1.name()); 1128 EXPECT_STREQ("name1", event1.name());
1053 EXPECT_STREQ("name2", event2.name()); 1129 EXPECT_STREQ("name2", event2.name());
1054 EXPECT_TRUE(event1.parameter_copy_storage() == NULL); 1130 EXPECT_TRUE(event1.parameter_copy_storage() == NULL);
1055 EXPECT_TRUE(event2.parameter_copy_storage() == NULL); 1131 EXPECT_TRUE(event2.parameter_copy_storage() == NULL);
1056 tracer->SetEnabled(false); 1132 EndTraceAndFlush();
1057 } 1133 }
1058 } 1134 }
1059 1135
1060 // Test that data sent from other threads is gathered 1136 // Test that data sent from other threads is gathered
1061 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { 1137 TEST_F(TraceEventTestFixture, DataCapturedOnThread) {
1062 ManualTestSetUp(); 1138 ManualTestSetUp();
1063 TraceLog::GetInstance()->SetEnabled(true); 1139 BeginTrace();
1064 1140
1065 Thread thread("1"); 1141 Thread thread("1");
1066 WaitableEvent task_complete_event(false, false); 1142 WaitableEvent task_complete_event(false, false);
1067 thread.Start(); 1143 thread.Start();
1068 1144
1069 thread.message_loop()->PostTask( 1145 thread.message_loop()->PostTask(
1070 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event)); 1146 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event));
1071 task_complete_event.Wait(); 1147 task_complete_event.Wait();
1072 thread.Stop(); 1148 thread.Stop();
1073 1149
1074 TraceLog::GetInstance()->SetEnabled(false); 1150 EndTraceAndFlush();
1075 ValidateAllTraceMacrosCreatedData(trace_parsed_); 1151 ValidateAllTraceMacrosCreatedData(trace_parsed_);
1076 } 1152 }
1077 1153
1078 // Test that data sent from multiple threads is gathered 1154 // Test that data sent from multiple threads is gathered
1079 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { 1155 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) {
1080 ManualTestSetUp(); 1156 ManualTestSetUp();
1081 TraceLog::GetInstance()->SetEnabled(true); 1157 BeginTrace();
1082 1158
1083 const int num_threads = 4; 1159 const int num_threads = 4;
1084 const int num_events = 4000; 1160 const int num_events = 4000;
1085 Thread* threads[num_threads]; 1161 Thread* threads[num_threads];
1086 WaitableEvent* task_complete_events[num_threads]; 1162 WaitableEvent* task_complete_events[num_threads];
1087 for (int i = 0; i < num_threads; i++) { 1163 for (int i = 0; i < num_threads; i++) {
1088 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); 1164 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str());
1089 task_complete_events[i] = new WaitableEvent(false, false); 1165 task_complete_events[i] = new WaitableEvent(false, false);
1090 threads[i]->Start(); 1166 threads[i]->Start();
1091 threads[i]->message_loop()->PostTask( 1167 threads[i]->message_loop()->PostTask(
1092 FROM_HERE, base::Bind(&TraceManyInstantEvents, 1168 FROM_HERE, base::Bind(&TraceManyInstantEvents,
1093 i, num_events, task_complete_events[i])); 1169 i, num_events, task_complete_events[i]));
1094 } 1170 }
1095 1171
1096 for (int i = 0; i < num_threads; i++) { 1172 for (int i = 0; i < num_threads; i++) {
1097 task_complete_events[i]->Wait(); 1173 task_complete_events[i]->Wait();
1098 } 1174 }
1099 1175
1100 for (int i = 0; i < num_threads; i++) { 1176 for (int i = 0; i < num_threads; i++) {
1101 threads[i]->Stop(); 1177 threads[i]->Stop();
1102 delete threads[i]; 1178 delete threads[i];
1103 delete task_complete_events[i]; 1179 delete task_complete_events[i];
1104 } 1180 }
1105 1181
1106 TraceLog::GetInstance()->SetEnabled(false); 1182 EndTraceAndFlush();
1107 1183
1108 ValidateInstantEventPresentOnEveryThread(trace_parsed_, 1184 ValidateInstantEventPresentOnEveryThread(trace_parsed_,
1109 num_threads, num_events); 1185 num_threads, num_events);
1110 } 1186 }
1111 1187
1112 // Test that thread and process names show up in the trace 1188 // Test that thread and process names show up in the trace
1113 TEST_F(TraceEventTestFixture, ThreadNames) { 1189 TEST_F(TraceEventTestFixture, ThreadNames) {
1114 ManualTestSetUp(); 1190 ManualTestSetUp();
1115 1191
1116 // Create threads before we enable tracing to make sure 1192 // Create threads before we enable tracing to make sure
1117 // that tracelog still captures them. 1193 // that tracelog still captures them.
1118 const int num_threads = 4; 1194 const int num_threads = 4;
1119 const int num_events = 10; 1195 const int num_events = 10;
1120 Thread* threads[num_threads]; 1196 Thread* threads[num_threads];
1121 PlatformThreadId thread_ids[num_threads]; 1197 PlatformThreadId thread_ids[num_threads];
1122 for (int i = 0; i < num_threads; i++) 1198 for (int i = 0; i < num_threads; i++)
1123 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); 1199 threads[i] = new Thread(StringPrintf("Thread %d", i).c_str());
1124 1200
1125 // Enable tracing. 1201 // Enable tracing.
1126 TraceLog::GetInstance()->SetEnabled(true); 1202 BeginTrace();
1127 1203
1128 // Now run some trace code on these threads. 1204 // Now run some trace code on these threads.
1129 WaitableEvent* task_complete_events[num_threads]; 1205 WaitableEvent* task_complete_events[num_threads];
1130 for (int i = 0; i < num_threads; i++) { 1206 for (int i = 0; i < num_threads; i++) {
1131 task_complete_events[i] = new WaitableEvent(false, false); 1207 task_complete_events[i] = new WaitableEvent(false, false);
1132 threads[i]->Start(); 1208 threads[i]->Start();
1133 thread_ids[i] = threads[i]->thread_id(); 1209 thread_ids[i] = threads[i]->thread_id();
1134 threads[i]->message_loop()->PostTask( 1210 threads[i]->message_loop()->PostTask(
1135 FROM_HERE, base::Bind(&TraceManyInstantEvents, 1211 FROM_HERE, base::Bind(&TraceManyInstantEvents,
1136 i, num_events, task_complete_events[i])); 1212 i, num_events, task_complete_events[i]));
1137 } 1213 }
1138 for (int i = 0; i < num_threads; i++) { 1214 for (int i = 0; i < num_threads; i++) {
1139 task_complete_events[i]->Wait(); 1215 task_complete_events[i]->Wait();
1140 } 1216 }
1141 1217
1142 // Shut things down. 1218 // Shut things down.
1143 for (int i = 0; i < num_threads; i++) { 1219 for (int i = 0; i < num_threads; i++) {
1144 threads[i]->Stop(); 1220 threads[i]->Stop();
1145 delete threads[i]; 1221 delete threads[i];
1146 delete task_complete_events[i]; 1222 delete task_complete_events[i];
1147 } 1223 }
1148 1224
1149 TraceLog::GetInstance()->SetEnabled(false); 1225 EndTraceAndFlush();
1150 1226
1151 std::string tmp; 1227 std::string tmp;
1152 int tmp_int; 1228 int tmp_int;
1153 const DictionaryValue* item; 1229 const DictionaryValue* item;
1154 1230
1155 // Make sure we get thread name metadata. 1231 // Make sure we get thread name metadata.
1156 // Note, the test suite may have created a ton of threads. 1232 // Note, the test suite may have created a ton of threads.
1157 // So, we'll have thread names for threads we didn't create. 1233 // So, we'll have thread names for threads we didn't create.
1158 std::vector<const DictionaryValue*> items = 1234 std::vector<const DictionaryValue*> items =
1159 FindTraceEntries(trace_parsed_, "thread_name"); 1235 FindTraceEntries(trace_parsed_, "thread_name");
(...skipping 15 matching lines...) Expand all
1175 // a comma-separated list of thread names, so look for a substring. 1251 // a comma-separated list of thread names, so look for a substring.
1176 EXPECT_TRUE(item->GetString("args.name", &tmp) && 1252 EXPECT_TRUE(item->GetString("args.name", &tmp) &&
1177 tmp.find(expected_name) != std::string::npos); 1253 tmp.find(expected_name) != std::string::npos);
1178 } 1254 }
1179 } 1255 }
1180 } 1256 }
1181 1257
1182 TEST_F(TraceEventTestFixture, ThreadNameChanges) { 1258 TEST_F(TraceEventTestFixture, ThreadNameChanges) {
1183 ManualTestSetUp(); 1259 ManualTestSetUp();
1184 1260
1185 TraceLog::GetInstance()->SetEnabled(true); 1261 BeginTrace();
1186 1262
1187 PlatformThread::SetName(""); 1263 PlatformThread::SetName("");
1188 TRACE_EVENT_INSTANT0("drink", "water"); 1264 TRACE_EVENT_INSTANT0("drink", "water");
1189 1265
1190 PlatformThread::SetName("cafe"); 1266 PlatformThread::SetName("cafe");
1191 TRACE_EVENT_INSTANT0("drink", "coffee"); 1267 TRACE_EVENT_INSTANT0("drink", "coffee");
1192 1268
1193 PlatformThread::SetName("shop"); 1269 PlatformThread::SetName("shop");
1194 // No event here, so won't appear in combined name. 1270 // No event here, so won't appear in combined name.
1195 1271
1196 PlatformThread::SetName("pub"); 1272 PlatformThread::SetName("pub");
1197 TRACE_EVENT_INSTANT0("drink", "beer"); 1273 TRACE_EVENT_INSTANT0("drink", "beer");
1198 TRACE_EVENT_INSTANT0("drink", "wine"); 1274 TRACE_EVENT_INSTANT0("drink", "wine");
1199 1275
1200 PlatformThread::SetName(" bar"); 1276 PlatformThread::SetName(" bar");
1201 TRACE_EVENT_INSTANT0("drink", "whisky"); 1277 TRACE_EVENT_INSTANT0("drink", "whisky");
1202 1278
1203 TraceLog::GetInstance()->SetEnabled(false); 1279 EndTraceAndFlush();
1204 1280
1205 std::vector<const DictionaryValue*> items = 1281 std::vector<const DictionaryValue*> items =
1206 FindTraceEntries(trace_parsed_, "thread_name"); 1282 FindTraceEntries(trace_parsed_, "thread_name");
1207 EXPECT_EQ(1u, items.size()); 1283 EXPECT_EQ(1u, items.size());
1208 ASSERT_GT(items.size(), 0u); 1284 ASSERT_GT(items.size(), 0u);
1209 const DictionaryValue* item = items[0]; 1285 const DictionaryValue* item = items[0];
1210 ASSERT_TRUE(item); 1286 ASSERT_TRUE(item);
1211 int tid; 1287 int tid;
1212 EXPECT_TRUE(item->GetInteger("tid", &tid)); 1288 EXPECT_TRUE(item->GetInteger("tid", &tid));
1213 EXPECT_EQ(PlatformThread::CurrentId(), static_cast<PlatformThreadId>(tid)); 1289 EXPECT_EQ(PlatformThread::CurrentId(), static_cast<PlatformThreadId>(tid));
(...skipping 18 matching lines...) Expand all
1232 // Scope to contain the then destroy the TraceLog singleton. 1308 // Scope to contain the then destroy the TraceLog singleton.
1233 { 1309 {
1234 base::ShadowingAtExitManager exit_manager_will_destroy_singletons; 1310 base::ShadowingAtExitManager exit_manager_will_destroy_singletons;
1235 1311
1236 // Setup TraceLog singleton inside this test's exit manager scope 1312 // Setup TraceLog singleton inside this test's exit manager scope
1237 // so that it will be destroyed when this scope closes. 1313 // so that it will be destroyed when this scope closes.
1238 ManualTestSetUp(); 1314 ManualTestSetUp();
1239 1315
1240 TRACE_EVENT_INSTANT0("all", "not recorded; system not enabled"); 1316 TRACE_EVENT_INSTANT0("all", "not recorded; system not enabled");
1241 1317
1242 TraceLog::GetInstance()->SetEnabled(true); 1318 BeginTrace();
1243 1319
1244 TRACE_EVENT_INSTANT0("all", "is recorded 1; system has been enabled"); 1320 TRACE_EVENT_INSTANT0("all", "is recorded 1; system has been enabled");
1245 // Trace calls that will cache pointers to categories; they're valid here 1321 // Trace calls that will cache pointers to categories; they're valid here
1246 TraceCallsWithCachedCategoryPointersPointers( 1322 TraceCallsWithCachedCategoryPointersPointers(
1247 "is recorded 2; system has been enabled"); 1323 "is recorded 2; system has been enabled");
1248 1324
1249 TraceLog::GetInstance()->SetEnabled(false); 1325 EndTraceAndFlush();
1250 } // scope to destroy singleton 1326 } // scope to destroy singleton
1251 ASSERT_FALSE(TraceLog::GetInstance()); 1327 ASSERT_FALSE(TraceLog::GetInstance());
1252 1328
1253 // Now that singleton is destroyed, check what trace events were recorded 1329 // Now that singleton is destroyed, check what trace events were recorded
1254 const DictionaryValue* item = NULL; 1330 const DictionaryValue* item = NULL;
1255 ListValue& trace_parsed = trace_parsed_; 1331 ListValue& trace_parsed = trace_parsed_;
1256 EXPECT_FIND_("is recorded 1"); 1332 EXPECT_FIND_("is recorded 1");
1257 EXPECT_FIND_("is recorded 2"); 1333 EXPECT_FIND_("is recorded 2");
1258 EXPECT_NOT_FIND_("not recorded"); 1334 EXPECT_NOT_FIND_("not recorded");
1259 1335
(...skipping 20 matching lines...) Expand all
1280 } 1356 }
1281 1357
1282 TEST_F(TraceEventTestFixture, NormallyNoDeepCopy) { 1358 TEST_F(TraceEventTestFixture, NormallyNoDeepCopy) {
1283 // Test that the TRACE_EVENT macros do not deep-copy their string. If they 1359 // Test that the TRACE_EVENT macros do not deep-copy their string. If they
1284 // do so it may indicate a performance regression, but more-over it would 1360 // do so it may indicate a performance regression, but more-over it would
1285 // make the DEEP_COPY overloads redundant. 1361 // make the DEEP_COPY overloads redundant.
1286 ManualTestSetUp(); 1362 ManualTestSetUp();
1287 1363
1288 std::string name_string("event name"); 1364 std::string name_string("event name");
1289 1365
1290 TraceLog::GetInstance()->SetEnabled(true); 1366 BeginTrace();
1291 TRACE_EVENT_INSTANT0("category", name_string.c_str()); 1367 TRACE_EVENT_INSTANT0("category", name_string.c_str());
1292 1368
1293 // Modify the string in place (a wholesale reassignment may leave the old 1369 // Modify the string in place (a wholesale reassignment may leave the old
1294 // string intact on the heap). 1370 // string intact on the heap).
1295 name_string[0] = '@'; 1371 name_string[0] = '@';
1296 1372
1297 TraceLog::GetInstance()->SetEnabled(false); 1373 EndTraceAndFlush();
1298 1374
1299 EXPECT_FALSE(FindTraceEntry(trace_parsed_, "event name")); 1375 EXPECT_FALSE(FindTraceEntry(trace_parsed_, "event name"));
1300 EXPECT_TRUE(FindTraceEntry(trace_parsed_, name_string.c_str())); 1376 EXPECT_TRUE(FindTraceEntry(trace_parsed_, name_string.c_str()));
1301 } 1377 }
1302 1378
1303 TEST_F(TraceEventTestFixture, DeepCopy) { 1379 TEST_F(TraceEventTestFixture, DeepCopy) {
1304 ManualTestSetUp(); 1380 ManualTestSetUp();
1305 1381
1306 static const char kOriginalName1[] = "name1"; 1382 static const char kOriginalName1[] = "name1";
1307 static const char kOriginalName2[] = "name2"; 1383 static const char kOriginalName2[] = "name2";
1308 static const char kOriginalName3[] = "name3"; 1384 static const char kOriginalName3[] = "name3";
1309 std::string name1(kOriginalName1); 1385 std::string name1(kOriginalName1);
1310 std::string name2(kOriginalName2); 1386 std::string name2(kOriginalName2);
1311 std::string name3(kOriginalName3); 1387 std::string name3(kOriginalName3);
1312 std::string arg1("arg1"); 1388 std::string arg1("arg1");
1313 std::string arg2("arg2"); 1389 std::string arg2("arg2");
1314 std::string val1("val1"); 1390 std::string val1("val1");
1315 std::string val2("val2"); 1391 std::string val2("val2");
1316 1392
1317 TraceLog::GetInstance()->SetEnabled(true); 1393 BeginTrace();
1318 TRACE_EVENT_COPY_INSTANT0("category", name1.c_str()); 1394 TRACE_EVENT_COPY_INSTANT0("category", name1.c_str());
1319 TRACE_EVENT_COPY_BEGIN1("category", name2.c_str(), 1395 TRACE_EVENT_COPY_BEGIN1("category", name2.c_str(),
1320 arg1.c_str(), 5); 1396 arg1.c_str(), 5);
1321 TRACE_EVENT_COPY_END2("category", name3.c_str(), 1397 TRACE_EVENT_COPY_END2("category", name3.c_str(),
1322 arg1.c_str(), val1, 1398 arg1.c_str(), val1,
1323 arg2.c_str(), val2); 1399 arg2.c_str(), val2);
1324 1400
1325 // As per NormallyNoDeepCopy, modify the strings in place. 1401 // As per NormallyNoDeepCopy, modify the strings in place.
1326 name1[0] = name2[0] = name3[0] = arg1[0] = arg2[0] = val1[0] = val2[0] = '@'; 1402 name1[0] = name2[0] = name3[0] = arg1[0] = arg2[0] = val1[0] = val2[0] = '@';
1327 1403
1328 TraceLog::GetInstance()->SetEnabled(false); 1404 EndTraceAndFlush();
1329 1405
1330 EXPECT_FALSE(FindTraceEntry(trace_parsed_, name1.c_str())); 1406 EXPECT_FALSE(FindTraceEntry(trace_parsed_, name1.c_str()));
1331 EXPECT_FALSE(FindTraceEntry(trace_parsed_, name2.c_str())); 1407 EXPECT_FALSE(FindTraceEntry(trace_parsed_, name2.c_str()));
1332 EXPECT_FALSE(FindTraceEntry(trace_parsed_, name3.c_str())); 1408 EXPECT_FALSE(FindTraceEntry(trace_parsed_, name3.c_str()));
1333 1409
1334 const DictionaryValue* entry1 = FindTraceEntry(trace_parsed_, kOriginalName1); 1410 const DictionaryValue* entry1 = FindTraceEntry(trace_parsed_, kOriginalName1);
1335 const DictionaryValue* entry2 = FindTraceEntry(trace_parsed_, kOriginalName2); 1411 const DictionaryValue* entry2 = FindTraceEntry(trace_parsed_, kOriginalName2);
1336 const DictionaryValue* entry3 = FindTraceEntry(trace_parsed_, kOriginalName3); 1412 const DictionaryValue* entry3 = FindTraceEntry(trace_parsed_, kOriginalName3);
1337 ASSERT_TRUE(entry1); 1413 ASSERT_TRUE(entry1);
1338 ASSERT_TRUE(entry2); 1414 ASSERT_TRUE(entry2);
(...skipping 28 matching lines...) Expand all
1367 Clear(); 1443 Clear();
1368 1444
1369 trace_buffer_.Start(); 1445 trace_buffer_.Start();
1370 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4"); 1446 trace_buffer_.AddFragment("bla1,bla2,bla3,bla4");
1371 trace_buffer_.Finish(); 1447 trace_buffer_.Finish();
1372 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]"); 1448 EXPECT_STREQ(json_output_.json_output.c_str(), "[bla1,bla2,bla3,bla4]");
1373 } 1449 }
1374 1450
1375 } // namespace debug 1451 } // namespace debug
1376 } // namespace base 1452 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698