#include "components/metrics/call_stacks/call_stack_profile_metrics_provider.h"
#include <string>
#include <utility>
#include "base/test/scoped_feature_list.h"
#include "execution_context.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
namespace metrics {
Eq;
Pair;
UnorderedElementsAre;
class CallStackProfileMetricsProviderTest : public testing::Test { … };
TEST_F(CallStackProfileMetricsProviderTest,
ProvideCurrentSessionDataUnserialized) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProvideCurrentSessionDataSerialized) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProvideCurrentSessionDataUnserializedAndSerialized) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProvideCurrentSessionDataExceedTotalCap) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProfileProvidedWhenCollectedBeforeInstantiation) { … }
TEST_F(CallStackProfileMetricsProviderTest, ProfileNotProvidedWhileDisabled) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProfileNotProvidedAfterChangeToDisabled) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProfileNotProvidedAfterChangeToDisabledThenEnabled) { … }
TEST_F(CallStackProfileMetricsProviderTest,
ProfileNotProvidedAfterChangeFromDisabled) { … }
TEST_F(CallStackProfileMetricsProviderTest,
HeapProfileNotProvidedWhenDisabled) { … }
TEST_F(CallStackProfileMetricsProviderTest, HeapProfileProvidedWhenEnabled) { … }
TEST_F(CallStackProfileMetricsProviderTest, CpuProfileNotProvidedWithoutFinch) { … }
#if BUILDFLAG(IS_CHROMEOS)
namespace {
void MakeMinimallySuccessfulCallStackProfile(
CallStackProfile* call_stack_profile) {
CallStackProfile::Stack* stack = call_stack_profile->add_stack();
CallStackProfile::Location* frame = stack->add_frame();
frame->set_address(123);
frame->set_module_id_index(1);
frame = stack->add_frame();
frame->set_address(456);
frame->set_module_id_index(0);
}
void RecieveProfile(metrics::Process process, metrics::Thread thread) {
SampledProfile profile;
profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
profile.set_process(process);
profile.set_thread(thread);
MakeMinimallySuccessfulCallStackProfile(profile.mutable_call_stack_profile());
CallStackProfileMetricsProvider::ReceiveProfile(base::TimeTicks::Now(),
profile);
}
void ReceiveSerializedProfile(metrics::Process process,
metrics::Thread thread) {
SampledProfile profile;
profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
profile.set_process(process);
profile.set_thread(thread);
MakeMinimallySuccessfulCallStackProfile(profile.mutable_call_stack_profile());
std::string serialized_profile;
profile.SerializeToString(&serialized_profile);
CallStackProfileMetricsProvider::ReceiveSerializedProfile(
base::TimeTicks::Now(), false,
std::move(serialized_profile));
}
}
TEST_F(CallStackProfileMetricsProviderTest,
SuccessfullyCollectedOnReceivedNotSent) {
CallStackProfileMetricsProvider provider;
provider.OnRecordingEnabled();
RecieveProfile(metrics::GPU_PROCESS, metrics::IO_THREAD);
ReceiveSerializedProfile(metrics::GPU_PROCESS, metrics::MAIN_THREAD);
EXPECT_THAT(
CallStackProfileMetricsProvider::GetSuccessfullyCollectedCounts(),
UnorderedElementsAre(
Pair(Eq(metrics::GPU_PROCESS),
UnorderedElementsAre(Pair(Eq(metrics::IO_THREAD), Eq(1)),
Pair(Eq(metrics::MAIN_THREAD), Eq(1))))));
}
TEST_F(CallStackProfileMetricsProviderTest, SuccessfullyCollectedOnSent) {
CallStackProfileMetricsProvider provider;
provider.OnRecordingEnabled();
RecieveProfile(metrics::GPU_PROCESS, metrics::IO_THREAD);
ReceiveSerializedProfile(metrics::BROWSER_PROCESS, metrics::IO_THREAD);
ChromeUserMetricsExtension uma_proto;
provider.ProvideCurrentSessionData(&uma_proto);
EXPECT_EQ(2, uma_proto.sampled_profile().size());
EXPECT_THAT(
CallStackProfileMetricsProvider::GetSuccessfullyCollectedCounts(),
UnorderedElementsAre(
Pair(Eq(metrics::GPU_PROCESS),
UnorderedElementsAre(Pair(Eq(metrics::IO_THREAD), Eq(1)))),
Pair(Eq(metrics::BROWSER_PROCESS),
UnorderedElementsAre(Pair(Eq(metrics::IO_THREAD), Eq(1))))));
}
TEST_F(CallStackProfileMetricsProviderTest,
SuccessfullyCollectedMixedSentUnsent) {
CallStackProfileMetricsProvider provider;
provider.OnRecordingEnabled();
RecieveProfile(metrics::GPU_PROCESS, metrics::IO_THREAD);
ReceiveSerializedProfile(metrics::BROWSER_PROCESS, metrics::IO_THREAD);
ChromeUserMetricsExtension uma_proto;
provider.ProvideCurrentSessionData(&uma_proto);
EXPECT_EQ(2, uma_proto.sampled_profile().size());
RecieveProfile(metrics::GPU_PROCESS, metrics::IO_THREAD);
ReceiveSerializedProfile(metrics::BROWSER_PROCESS, metrics::MAIN_THREAD);
EXPECT_THAT(
CallStackProfileMetricsProvider::GetSuccessfullyCollectedCounts(),
UnorderedElementsAre(
Pair(Eq(metrics::GPU_PROCESS),
UnorderedElementsAre(Pair(Eq(metrics::IO_THREAD), Eq(2)))),
Pair(Eq(metrics::BROWSER_PROCESS),
UnorderedElementsAre(Pair(Eq(metrics::IO_THREAD), Eq(1)),
Pair(Eq(metrics::MAIN_THREAD), Eq(1))))));
}
TEST_F(CallStackProfileMetricsProviderTest,
SuccessfullyCollectedIgnoresUnsuccessful) {
CallStackProfileMetricsProvider provider;
provider.OnRecordingEnabled();
RecieveProfile(metrics::GPU_PROCESS, metrics::IO_THREAD);
ReceiveSerializedProfile(metrics::GPU_PROCESS, metrics::IO_THREAD);
{
SampledProfile no_stack_profile;
no_stack_profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
no_stack_profile.set_process(metrics::BROWSER_PROCESS);
no_stack_profile.set_thread(metrics::MAIN_THREAD);
CallStackProfileMetricsProvider::ReceiveProfile(base::TimeTicks::Now(),
no_stack_profile);
std::string serialized_no_stack_profile;
no_stack_profile.SerializeToString(&serialized_no_stack_profile);
CallStackProfileMetricsProvider::ReceiveSerializedProfile(
base::TimeTicks::Now(), false,
std::move(serialized_no_stack_profile));
}
{
SampledProfile one_frame_profile;
one_frame_profile.set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
one_frame_profile.set_process(metrics::BROWSER_PROCESS);
one_frame_profile.set_thread(metrics::MAIN_THREAD);
CallStackProfile::Stack* stack =
one_frame_profile.mutable_call_stack_profile()->add_stack();
CallStackProfile::Location* frame = stack->add_frame();
frame->set_address(123);
frame->set_module_id_index(1);
CallStackProfileMetricsProvider::ReceiveProfile(base::TimeTicks::Now(),
one_frame_profile);
std::string serialized_one_frame_profile;
one_frame_profile.SerializeToString(&serialized_one_frame_profile);
CallStackProfileMetricsProvider::ReceiveSerializedProfile(
base::TimeTicks::Now(), false,
std::move(serialized_one_frame_profile));
}
EXPECT_THAT(CallStackProfileMetricsProvider::GetSuccessfullyCollectedCounts(),
UnorderedElementsAre(Pair(
Eq(metrics::GPU_PROCESS),
UnorderedElementsAre(Pair(Eq(metrics::IO_THREAD), Eq(2))))));
}
#endif
}