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

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

Issue 8355024: Internalize JSON chunk management to trace_event.h API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 22 matching lines...) Expand all
33 const char* value; 33 const char* value;
34 CompareOp op; 34 CompareOp op;
35 }; 35 };
36 36
37 class TraceEventTestFixture : public testing::Test { 37 class TraceEventTestFixture : public testing::Test {
38 public: 38 public:
39 // This fixture does not use SetUp() because the fixture must be manually set 39 // This fixture does not use SetUp() because the fixture must be manually set
40 // up multiple times when testing AtExit. Use ManualTestSetUp for this. 40 // up multiple times when testing AtExit. Use ManualTestSetUp for this.
41 void ManualTestSetUp(); 41 void ManualTestSetUp();
42 void OnTraceDataCollected( 42 void OnTraceDataCollected(
43 scoped_refptr<TraceLog::RefCountedString> json_events_str); 43 scoped_refptr<TraceLog::RefCountedString> events_str);
44 bool FindMatchingTraceEntry(const JsonKeyValue* key_values); 44 bool FindMatchingTraceEntry(const JsonKeyValue* key_values);
45 bool FindNamePhase(const char* name, const char* phase); 45 bool FindNamePhase(const char* name, const char* phase);
46 bool FindMatchingValue(const char* key, 46 bool FindMatchingValue(const char* key,
47 const char* value); 47 const char* value);
48 bool FindNonMatchingValue(const char* key, 48 bool FindNonMatchingValue(const char* key,
49 const char* value); 49 const char* value);
50 void Clear() { 50 void Clear() {
51 trace_string_.clear();
52 trace_parsed_.Clear(); 51 trace_parsed_.Clear();
53 } 52 }
54 53
55 std::string trace_string_;
56 ListValue trace_parsed_; 54 ListValue trace_parsed_;
57 55
58 private: 56 private:
59 // We want our singleton torn down after each test. 57 // We want our singleton torn down after each test.
60 ShadowingAtExitManager at_exit_manager_; 58 ShadowingAtExitManager at_exit_manager_;
61 Lock lock_; 59 Lock lock_;
62 }; 60 };
63 61
64 void TraceEventTestFixture::ManualTestSetUp() { 62 void TraceEventTestFixture::ManualTestSetUp() {
65 TraceLog::DeleteForTesting(); 63 TraceLog::DeleteForTesting();
66 TraceLog::Resurrect(); 64 TraceLog::Resurrect();
67 TraceLog* tracelog = TraceLog::GetInstance(); 65 TraceLog* tracelog = TraceLog::GetInstance();
68 ASSERT_TRUE(tracelog); 66 ASSERT_TRUE(tracelog);
69 ASSERT_FALSE(tracelog->IsEnabled()); 67 ASSERT_FALSE(tracelog->IsEnabled());
70 tracelog->SetOutputCallback( 68 tracelog->SetOutputCallback(
71 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, 69 base::Bind(&TraceEventTestFixture::OnTraceDataCollected,
72 base::Unretained(this))); 70 base::Unretained(this)));
73 } 71 }
74 72
75 void TraceEventTestFixture::OnTraceDataCollected( 73 void TraceEventTestFixture::OnTraceDataCollected(
76 scoped_refptr<TraceLog::RefCountedString> json_events_str) { 74 scoped_refptr<TraceLog::RefCountedString> events_str) {
77 AutoLock lock(lock_); 75 AutoLock lock(lock_);
78 trace_string_ += json_events_str->data; 76 std::string json_events;
77 base::debug::TraceResultBuffer trace_buffer_;
78 trace_buffer_.AddFragment(events_str->data);
79 trace_buffer_.GetJSON(&json_events);
79 80
80 scoped_ptr<Value> root; 81 scoped_ptr<Value> root;
81 root.reset(base::JSONReader::Read(json_events_str->data, false)); 82 root.reset(base::JSONReader::Read(json_events, false));
82 83
83 ListValue* root_list = NULL; 84 ListValue* root_list = NULL;
84 ASSERT_TRUE(root.get()); 85 ASSERT_TRUE(root.get());
85 ASSERT_TRUE(root->GetAsList(&root_list)); 86 ASSERT_TRUE(root->GetAsList(&root_list));
86 87
87 // Move items into our aggregate collection 88 // Move items into our aggregate collection
88 while (root_list->GetSize()) { 89 while (root_list->GetSize()) {
89 Value* item = NULL; 90 Value* item = NULL;
90 root_list->Remove(0, &item); 91 root_list->Remove(0, &item);
91 trace_parsed_.Append(item); 92 trace_parsed_.Append(item);
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 TRACE_EVENT_END1("all", "TRACE_EVENT_END1 call", "name1", "value1"); 275 TRACE_EVENT_END1("all", "TRACE_EVENT_END1 call", "name1", "value1");
275 TRACE_EVENT_END2("all", "TRACE_EVENT_END2 call", 276 TRACE_EVENT_END2("all", "TRACE_EVENT_END2 call",
276 "name1", "value1", 277 "name1", "value1",
277 "name2", "value2"); 278 "name2", "value2");
278 } // Scope close causes TRACE_EVENT0 etc to send their END events. 279 } // Scope close causes TRACE_EVENT0 etc to send their END events.
279 280
280 if (task_complete_event) 281 if (task_complete_event)
281 task_complete_event->Signal(); 282 task_complete_event->Signal();
282 } 283 }
283 284
284 void ValidateAllTraceMacrosCreatedData(const ListValue& trace_parsed, 285 void ValidateAllTraceMacrosCreatedData(const ListValue& trace_parsed) {
285 const std::string& trace_string) {
286 DictionaryValue* item = NULL; 286 DictionaryValue* item = NULL;
287 287
288 #define EXPECT_FIND_(string) \ 288 #define EXPECT_FIND_(string) \
289 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); 289 EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string)));
290 #define EXPECT_NOT_FIND_(string) \ 290 #define EXPECT_NOT_FIND_(string) \
291 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); 291 EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string)));
292 #define EXPECT_SUB_FIND_(string) \ 292 #define EXPECT_SUB_FIND_(string) \
293 if (item) EXPECT_TRUE((IsStringInDict(string, item))); 293 if (item) EXPECT_TRUE((IsStringInDict(string, item)));
294 294
295 EXPECT_FIND_("ETW Trace Event"); 295 EXPECT_FIND_("ETW Trace Event");
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 EXPECT_FIND_("TRACE_EVENT_END0 call"); 338 EXPECT_FIND_("TRACE_EVENT_END0 call");
339 EXPECT_FIND_("TRACE_EVENT_END1 call"); 339 EXPECT_FIND_("TRACE_EVENT_END1 call");
340 EXPECT_FIND_("TRACE_EVENT_END2 call"); 340 EXPECT_FIND_("TRACE_EVENT_END2 call");
341 EXPECT_SUB_FIND_("name1"); 341 EXPECT_SUB_FIND_("name1");
342 EXPECT_SUB_FIND_("value1"); 342 EXPECT_SUB_FIND_("value1");
343 EXPECT_SUB_FIND_("name2"); 343 EXPECT_SUB_FIND_("name2");
344 EXPECT_SUB_FIND_("value2"); 344 EXPECT_SUB_FIND_("value2");
345 } 345 }
346 346
347 void TraceManyInstantEvents(int thread_id, int num_events, 347 void TraceManyInstantEvents(int thread_id, int num_events,
348 WaitableEvent* task_complete_event) { 348 WaitableEvent* task_complete_event) {
349 for (int i = 0; i < num_events; i++) { 349 for (int i = 0; i < num_events; i++) {
350 TRACE_EVENT_INSTANT2("all", "multi thread event", 350 TRACE_EVENT_INSTANT2("all", "multi thread event",
351 "thread", thread_id, 351 "thread", thread_id,
352 "event", i); 352 "event", i);
353 } 353 }
354 354
355 if (task_complete_event) 355 if (task_complete_event)
356 task_complete_event->Signal(); 356 task_complete_event->Signal();
357 } 357 }
358 358
359 void ValidateInstantEventPresentOnEveryThread(const ListValue& trace_parsed, 359 void ValidateInstantEventPresentOnEveryThread(const ListValue& trace_parsed,
360 const std::string& trace_string, 360 int num_threads,
361 int num_threads, int num_events) { 361 int num_events) {
362 std::map<int, std::map<int, bool> > results; 362 std::map<int, std::map<int, bool> > results;
363 363
364 size_t trace_parsed_count = trace_parsed.GetSize(); 364 size_t trace_parsed_count = trace_parsed.GetSize();
365 for (size_t i = 0; i < trace_parsed_count; i++) { 365 for (size_t i = 0; i < trace_parsed_count; i++) {
366 Value* value = NULL; 366 Value* value = NULL;
367 trace_parsed.Get(i, &value); 367 trace_parsed.Get(i, &value);
368 if (!value || value->GetType() != Value::TYPE_DICTIONARY) 368 if (!value || value->GetType() != Value::TYPE_DICTIONARY)
369 continue; 369 continue;
370 DictionaryValue* dict = static_cast<DictionaryValue*>(value); 370 DictionaryValue* dict = static_cast<DictionaryValue*>(value);
371 std::string name; 371 std::string name;
(...skipping 27 matching lines...) Expand all
399 399
400 // Simple Test for emitting data and validating it was received. 400 // Simple Test for emitting data and validating it was received.
401 TEST_F(TraceEventTestFixture, DataCaptured) { 401 TEST_F(TraceEventTestFixture, DataCaptured) {
402 ManualTestSetUp(); 402 ManualTestSetUp();
403 TraceLog::GetInstance()->SetEnabled(true); 403 TraceLog::GetInstance()->SetEnabled(true);
404 404
405 TraceWithAllMacroVariants(NULL); 405 TraceWithAllMacroVariants(NULL);
406 406
407 TraceLog::GetInstance()->SetEnabled(false); 407 TraceLog::GetInstance()->SetEnabled(false);
408 408
409 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); 409 ValidateAllTraceMacrosCreatedData(trace_parsed_);
410 } 410 }
411 411
412 // Test that categories work. 412 // Test that categories work.
413 TEST_F(TraceEventTestFixture, Categories) { 413 TEST_F(TraceEventTestFixture, Categories) {
414 ManualTestSetUp(); 414 ManualTestSetUp();
415 415
416 // Test that categories that are used can be retrieved whether trace was 416 // Test that categories that are used can be retrieved whether trace was
417 // enabled or disabled when the trace event was encountered. 417 // enabled or disabled when the trace event was encountered.
418 TRACE_EVENT_INSTANT0("c1", "name"); 418 TRACE_EVENT_INSTANT0("c1", "name");
419 TRACE_EVENT_INSTANT0("c2", "name"); 419 TRACE_EVENT_INSTANT0("c2", "name");
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 Thread thread("1"); 680 Thread thread("1");
681 WaitableEvent task_complete_event(false, false); 681 WaitableEvent task_complete_event(false, false);
682 thread.Start(); 682 thread.Start();
683 683
684 thread.message_loop()->PostTask( 684 thread.message_loop()->PostTask(
685 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event)); 685 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event));
686 task_complete_event.Wait(); 686 task_complete_event.Wait();
687 thread.Stop(); 687 thread.Stop();
688 688
689 TraceLog::GetInstance()->SetEnabled(false); 689 TraceLog::GetInstance()->SetEnabled(false);
690 ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); 690 ValidateAllTraceMacrosCreatedData(trace_parsed_);
691 } 691 }
692 692
693 // Test that data sent from multiple threads is gathered 693 // Test that data sent from multiple threads is gathered
694 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { 694 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) {
695 ManualTestSetUp(); 695 ManualTestSetUp();
696 TraceLog::GetInstance()->SetEnabled(true); 696 TraceLog::GetInstance()->SetEnabled(true);
697 697
698 const int num_threads = 4; 698 const int num_threads = 4;
699 const int num_events = 4000; 699 const int num_events = 4000;
700 Thread* threads[num_threads]; 700 Thread* threads[num_threads];
(...skipping 12 matching lines...) Expand all
713 } 713 }
714 714
715 for (int i = 0; i < num_threads; i++) { 715 for (int i = 0; i < num_threads; i++) {
716 threads[i]->Stop(); 716 threads[i]->Stop();
717 delete threads[i]; 717 delete threads[i];
718 delete task_complete_events[i]; 718 delete task_complete_events[i];
719 } 719 }
720 720
721 TraceLog::GetInstance()->SetEnabled(false); 721 TraceLog::GetInstance()->SetEnabled(false);
722 722
723 ValidateInstantEventPresentOnEveryThread(trace_parsed_, trace_string_, 723 ValidateInstantEventPresentOnEveryThread(trace_parsed_,
724 num_threads, num_events); 724 num_threads, num_events);
725 } 725 }
726 726
nduca 2011/10/20 00:28:03 I can haz a unit test for the trace buffer?
jbates 2011/10/20 22:18:49 Done.
727 // Test that thread and process names show up in the trace 727 // Test that thread and process names show up in the trace
728 TEST_F(TraceEventTestFixture, ThreadNames) { 728 TEST_F(TraceEventTestFixture, ThreadNames) {
729 ManualTestSetUp(); 729 ManualTestSetUp();
730 730
731 // Create threads before we enable tracing to make sure 731 // Create threads before we enable tracing to make sure
732 // that tracelog still captures them. 732 // that tracelog still captures them.
733 const int num_threads = 4; 733 const int num_threads = 4;
734 const int num_events = 10; 734 const int num_events = 10;
735 Thread* threads[num_threads]; 735 Thread* threads[num_threads];
736 PlatformThreadId thread_ids[num_threads]; 736 PlatformThreadId thread_ids[num_threads];
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
920 920
921 std::string s; 921 std::string s;
922 EXPECT_TRUE(entry3->GetString("args.arg1", &s)); 922 EXPECT_TRUE(entry3->GetString("args.arg1", &s));
923 EXPECT_EQ("val1", s); 923 EXPECT_EQ("val1", s);
924 EXPECT_TRUE(entry3->GetString("args.arg2", &s)); 924 EXPECT_TRUE(entry3->GetString("args.arg2", &s));
925 EXPECT_EQ("val2", s); 925 EXPECT_EQ("val2", s);
926 } 926 }
927 927
928 } // namespace debug 928 } // namespace debug
929 } // namespace base 929 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698