OLD | NEW |
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 "base/metrics/field_trial.h" |
7 #include "base/profiler/stack_sampling_profiler.h" | 8 #include "base/profiler/stack_sampling_profiler.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | 10 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 11 #include "components/variations/entropy_provider.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 using base::StackSamplingProfiler; | 14 using base::StackSamplingProfiler; |
13 using Frame = StackSamplingProfiler::Frame; | 15 using Frame = StackSamplingProfiler::Frame; |
14 using Module = StackSamplingProfiler::Module; | 16 using Module = StackSamplingProfiler::Module; |
15 using Profile = StackSamplingProfiler::Profile; | 17 using Profile = StackSamplingProfiler::Profile; |
16 using Sample = StackSamplingProfiler::Sample; | 18 using Sample = StackSamplingProfiler::Sample; |
17 | 19 |
18 namespace metrics { | 20 namespace metrics { |
19 | 21 |
| 22 // This test fixture enables the field trial that |
| 23 // CallStackProfileMetricsProvider depends on to report profiles. |
| 24 class CallStackProfileMetricsProviderTest : public testing::Test { |
| 25 public: |
| 26 CallStackProfileMetricsProviderTest() |
| 27 : field_trial_list_(new base::FieldTrialList( |
| 28 new metrics::SHA1EntropyProvider("foo"))) { |
| 29 base::FieldTrialList::CreateFieldTrial( |
| 30 FieldTrialState::kFieldTrialName, |
| 31 FieldTrialState::kReportProfilesGroupName); |
| 32 } |
| 33 |
| 34 ~CallStackProfileMetricsProviderTest() override {} |
| 35 |
| 36 private: |
| 37 // Exposes field trial/group names from the CallStackProfileMetricsProvider. |
| 38 class FieldTrialState : public CallStackProfileMetricsProvider { |
| 39 public: |
| 40 using CallStackProfileMetricsProvider::kFieldTrialName; |
| 41 using CallStackProfileMetricsProvider::kReportProfilesGroupName; |
| 42 }; |
| 43 |
| 44 const scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 45 }; |
| 46 |
20 // Checks that all properties from multiple profiles are filled as expected. | 47 // Checks that all properties from multiple profiles are filled as expected. |
21 TEST(CallStackProfileMetricsProviderTest, MultipleProfiles) { | 48 TEST_F(CallStackProfileMetricsProviderTest, MultipleProfiles) { |
22 const uintptr_t module1_base_address = 0x1000; | 49 const uintptr_t module1_base_address = 0x1000; |
23 const uintptr_t module2_base_address = 0x2000; | 50 const uintptr_t module2_base_address = 0x2000; |
24 const uintptr_t module3_base_address = 0x3000; | 51 const uintptr_t module3_base_address = 0x3000; |
25 | 52 |
26 const Module profile_modules[][2] = { | 53 const Module profile_modules[][2] = { |
27 { | 54 { |
28 Module( | 55 Module( |
29 reinterpret_cast<const void*>(module1_base_address), | 56 reinterpret_cast<const void*>(module1_base_address), |
30 "ABCD", | 57 "ABCD", |
31 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 EXPECT_EQ(profile_durations[i].InMilliseconds(), | 249 EXPECT_EQ(profile_durations[i].InMilliseconds(), |
223 call_stack_profile.profile_duration_ms()); | 250 call_stack_profile.profile_duration_ms()); |
224 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms()); | 251 ASSERT_TRUE(call_stack_profile.has_sampling_period_ms()); |
225 EXPECT_EQ(profile_sampling_periods[i].InMilliseconds(), | 252 EXPECT_EQ(profile_sampling_periods[i].InMilliseconds(), |
226 call_stack_profile.sampling_period_ms()); | 253 call_stack_profile.sampling_period_ms()); |
227 } | 254 } |
228 } | 255 } |
229 | 256 |
230 // Checks that all duplicate samples are collapsed with | 257 // Checks that all duplicate samples are collapsed with |
231 // preserve_sample_ordering = false. | 258 // preserve_sample_ordering = false. |
232 TEST(CallStackProfileMetricsProviderTest, RepeatedStacksUnordered) { | 259 TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksUnordered) { |
233 const uintptr_t module_base_address = 0x1000; | 260 const uintptr_t module_base_address = 0x1000; |
234 | 261 |
235 const Module modules[] = { | 262 const Module modules[] = { |
236 Module( | 263 Module( |
237 reinterpret_cast<const void*>(module_base_address), | 264 reinterpret_cast<const void*>(module_base_address), |
238 "ABCD", | 265 "ABCD", |
239 #if defined(OS_WIN) | 266 #if defined(OS_WIN) |
240 base::FilePath(L"c:\\some\\path\\to\\chrome.exe") | 267 base::FilePath(L"c:\\some\\path\\to\\chrome.exe") |
241 #else | 268 #else |
242 base::FilePath("/some/path/to/chrome") | 269 base::FilePath("/some/path/to/chrome") |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 EXPECT_EQ(static_cast<uint64>(instruction_pointer - module_base_address), | 325 EXPECT_EQ(static_cast<uint64>(instruction_pointer - module_base_address), |
299 entry.address()); | 326 entry.address()); |
300 ASSERT_TRUE(entry.has_module_id_index()); | 327 ASSERT_TRUE(entry.has_module_id_index()); |
301 EXPECT_EQ(sample_frames[i][j].module_index, entry.module_id_index()); | 328 EXPECT_EQ(sample_frames[i][j].module_index, entry.module_id_index()); |
302 } | 329 } |
303 } | 330 } |
304 } | 331 } |
305 | 332 |
306 // Checks that only contiguous duplicate samples are collapsed with | 333 // Checks that only contiguous duplicate samples are collapsed with |
307 // preserve_sample_ordering = true. | 334 // preserve_sample_ordering = true. |
308 TEST(CallStackProfileMetricsProviderTest, RepeatedStacksOrdered) { | 335 TEST_F(CallStackProfileMetricsProviderTest, RepeatedStacksOrdered) { |
309 const uintptr_t module_base_address = 0x1000; | 336 const uintptr_t module_base_address = 0x1000; |
310 | 337 |
311 const Module modules[] = { | 338 const Module modules[] = { |
312 Module( | 339 Module( |
313 reinterpret_cast<const void*>(module_base_address), | 340 reinterpret_cast<const void*>(module_base_address), |
314 "ABCD", | 341 "ABCD", |
315 #if defined(OS_WIN) | 342 #if defined(OS_WIN) |
316 base::FilePath(L"c:\\some\\path\\to\\chrome.exe") | 343 base::FilePath(L"c:\\some\\path\\to\\chrome.exe") |
317 #else | 344 #else |
318 base::FilePath("/some/path/to/chrome") | 345 base::FilePath("/some/path/to/chrome") |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 EXPECT_EQ(static_cast<uint64>(instruction_pointer - module_base_address), | 401 EXPECT_EQ(static_cast<uint64>(instruction_pointer - module_base_address), |
375 entry.address()); | 402 entry.address()); |
376 ASSERT_TRUE(entry.has_module_id_index()); | 403 ASSERT_TRUE(entry.has_module_id_index()); |
377 EXPECT_EQ(sample_frames[i][j].module_index, entry.module_id_index()); | 404 EXPECT_EQ(sample_frames[i][j].module_index, entry.module_id_index()); |
378 } | 405 } |
379 } | 406 } |
380 } | 407 } |
381 | 408 |
382 | 409 |
383 // Checks that unknown modules produce an empty Entry. | 410 // Checks that unknown modules produce an empty Entry. |
384 TEST(CallStackProfileMetricsProviderTest, UnknownModule) { | 411 TEST_F(CallStackProfileMetricsProviderTest, UnknownModule) { |
385 // -1 indicates an unknown module. | 412 // -1 indicates an unknown module. |
386 const Frame frame(reinterpret_cast<const void*>(0x1000), -1); | 413 const Frame frame(reinterpret_cast<const void*>(0x1000), -1); |
387 | 414 |
388 Profile profile; | 415 Profile profile; |
389 | 416 |
390 profile.samples.push_back(Sample(1, frame)); | 417 profile.samples.push_back(Sample(1, frame)); |
391 | 418 |
392 profile.profile_duration = base::TimeDelta::FromMilliseconds(100); | 419 profile.profile_duration = base::TimeDelta::FromMilliseconds(100); |
393 profile.sampling_period = base::TimeDelta::FromMilliseconds(10); | 420 profile.sampling_period = base::TimeDelta::FromMilliseconds(10); |
394 profile.preserve_sample_ordering = false; | 421 profile.preserve_sample_ordering = false; |
(...skipping 14 matching lines...) Expand all Loading... |
409 call_stack_profile.sample().Get(0); | 436 call_stack_profile.sample().Get(0); |
410 ASSERT_EQ(1, proto_sample.entry().size()); | 437 ASSERT_EQ(1, proto_sample.entry().size()); |
411 ASSERT_TRUE(proto_sample.has_count()); | 438 ASSERT_TRUE(proto_sample.has_count()); |
412 EXPECT_EQ(1u, proto_sample.count()); | 439 EXPECT_EQ(1u, proto_sample.count()); |
413 const CallStackProfile::Entry& entry = proto_sample.entry().Get(0); | 440 const CallStackProfile::Entry& entry = proto_sample.entry().Get(0); |
414 EXPECT_FALSE(entry.has_address()); | 441 EXPECT_FALSE(entry.has_address()); |
415 EXPECT_FALSE(entry.has_module_id_index()); | 442 EXPECT_FALSE(entry.has_module_id_index()); |
416 } | 443 } |
417 | 444 |
418 } // namespace metrics | 445 } // namespace metrics |
OLD | NEW |