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

Unified Diff: base/win/sampling_profiler_unittest.cc

Issue 8803022: Windows-native sampling profiler wrapper class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Unittest no longer hanging on trybots. Created 9 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/win/sampling_profiler.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..caa4c63af0dc6b0912702be0184d5ad78de2eb92
--- /dev/null
+++ b/base/win/sampling_profiler_unittest.cc
@@ -0,0 +1,116 @@
+// 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 "testing/gtest/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, Sample) {
+ 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]);
+
+ // We use a roomy timeout to make sure this test is not flaky.
+ // On the buildbots, there may not be a whole lot of CPU time
+ // allotted to our process in this wall-clock time duration,
+ // and samples will only accrue while this thread is busy on
+ // a CPU core.
+ base::TimeDelta spin_time =
+ base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms());
+
+ base::TimeDelta save_sampling_interval;
+ ASSERT_TRUE(SamplingProfiler::GetSamplingInterval(&save_sampling_interval));
+
+ // Sample every 0.5 millisecs.
+ ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(
+ base::TimeDelta::FromMicroseconds(500)));
+
+ ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(
+ base::TimeDelta::FromMicroseconds(500)));
+
+ // Start the profiler.
+ ASSERT_TRUE(profiler.Start());
+
+ // Get a volatile pointer to our bucket to make sure that the compiler
+ // doesn't optimize out the test in the loop that follows.
+ volatile const ULONG* bucket_ptr = &profiler.buckets()[0];
+
+ // Spin for spin_time wall-clock seconds, or until we get some samples.
+ // Note that sleeping isn't going to do us any good, the samples only
+ // accrue while we're executing code.
+ base::Time start = base::Time::Now();
+ base::TimeDelta elapsed;
+ do {
+ elapsed = base::Time::Now() - start;
+ } while((elapsed < spin_time) && *bucket_ptr == 0);
+
+ // Stop the profiler.
+ ASSERT_TRUE(profiler.Stop());
+
+ // Restore the sampling interval we found.
+ ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(save_sampling_interval));
+
+ // Check that we got some samples.
+ ASSERT_NE(0U, profiler.buckets()[0]);
+}
+
+} // namespace win
+} // namespace base
« no previous file with comments | « base/win/sampling_profiler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698