 Chromium Code Reviews
 Chromium Code Reviews Issue 2163783003:
  Implement a ScopedThreadHeapUsage class to allow profiling per-thread heap usage.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@shim-default
    
  
    Issue 2163783003:
  Implement a ScopedThreadHeapUsage class to allow profiling per-thread heap usage.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@shim-default| Index: base/debug/scoped_heap_usage_unittest.cc | 
| diff --git a/base/debug/scoped_heap_usage_unittest.cc b/base/debug/scoped_heap_usage_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..8bb897012ad4a2f0592ae9b3c7533e3b0e35705f | 
| --- /dev/null | 
| +++ b/base/debug/scoped_heap_usage_unittest.cc | 
| @@ -0,0 +1,96 @@ | 
| +// Copyright 2016 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/debug/scoped_heap_usage.h" | 
| + | 
| +#include "base/allocator/features.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| + | 
| +namespace base { | 
| +namespace debug { | 
| + | 
| +#if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 
| +namespace { | 
| + | 
| +class TestingScopedHeapUsage : public ScopedHeapUsage { | 
| + public: | 
| + using ScopedHeapUsage::TearDownForTesting; | 
| +}; | 
| + | 
| +class ScopedHeapUsageTest : public testing::Test { | 
| + public: | 
| + void SetUp() override { ScopedHeapUsage::Initialize(); } | 
| + | 
| + void TearDown() override { TestingScopedHeapUsage::TearDownForTesting(); } | 
| +}; | 
| + | 
| +} // namespace | 
| + | 
| +TEST_F(ScopedHeapUsageTest, SimpleUsage) { | 
| + ScopedHeapUsage scoped_usage; | 
| + | 
| + ScopedHeapUsage::AllocatorUsage u1 = scoped_usage.usage_at_creation(); | 
| + ScopedHeapUsage::AllocatorUsage u2 = ScopedHeapUsage::Now(); | 
| + | 
| + EXPECT_EQ(u1.alloc_ops, u2.alloc_ops); | 
| 
Primiano Tucci (use gerrit)
2016/08/24 14:11:46
I'd move these expect statements below., after u3
 
Sigurður Ásgeirsson
2016/09/01 15:18:18
Yeah, I'm going to rewrite this test to test the i
 | 
| + EXPECT_EQ(u1.alloc_bytes, u2.alloc_bytes); | 
| + EXPECT_EQ(u1.alloc_overhead_bytes, u2.alloc_overhead_bytes); | 
| + EXPECT_EQ(u1.free_ops, u2.free_ops); | 
| + EXPECT_EQ(u1.free_bytes, u2.free_bytes); | 
| + EXPECT_EQ(0U, u2.max_allocated_bytes); | 
| + | 
| + const size_t kAllocSize = 1029U; | 
| + void* ptr = malloc(kAllocSize); | 
| + free(ptr); | 
| + | 
| + ScopedHeapUsage::AllocatorUsage u3 = ScopedHeapUsage::Now(); | 
| + | 
| + EXPECT_LT(u1.alloc_ops, u3.alloc_ops); | 
| + EXPECT_LE(u1.alloc_bytes + kAllocSize, u3.alloc_bytes); | 
| + EXPECT_LE(u1.alloc_overhead_bytes, u3.alloc_overhead_bytes); | 
| + EXPECT_LT(u1.free_ops, u3.free_ops); | 
| + EXPECT_LE(u1.free_bytes + kAllocSize, u3.free_bytes); | 
| + EXPECT_LE(kAllocSize, u3.max_allocated_bytes); | 
| +} | 
| + | 
| +TEST_F(ScopedHeapUsageTest, NestedMaxWorks) { | 
| + ScopedHeapUsage outer_scoped_usage; | 
| + | 
| + const size_t kOuterAllocSize = 1029U; | 
| + void* ptr = malloc(kOuterAllocSize); | 
| + free(ptr); | 
| + | 
| + EXPECT_LE(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | 
| + | 
| + { | 
| + ScopedHeapUsage inner_scoped_usage; | 
| + | 
| + const size_t kInnerAllocSize = 673U; | 
| + ptr = malloc(kInnerAllocSize); | 
| + free(ptr); | 
| + | 
| + EXPECT_LE(kInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | 
| + } | 
| + | 
| + // The greater, outer allocation size should have been restored. | 
| + EXPECT_LE(kOuterAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | 
| + | 
| + const size_t kLargerInnerAllocSize = kOuterAllocSize + 673U; | 
| + { | 
| + ScopedHeapUsage inner_scoped_usage; | 
| + | 
| + ptr = malloc(kLargerInnerAllocSize); | 
| + free(ptr); | 
| + | 
| + EXPECT_LE(kLargerInnerAllocSize, | 
| + ScopedHeapUsage::Now().max_allocated_bytes); | 
| + } | 
| + | 
| + // The greater, inner allocation size should have been preserved. | 
| + EXPECT_LE(kLargerInnerAllocSize, ScopedHeapUsage::Now().max_allocated_bytes); | 
| +} | 
| +#endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 
| + | 
| +} // namespace debug | 
| +} // namespace base |