OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/browser/memory/memory_wedge.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "ios/chrome/browser/memory/memory_metrics.h" |
| 9 #include "testing/gtest/include/gtest/gtest-param-test.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace memory_wedge { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // The number of bytes in a megabyte. |
| 17 const uint64 kNumBytesInMB = 1024 * 1024; |
| 18 |
| 19 // Note: in the following tests, only memory_util::GetInternalVMBytes, |
| 20 // and memory_util::GetRealMemoryUsedInBytes are used to check the state of the |
| 21 // memory before and after an action. |
| 22 // memory_util::GetFreePhysicalBytes and memory_util::GetDirtyVMBytes are not |
| 23 // used because external events can change these values, making them not |
| 24 // reliable. |
| 25 |
| 26 // Performs a snapshot of the memory when constructed. Deviation from the |
| 27 // initial values can be verified with VerifyDeviation. The comparison is |
| 28 // on the integer part of the values in MB. |
| 29 class MemoryChecker { |
| 30 public: |
| 31 MemoryChecker() { |
| 32 internal_vm_ = memory_util::GetInternalVMBytes() / kNumBytesInMB; |
| 33 real_memory_used_ = memory_util::GetRealMemoryUsedInBytes() / kNumBytesInMB; |
| 34 } |
| 35 |
| 36 // Verifies that the memory metrics deviated only by |deviation| in MB. |
| 37 void VerifyDeviation(uint64 deviation) const { |
| 38 EXPECT_NEAR(memory_util::GetInternalVMBytes() / kNumBytesInMB, |
| 39 deviation + internal_vm_, 1); |
| 40 EXPECT_NEAR(memory_util::GetRealMemoryUsedInBytes() / kNumBytesInMB, |
| 41 deviation + real_memory_used_, 1); |
| 42 } |
| 43 |
| 44 private: |
| 45 uint64 internal_vm_; |
| 46 uint64 real_memory_used_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(MemoryChecker); |
| 49 }; |
| 50 |
| 51 class MemoryWedgeTest : public testing::TestWithParam<size_t> {}; |
| 52 |
| 53 } // namespace |
| 54 |
| 55 // Checks that the wedge size passed to AddWedge is indeed added to the global |
| 56 // footprint of the app. |
| 57 TEST_P(MemoryWedgeTest, WedgeSize) { |
| 58 const MemoryChecker memory_checker; |
| 59 size_t wedge_size = GetParam(); |
| 60 |
| 61 AddWedge(wedge_size); |
| 62 |
| 63 memory_checker.VerifyDeviation(wedge_size); |
| 64 if (wedge_size > 0) |
| 65 RemoveWedgeForTesting(); |
| 66 } |
| 67 |
| 68 INSTANTIATE_TEST_CASE_P( |
| 69 /* No InstantiationName */, |
| 70 MemoryWedgeTest, |
| 71 testing::Values(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)); |
| 72 |
| 73 } // namespace memory_wedge |
OLD | NEW |