| Index: base/win/sampling_profiler_unittest.cc
|
| diff --git a/base/win/sampling_profiler_unittest.cc b/base/win/sampling_profiler_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..750a8958e29f0a660119b60893e43ad0441d3686
|
| --- /dev/null
|
| +++ b/base/win/sampling_profiler_unittest.cc
|
| @@ -0,0 +1,118 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/win/sampling_profiler.h"
|
| +
|
| +#include "base/test/test_timeouts.h"
|
| +#include "base/win/pe_image.h"
|
| +#include "base/win/scoped_handle.h"
|
| +#include "gtest/gtest.h"
|
| +
|
| +// The address of our image base.
|
| +extern "C" IMAGE_DOS_HEADER __ImageBase;
|
| +
|
| +namespace base {
|
| +namespace win {
|
| +
|
| +namespace {
|
| +
|
| +class SamplingProfilerTest : public testing::Test {
|
| + public:
|
| + SamplingProfilerTest() : code_start(NULL), code_size(0) {
|
| + }
|
| +
|
| + virtual void SetUp() {
|
| + process.Set(::OpenProcess(PROCESS_QUERY_INFORMATION,
|
| + FALSE,
|
| + ::GetCurrentProcessId()));
|
| + ASSERT_TRUE(process.IsValid());
|
| +
|
| + PEImage image(&__ImageBase);
|
| +
|
| + // Get the address of the .text section, which is the first section output
|
| + // by the VS tools.
|
| + ASSERT_TRUE(image.GetNumSections() > 0);
|
| + const IMAGE_SECTION_HEADER* text_section = image.GetSectionHeader(0);
|
| + ASSERT_EQ(0, strncmp(".text",
|
| + reinterpret_cast<const char*>(text_section->Name),
|
| + arraysize(text_section->Name)));
|
| + ASSERT_NE(0U, text_section->Characteristics & IMAGE_SCN_MEM_EXECUTE);
|
| +
|
| + code_start = reinterpret_cast<uint8*>(&__ImageBase) +
|
| + text_section->VirtualAddress;
|
| + code_size = text_section->Misc.VirtualSize;
|
| + }
|
| +
|
| + protected:
|
| + ScopedHandle process;
|
| + void* code_start;
|
| + size_t code_size;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +TEST_F(SamplingProfilerTest, Initialize) {
|
| + SamplingProfiler profiler;
|
| +
|
| + ASSERT_TRUE(profiler.Initialize(process.Get(), code_start, code_size, 8));
|
| +}
|
| +
|
| +TEST_F(SamplingProfilerTest, StartStop) {
|
| + SamplingProfiler profiler;
|
| +
|
| + // Initialize with a huge bucket size, aiming for a single bucket.
|
| + ASSERT_TRUE(
|
| + profiler.Initialize(process.Get(), code_start, code_size, 31));
|
| +
|
| + ASSERT_EQ(1, profiler.buckets().size());
|
| + ASSERT_EQ(0, profiler.buckets()[0]);
|
| +
|
| + FILETIME ignore = {};
|
| + FILETIME user_time = {};
|
| + ASSERT_TRUE(::GetThreadTimes(
|
| + ::GetCurrentThread(), &ignore, &ignore, &ignore, &user_time));
|
| +
|
| + base::Time start = base::Time::FromFileTime(user_time);
|
| + base::TimeDelta spin_time =
|
| + base::TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms());
|
| +
|
| + // Start the profiler.
|
| + ASSERT_TRUE(profiler.Start());
|
| +
|
| + // Spin for spin_time CPU seconds to get some samples.
|
| + base::Time now;
|
| + do {
|
| + ASSERT_TRUE(::GetThreadTimes(
|
| + ::GetCurrentThread(), &ignore, &ignore, &ignore, &user_time));
|
| +
|
| + now = base::Time::FromFileTime(user_time);
|
| + } while(now - start < spin_time);
|
| +
|
| + // Stop the profiler.
|
| + ASSERT_TRUE(profiler.Stop());
|
| +
|
| + // Check that we got some samples.
|
| + ASSERT_NE(0U, profiler.buckets()[0]);
|
| +}
|
| +
|
| +TEST_F(SamplingProfilerTest, GetSetInterval) {
|
| + // Get the original sampling interval.
|
| + base::TimeDelta original_interval;
|
| + ASSERT_TRUE(SamplingProfiler::GetSamplingInterval(&original_interval));
|
| +
|
| + // Set a new interval that's double the old one.
|
| + base::TimeDelta new_interval = original_interval * 2;
|
| + ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(new_interval));
|
| +
|
| + // And verify that we can fetch the new one.
|
| + base::TimeDelta set_interval;
|
| + ASSERT_TRUE(SamplingProfiler::GetSamplingInterval(&set_interval));
|
| + EXPECT_EQ(new_interval.InMicroseconds(), set_interval.InMicroseconds());
|
| +
|
| + // Restore the original interval.
|
| + ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(original_interval));
|
| +}
|
| +
|
| +} // namespace win
|
| +} // namespace base
|
|
|