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

Side by Side Diff: components/metrics/call_stack_profile_metrics_provider_unittest.cc

Issue 2927593002: Make stack sampling profiler sample beyond startup. (Closed)
Patch Set: Remove debug log statements. Created 3 years, 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/metrics/call_stack_profile_metrics_provider.h" 5 #include "components/metrics/call_stack_profile_metrics_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/profiler/stack_sampling_profiler.h" 13 #include "base/profiler/stack_sampling_profiler.h"
15 #include "base/run_loop.h" 14 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/test/scoped_feature_list.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "components/metrics/call_stack_profile_params.h" 18 #include "components/metrics/call_stack_profile_params.h"
19 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" 19 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
20 #include "components/variations/entropy_provider.h" 20 #include "components/variations/entropy_provider.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using base::StackSamplingProfiler; 23 using base::StackSamplingProfiler;
24 using Frame = StackSamplingProfiler::Frame; 24 using Frame = StackSamplingProfiler::Frame;
25 using Module = StackSamplingProfiler::Module; 25 using Module = StackSamplingProfiler::Module;
26 using Profile = StackSamplingProfiler::CallStackProfile; 26 using Profile = StackSamplingProfiler::CallStackProfile;
(...skipping 24 matching lines...) Expand all
51 int32_t duration_ms; 51 int32_t duration_ms;
52 int32_t period_ms; 52 int32_t period_ms;
53 const ExpectedProtoModule* modules; 53 const ExpectedProtoModule* modules;
54 int module_count; 54 int module_count;
55 const ExpectedProtoSample* samples; 55 const ExpectedProtoSample* samples;
56 int sample_count; 56 int sample_count;
57 }; 57 };
58 58
59 class ProfilesFactory { 59 class ProfilesFactory {
60 public: 60 public:
61 ProfilesFactory(){}; 61 ProfilesFactory() {}
62 ~ProfilesFactory(){}; 62 ~ProfilesFactory() {}
63 63
64 ProfilesFactory& AddMilestone(int milestone); 64 ProfilesFactory& AddMilestone(int milestone);
65 ProfilesFactory& NewProfile(int duration_ms, int interval_ms); 65 ProfilesFactory& NewProfile(int duration_ms, int interval_ms);
66 ProfilesFactory& NewSample(); 66 ProfilesFactory& NewSample();
67 ProfilesFactory& AddFrame(size_t module, uintptr_t offset); 67 ProfilesFactory& AddFrame(size_t module, uintptr_t offset);
68 ProfilesFactory& DefineModule(const char* name, 68 ProfilesFactory& DefineModule(const char* name,
69 const base::FilePath& path, 69 const base::FilePath& path,
70 uintptr_t base); 70 uintptr_t base);
71 71
72 Profiles Build(); 72 Profiles Build();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 } 110 }
111 111
112 Profiles ProfilesFactory::Build() { 112 Profiles ProfilesFactory::Build() {
113 return std::move(profiles_); 113 return std::move(profiles_);
114 } 114 }
115 115
116 } // namespace 116 } // namespace
117 117
118 namespace metrics { 118 namespace metrics {
119 119
120 // This test fixture enables the field trial that 120 // This test fixture enables the feature that
121 // CallStackProfileMetricsProvider depends on to report profiles. 121 // CallStackProfileMetricsProvider depends on to report profiles.
122 class CallStackProfileMetricsProviderTest : public testing::Test { 122 class CallStackProfileMetricsProviderTest : public testing::Test {
123 public: 123 public:
124 CallStackProfileMetricsProviderTest() 124 CallStackProfileMetricsProviderTest() {
125 : field_trial_list_(nullptr) { 125 scoped_feature_list_.InitAndEnableFeature(TestState::kEnableReporting);
126 base::FieldTrialList::CreateFieldTrial(
127 TestState::kFieldTrialName,
128 TestState::kReportProfilesGroupName);
129 TestState::ResetStaticStateForTesting(); 126 TestState::ResetStaticStateForTesting();
130 } 127 }
131 128
132 ~CallStackProfileMetricsProviderTest() override {} 129 ~CallStackProfileMetricsProviderTest() override {}
133 130
134 // Utility function to append profiles to the metrics provider. 131 // Utility function to append profiles to the metrics provider.
135 void AppendProfiles(const CallStackProfileParams& params, Profiles profiles) { 132 void AppendProfiles(CallStackProfileParams* params, Profiles profiles) {
136 CallStackProfileMetricsProvider::GetProfilerCallback(params).Run( 133 internal::GetProfilerCallback(params).Run(std::move(profiles));
137 std::move(profiles));
138 } 134 }
139 135
140 void VerifyProfileProto(const ExpectedProtoProfile& expected, 136 void VerifyProfileProto(const ExpectedProtoProfile& expected,
141 const SampledProfile& proto); 137 const SampledProfile& proto);
142 138
143 private: 139 private:
144 // Exposes field trial/group names from the CallStackProfileMetricsProvider. 140 // Exposes the feature from the CallStackProfileMetricsProvider.
145 class TestState : public CallStackProfileMetricsProvider { 141 class TestState : public CallStackProfileMetricsProvider {
146 public: 142 public:
147 using CallStackProfileMetricsProvider::kFieldTrialName; 143 using CallStackProfileMetricsProvider::kEnableReporting;
148 using CallStackProfileMetricsProvider::kReportProfilesGroupName;
149 using CallStackProfileMetricsProvider::ResetStaticStateForTesting; 144 using CallStackProfileMetricsProvider::ResetStaticStateForTesting;
150 }; 145 };
151 146
152 base::FieldTrialList field_trial_list_; 147 base::test::ScopedFeatureList scoped_feature_list_;
153 148
154 DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProviderTest); 149 DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProviderTest);
155 }; 150 };
156 151
157 void CallStackProfileMetricsProviderTest::VerifyProfileProto( 152 void CallStackProfileMetricsProviderTest::VerifyProfileProto(
158 const ExpectedProtoProfile& expected, 153 const ExpectedProtoProfile& expected,
159 const SampledProfile& proto) { 154 const SampledProfile& proto) {
160 ASSERT_TRUE(proto.has_call_stack_profile()); 155 ASSERT_TRUE(proto.has_call_stack_profile());
161 const CallStackProfile& stack = proto.call_stack_profile(); 156 const CallStackProfile& stack = proto.call_stack_profile();
162 157
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 200, 20, 331 200, 20,
337 expected_proto_modules2, arraysize(expected_proto_modules2), 332 expected_proto_modules2, arraysize(expected_proto_modules2),
338 expected_proto_samples2, arraysize(expected_proto_samples2), 333 expected_proto_samples2, arraysize(expected_proto_samples2),
339 }, 334 },
340 }; 335 };
341 336
342 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size()); 337 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
343 338
344 CallStackProfileMetricsProvider provider; 339 CallStackProfileMetricsProvider provider;
345 provider.OnRecordingEnabled(); 340 provider.OnRecordingEnabled();
346 AppendProfiles( 341 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
347 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 342 CallStackProfileParams::UI_THREAD,
348 CallStackProfileParams::UI_THREAD, 343 CallStackProfileParams::PROCESS_STARTUP,
349 CallStackProfileParams::PROCESS_STARTUP, 344 CallStackProfileParams::MAY_SHUFFLE);
350 CallStackProfileParams::MAY_SHUFFLE), 345 AppendProfiles(&params, std::move(profiles));
351 std::move(profiles));
352 ChromeUserMetricsExtension uma_proto; 346 ChromeUserMetricsExtension uma_proto;
353 provider.ProvideGeneralMetrics(&uma_proto); 347 provider.ProvideGeneralMetrics(&uma_proto);
354 348
355 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)), 349 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
356 uma_proto.sampled_profile().size()); 350 uma_proto.sampled_profile().size());
357 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) { 351 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
358 SCOPED_TRACE("profile " + base::SizeTToString(p)); 352 SCOPED_TRACE("profile " + base::SizeTToString(p));
359 VerifyProfileProto(expected_proto_profiles[p], 353 VerifyProfileProto(expected_proto_profiles[p],
360 uma_proto.sampled_profile().Get(p)); 354 uma_proto.sampled_profile().Get(p));
361 } 355 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 100, 10, 415 100, 10,
422 expected_proto_modules, arraysize(expected_proto_modules), 416 expected_proto_modules, arraysize(expected_proto_modules),
423 expected_proto_samples, arraysize(expected_proto_samples), 417 expected_proto_samples, arraysize(expected_proto_samples),
424 }, 418 },
425 }; 419 };
426 420
427 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size()); 421 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
428 422
429 CallStackProfileMetricsProvider provider; 423 CallStackProfileMetricsProvider provider;
430 provider.OnRecordingEnabled(); 424 provider.OnRecordingEnabled();
431 AppendProfiles( 425 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
432 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 426 CallStackProfileParams::UI_THREAD,
433 CallStackProfileParams::UI_THREAD, 427 CallStackProfileParams::PROCESS_STARTUP,
434 CallStackProfileParams::PROCESS_STARTUP, 428 CallStackProfileParams::MAY_SHUFFLE);
435 CallStackProfileParams::MAY_SHUFFLE), 429 AppendProfiles(&params, std::move(profiles));
436 std::move(profiles));
437 ChromeUserMetricsExtension uma_proto; 430 ChromeUserMetricsExtension uma_proto;
438 provider.ProvideGeneralMetrics(&uma_proto); 431 provider.ProvideGeneralMetrics(&uma_proto);
439 432
440 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)), 433 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
441 uma_proto.sampled_profile().size()); 434 uma_proto.sampled_profile().size());
442 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) { 435 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
443 SCOPED_TRACE("profile " + base::SizeTToString(p)); 436 SCOPED_TRACE("profile " + base::SizeTToString(p));
444 VerifyProfileProto(expected_proto_profiles[p], 437 VerifyProfileProto(expected_proto_profiles[p],
445 uma_proto.sampled_profile().Get(p)); 438 uma_proto.sampled_profile().Get(p));
446 } 439 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 100, 10, 501 100, 10,
509 expected_proto_modules, arraysize(expected_proto_modules), 502 expected_proto_modules, arraysize(expected_proto_modules),
510 expected_proto_samples, arraysize(expected_proto_samples), 503 expected_proto_samples, arraysize(expected_proto_samples),
511 }, 504 },
512 }; 505 };
513 506
514 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size()); 507 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
515 508
516 CallStackProfileMetricsProvider provider; 509 CallStackProfileMetricsProvider provider;
517 provider.OnRecordingEnabled(); 510 provider.OnRecordingEnabled();
518 AppendProfiles(CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 511 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
519 CallStackProfileParams::UI_THREAD, 512 CallStackProfileParams::UI_THREAD,
520 CallStackProfileParams::PROCESS_STARTUP, 513 CallStackProfileParams::PROCESS_STARTUP,
521 CallStackProfileParams::PRESERVE_ORDER), 514 CallStackProfileParams::PRESERVE_ORDER);
522 std::move(profiles)); 515 AppendProfiles(&params, std::move(profiles));
523 ChromeUserMetricsExtension uma_proto; 516 ChromeUserMetricsExtension uma_proto;
524 provider.ProvideGeneralMetrics(&uma_proto); 517 provider.ProvideGeneralMetrics(&uma_proto);
525 518
526 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)), 519 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
527 uma_proto.sampled_profile().size()); 520 uma_proto.sampled_profile().size());
528 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) { 521 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
529 SCOPED_TRACE("profile " + base::SizeTToString(p)); 522 SCOPED_TRACE("profile " + base::SizeTToString(p));
530 VerifyProfileProto(expected_proto_profiles[p], 523 VerifyProfileProto(expected_proto_profiles[p],
531 uma_proto.sampled_profile().Get(p)); 524 uma_proto.sampled_profile().Get(p));
532 } 525 }
(...skipping 19 matching lines...) Expand all
552 100, 10, 545 100, 10,
553 nullptr, 0, 546 nullptr, 0,
554 expected_proto_samples, arraysize(expected_proto_samples), 547 expected_proto_samples, arraysize(expected_proto_samples),
555 }, 548 },
556 }; 549 };
557 550
558 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size()); 551 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
559 552
560 CallStackProfileMetricsProvider provider; 553 CallStackProfileMetricsProvider provider;
561 provider.OnRecordingEnabled(); 554 provider.OnRecordingEnabled();
562 AppendProfiles( 555 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
563 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 556 CallStackProfileParams::UI_THREAD,
564 CallStackProfileParams::UI_THREAD, 557 CallStackProfileParams::PROCESS_STARTUP,
565 CallStackProfileParams::PROCESS_STARTUP, 558 CallStackProfileParams::MAY_SHUFFLE);
566 CallStackProfileParams::MAY_SHUFFLE), 559 AppendProfiles(&params, std::move(profiles));
567 std::move(profiles));
568 ChromeUserMetricsExtension uma_proto; 560 ChromeUserMetricsExtension uma_proto;
569 provider.ProvideGeneralMetrics(&uma_proto); 561 provider.ProvideGeneralMetrics(&uma_proto);
570 562
571 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)), 563 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
572 uma_proto.sampled_profile().size()); 564 uma_proto.sampled_profile().size());
573 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) { 565 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
574 SCOPED_TRACE("profile " + base::SizeTToString(p)); 566 SCOPED_TRACE("profile " + base::SizeTToString(p));
575 VerifyProfileProto(expected_proto_profiles[p], 567 VerifyProfileProto(expected_proto_profiles[p],
576 uma_proto.sampled_profile().Get(p)); 568 uma_proto.sampled_profile().Get(p));
577 } 569 }
578 } 570 }
579 571
580 // Checks that pending profiles are only passed back to ProvideGeneralMetrics 572 // Checks that pending profiles are only passed back to ProvideGeneralMetrics
581 // once. 573 // once.
582 TEST_F(CallStackProfileMetricsProviderTest, ProfilesProvidedOnlyOnce) { 574 TEST_F(CallStackProfileMetricsProviderTest, ProfilesProvidedOnlyOnce) {
583 CallStackProfileMetricsProvider provider; 575 CallStackProfileMetricsProvider provider;
584 for (int r = 0; r < 2; ++r) { 576 for (int r = 0; r < 2; ++r) {
585 Profiles profiles = ProfilesFactory() 577 Profiles profiles = ProfilesFactory()
586 // Use the sampling period to distinguish the two profiles. 578 // Use the sampling period to distinguish the two profiles.
587 .NewProfile(100, r) 579 .NewProfile(100, r)
588 .NewSample() 580 .NewSample()
589 .AddFrame(Frame::kUnknownModuleIndex, 0x1234) 581 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
590 .Build(); 582 .Build();
591 583
592 ASSERT_EQ(1U, profiles.size()); 584 ASSERT_EQ(1U, profiles.size());
593 585
594 CallStackProfileMetricsProvider provider; 586 CallStackProfileMetricsProvider provider;
595 provider.OnRecordingEnabled(); 587 provider.OnRecordingEnabled();
596 AppendProfiles( 588 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
597 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 589 CallStackProfileParams::UI_THREAD,
598 CallStackProfileParams::UI_THREAD, 590 CallStackProfileParams::PROCESS_STARTUP,
599 CallStackProfileParams::PROCESS_STARTUP, 591 CallStackProfileParams::MAY_SHUFFLE);
600 CallStackProfileParams::MAY_SHUFFLE), 592 AppendProfiles(&params, std::move(profiles));
601 std::move(profiles));
602 ChromeUserMetricsExtension uma_proto; 593 ChromeUserMetricsExtension uma_proto;
603 provider.ProvideGeneralMetrics(&uma_proto); 594 provider.ProvideGeneralMetrics(&uma_proto);
604 595
605 ASSERT_EQ(1, uma_proto.sampled_profile().size()); 596 ASSERT_EQ(1, uma_proto.sampled_profile().size());
606 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0); 597 const SampledProfile& sampled_profile = uma_proto.sampled_profile().Get(0);
607 ASSERT_TRUE(sampled_profile.has_call_stack_profile()); 598 ASSERT_TRUE(sampled_profile.has_call_stack_profile());
608 const CallStackProfile& call_stack_profile = 599 const CallStackProfile& call_stack_profile =
609 sampled_profile.call_stack_profile(); 600 sampled_profile.call_stack_profile();
610 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms()); 601 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms());
611 EXPECT_EQ(r, call_stack_profile.sampling_period_ms()); 602 EXPECT_EQ(r, call_stack_profile.sampling_period_ms());
612 } 603 }
613 } 604 }
614 605
615 // Checks that pending profiles are provided to ProvideGeneralMetrics 606 // Checks that pending profiles are provided to ProvideGeneralMetrics
616 // when collected before CallStackProfileMetricsProvider is instantiated. 607 // when collected before CallStackProfileMetricsProvider is instantiated.
617 TEST_F(CallStackProfileMetricsProviderTest, 608 TEST_F(CallStackProfileMetricsProviderTest,
618 ProfilesProvidedWhenCollectedBeforeInstantiation) { 609 ProfilesProvidedWhenCollectedBeforeInstantiation) {
619 Profiles profiles = ProfilesFactory() 610 Profiles profiles = ProfilesFactory()
620 .NewProfile(100, 10) 611 .NewProfile(100, 10)
621 .NewSample() 612 .NewSample()
622 .AddFrame(Frame::kUnknownModuleIndex, 0x1234) 613 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
623 .Build(); 614 .Build();
624 615
625 ASSERT_EQ(1U, profiles.size()); 616 ASSERT_EQ(1U, profiles.size());
626 617
627 AppendProfiles( 618 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
628 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 619 CallStackProfileParams::UI_THREAD,
629 CallStackProfileParams::UI_THREAD, 620 CallStackProfileParams::PROCESS_STARTUP,
630 CallStackProfileParams::PROCESS_STARTUP, 621 CallStackProfileParams::MAY_SHUFFLE);
631 CallStackProfileParams::MAY_SHUFFLE), 622 AppendProfiles(&params, std::move(profiles));
632 std::move(profiles));
633 623
634 CallStackProfileMetricsProvider provider; 624 CallStackProfileMetricsProvider provider;
635 provider.OnRecordingEnabled(); 625 provider.OnRecordingEnabled();
636 ChromeUserMetricsExtension uma_proto; 626 ChromeUserMetricsExtension uma_proto;
637 provider.ProvideGeneralMetrics(&uma_proto); 627 provider.ProvideGeneralMetrics(&uma_proto);
638 628
639 EXPECT_EQ(1, uma_proto.sampled_profile_size()); 629 EXPECT_EQ(1, uma_proto.sampled_profile_size());
640 } 630 }
641 631
642 // Checks that pending profiles are not provided to ProvideGeneralMetrics 632 // Checks that pending profiles are not provided to ProvideGeneralMetrics
643 // while recording is disabled. 633 // while recording is disabled.
644 TEST_F(CallStackProfileMetricsProviderTest, ProfilesNotProvidedWhileDisabled) { 634 TEST_F(CallStackProfileMetricsProviderTest, ProfilesNotProvidedWhileDisabled) {
645 Profiles profiles = ProfilesFactory() 635 Profiles profiles = ProfilesFactory()
646 .NewProfile(100, 10) 636 .NewProfile(100, 10)
647 .NewSample() 637 .NewSample()
648 .AddFrame(Frame::kUnknownModuleIndex, 0x1234) 638 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
649 .Build(); 639 .Build();
650 640
651 ASSERT_EQ(1U, profiles.size()); 641 ASSERT_EQ(1U, profiles.size());
652 642
653 CallStackProfileMetricsProvider provider; 643 CallStackProfileMetricsProvider provider;
654 provider.OnRecordingDisabled(); 644 provider.OnRecordingDisabled();
655 AppendProfiles( 645 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
656 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS, 646 CallStackProfileParams::UI_THREAD,
657 CallStackProfileParams::UI_THREAD, 647 CallStackProfileParams::PROCESS_STARTUP,
658 CallStackProfileParams::PROCESS_STARTUP, 648 CallStackProfileParams::MAY_SHUFFLE);
659 CallStackProfileParams::MAY_SHUFFLE), 649 AppendProfiles(&params, std::move(profiles));
660 std::move(profiles));
661 ChromeUserMetricsExtension uma_proto; 650 ChromeUserMetricsExtension uma_proto;
662 provider.ProvideGeneralMetrics(&uma_proto); 651 provider.ProvideGeneralMetrics(&uma_proto);
663 652
664 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 653 EXPECT_EQ(0, uma_proto.sampled_profile_size());
665 } 654 }
666 655
667 // Checks that pending profiles are not provided to ProvideGeneralMetrics 656 // Checks that pending profiles are not provided to ProvideGeneralMetrics
668 // if recording is disabled while profiling. 657 // if recording is disabled while profiling.
669 TEST_F(CallStackProfileMetricsProviderTest, 658 TEST_F(CallStackProfileMetricsProviderTest,
670 ProfilesNotProvidedAfterChangeToDisabled) { 659 ProfilesNotProvidedAfterChangeToDisabled) {
671 CallStackProfileMetricsProvider provider; 660 CallStackProfileMetricsProvider provider;
672 provider.OnRecordingEnabled(); 661 provider.OnRecordingEnabled();
662 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
663 CallStackProfileParams::UI_THREAD,
664 CallStackProfileParams::PROCESS_STARTUP,
665 CallStackProfileParams::MAY_SHUFFLE);
673 base::StackSamplingProfiler::CompletedCallback callback = 666 base::StackSamplingProfiler::CompletedCallback callback =
674 CallStackProfileMetricsProvider::GetProfilerCallback( 667 internal::GetProfilerCallback(&params);
675 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
676 CallStackProfileParams::UI_THREAD,
677 CallStackProfileParams::PROCESS_STARTUP,
678 CallStackProfileParams::MAY_SHUFFLE));
679 provider.OnRecordingDisabled(); 668 provider.OnRecordingDisabled();
680 669
681 Profiles profiles = ProfilesFactory() 670 Profiles profiles = ProfilesFactory()
682 .NewProfile(100, 10) 671 .NewProfile(100, 10)
683 .NewSample() 672 .NewSample()
684 .AddFrame(Frame::kUnknownModuleIndex, 0x1234) 673 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
685 .Build(); 674 .Build();
686 callback.Run(std::move(profiles)); 675 callback.Run(std::move(profiles));
687 ChromeUserMetricsExtension uma_proto; 676 ChromeUserMetricsExtension uma_proto;
688 provider.ProvideGeneralMetrics(&uma_proto); 677 provider.ProvideGeneralMetrics(&uma_proto);
689 678
690 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 679 EXPECT_EQ(0, uma_proto.sampled_profile_size());
691 } 680 }
692 681
693 // Checks that pending profiles are not provided to ProvideGeneralMetrics if 682 // Checks that pending profiles are not provided to ProvideGeneralMetrics if
694 // recording is enabled, but then disabled and reenabled while profiling. 683 // recording is enabled, but then disabled and reenabled while profiling.
695 TEST_F(CallStackProfileMetricsProviderTest, 684 TEST_F(CallStackProfileMetricsProviderTest,
696 ProfilesNotProvidedAfterChangeToDisabledThenEnabled) { 685 ProfilesNotProvidedAfterChangeToDisabledThenEnabled) {
697 CallStackProfileMetricsProvider provider; 686 CallStackProfileMetricsProvider provider;
698 provider.OnRecordingEnabled(); 687 provider.OnRecordingEnabled();
688 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
689 CallStackProfileParams::UI_THREAD,
690 CallStackProfileParams::PROCESS_STARTUP,
691 CallStackProfileParams::MAY_SHUFFLE);
699 base::StackSamplingProfiler::CompletedCallback callback = 692 base::StackSamplingProfiler::CompletedCallback callback =
700 CallStackProfileMetricsProvider::GetProfilerCallback( 693 internal::GetProfilerCallback(&params);
701 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
702 CallStackProfileParams::UI_THREAD,
703 CallStackProfileParams::PROCESS_STARTUP,
704 CallStackProfileParams::MAY_SHUFFLE));
705 provider.OnRecordingDisabled(); 694 provider.OnRecordingDisabled();
706 provider.OnRecordingEnabled(); 695 provider.OnRecordingEnabled();
707 696
708 Profiles profiles = ProfilesFactory() 697 Profiles profiles = ProfilesFactory()
709 .NewProfile(100, 10) 698 .NewProfile(100, 10)
710 .NewSample() 699 .NewSample()
711 .AddFrame(Frame::kUnknownModuleIndex, 0x1234) 700 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
712 .Build(); 701 .Build();
713 callback.Run(std::move(profiles)); 702 callback.Run(std::move(profiles));
714 ChromeUserMetricsExtension uma_proto; 703 ChromeUserMetricsExtension uma_proto;
715 provider.ProvideGeneralMetrics(&uma_proto); 704 provider.ProvideGeneralMetrics(&uma_proto);
716 705
717 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 706 EXPECT_EQ(0, uma_proto.sampled_profile_size());
718 } 707 }
719 708
720 // Checks that pending profiles are not provided to ProvideGeneralMetrics 709 // Checks that pending profiles are not provided to ProvideGeneralMetrics
721 // if recording is disabled, but then enabled while profiling. 710 // if recording is disabled, but then enabled while profiling.
722 TEST_F(CallStackProfileMetricsProviderTest, 711 TEST_F(CallStackProfileMetricsProviderTest,
723 ProfilesNotProvidedAfterChangeFromDisabled) { 712 ProfilesNotProvidedAfterChangeFromDisabled) {
724 CallStackProfileMetricsProvider provider; 713 CallStackProfileMetricsProvider provider;
725 provider.OnRecordingDisabled(); 714 provider.OnRecordingDisabled();
715 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
716 CallStackProfileParams::UI_THREAD,
717 CallStackProfileParams::PROCESS_STARTUP,
718 CallStackProfileParams::MAY_SHUFFLE);
726 base::StackSamplingProfiler::CompletedCallback callback = 719 base::StackSamplingProfiler::CompletedCallback callback =
727 CallStackProfileMetricsProvider::GetProfilerCallback( 720 internal::GetProfilerCallback(&params);
728 CallStackProfileParams(CallStackProfileParams::BROWSER_PROCESS,
729 CallStackProfileParams::UI_THREAD,
730 CallStackProfileParams::PROCESS_STARTUP,
731 CallStackProfileParams::MAY_SHUFFLE));
732 provider.OnRecordingEnabled(); 721 provider.OnRecordingEnabled();
733 722
734 Profiles profiles = ProfilesFactory() 723 Profiles profiles = ProfilesFactory()
735 .NewProfile(100, 10) 724 .NewProfile(100, 10)
736 .NewSample() 725 .NewSample()
737 .AddFrame(Frame::kUnknownModuleIndex, 0x1234) 726 .AddFrame(Frame::kUnknownModuleIndex, 0x1234)
738 .Build(); 727 .Build();
739 callback.Run(std::move(profiles)); 728 callback.Run(std::move(profiles));
740 ChromeUserMetricsExtension uma_proto; 729 ChromeUserMetricsExtension uma_proto;
741 provider.ProvideGeneralMetrics(&uma_proto); 730 provider.ProvideGeneralMetrics(&uma_proto);
742 731
743 EXPECT_EQ(0, uma_proto.sampled_profile_size()); 732 EXPECT_EQ(0, uma_proto.sampled_profile_size());
744 } 733 }
745 734
735 // Only certain platforms support GetUptime() which is used both by the test
736 // and the code being tested.
737 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) || \
738 defined(OS_LINUX)
739 #define MAYBE_PeriodicProfiles PeriodicProfiles
740 #else
741 #define MAYBE_PeriodicProfiles DISABLED_PeriodicProfiles
742 #endif
743 TEST_F(CallStackProfileMetricsProviderTest, MAYBE_PeriodicProfiles) {
744 const uintptr_t module_base_address = 0x1000;
745 const char* module_name = "ABCD";
746
747 #if defined(OS_WIN)
748 uint64_t module_md5 = 0x46C3E4166659AC02ULL;
749 base::FilePath module_path(L"c:\\some\\path\\to\\chrome.exe");
750 #else
751 uint64_t module_md5 = 0x554838A8451AC36CULL;
752 base::FilePath module_path("/some/path/to/chrome");
753 #endif
754
755 Profiles profiles =
756 ProfilesFactory()
757 .NewProfile(100, 10)
758 .DefineModule(module_name, module_path, module_base_address)
759
760 .AddMilestone(0)
761 .NewSample()
762 .AddFrame(0, module_base_address + 0x10)
763 .NewSample()
764 .AddFrame(0, module_base_address + 0x20)
765 .NewSample()
766 .AddFrame(0, module_base_address + 0x10)
767 .NewSample()
768 .AddFrame(0, module_base_address + 0x10)
769
770 .AddMilestone(1)
771 .NewSample()
772 .AddFrame(0, module_base_address + 0x10)
773 .NewSample()
774 .AddFrame(0, module_base_address + 0x20)
775 .NewSample()
776 .AddFrame(0, module_base_address + 0x10)
777 .NewSample()
778 .AddFrame(0, module_base_address + 0x10)
779
780 .Build();
781
782 const ExpectedProtoModule expected_proto_modules[] = {
783 {module_name, module_md5, module_base_address},
784 };
785
786 const ExpectedProtoEntry expected_proto_entries[] = {
787 {0, 0x10}, {0, 0x20},
788 };
789 const ExpectedProtoSample expected_proto_samples[] = {
790 {1, &expected_proto_entries[0], 1, 3},
791 {0, &expected_proto_entries[1], 1, 1},
792 {2, &expected_proto_entries[0], 1, 3},
793 {0, &expected_proto_entries[1], 1, 1},
794 };
795
796 ExpectedProtoProfile expected_proto_profiles[] = {
797 {
798 0, // Will be updated below.
799 1000, // Based on kPeriodicSamplingInterval in the .cc.
800 expected_proto_modules, arraysize(expected_proto_modules),
801 expected_proto_samples, arraysize(expected_proto_samples),
802 },
803 };
804
805 ASSERT_EQ(arraysize(expected_proto_profiles), profiles.size());
806
807 CallStackProfileMetricsProvider provider;
808 provider.OnRecordingEnabled();
809 CallStackProfileParams params(CallStackProfileParams::BROWSER_PROCESS,
810 CallStackProfileParams::UI_THREAD,
811 CallStackProfileParams::PERIODIC_COLLECTION,
812 CallStackProfileParams::MAY_SHUFFLE);
813 const base::TimeDelta min_expected_uptime = internal::GetUptime();
814 AppendProfiles(&params, std::move(profiles));
815 const base::TimeDelta max_expected_uptime = internal::GetUptime();
816
817 ChromeUserMetricsExtension uma_proto;
818 provider.ProvideGeneralMetrics(&uma_proto);
819
820 // We expect duration_ms to be the process uptime. Check that it's within the
821 // min/max boundary values that were retrieved earlier. Then, set the value
822 // in |expected_proto_profiles| to be the actual value so it matches below.
823 const int32_t profile_duration_ms =
824 uma_proto.sampled_profile(0).call_stack_profile().profile_duration_ms();
825 EXPECT_GE(profile_duration_ms, min_expected_uptime.InMilliseconds());
826 EXPECT_LE(profile_duration_ms, max_expected_uptime.InMilliseconds());
827 expected_proto_profiles[0].duration_ms = profile_duration_ms;
828
829 ASSERT_EQ(static_cast<int>(arraysize(expected_proto_profiles)),
830 uma_proto.sampled_profile().size());
831 for (size_t p = 0; p < arraysize(expected_proto_profiles); ++p) {
832 SCOPED_TRACE("profile " + base::SizeTToString(p));
833 VerifyProfileProto(expected_proto_profiles[p],
834 uma_proto.sampled_profile().Get(p));
835 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION,
836 uma_proto.sampled_profile(p).trigger_event());
837 }
838 }
839
746 } // namespace metrics 840 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/call_stack_profile_metrics_provider.cc ('k') | components/metrics/call_stack_profile_params.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698