1// Copyright 2014 the V8 project 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 <vector> 6 7#include "src/counters.h" 8#include "src/handles-inl.h" 9#include "src/objects-inl.h" 10#include "testing/gtest/include/gtest/gtest.h" 11 12namespace v8 { 13namespace internal { 14 15namespace { 16 17class MockHistogram : public Histogram { 18 public: 19 void AddSample(int value) { samples_.push_back(value); } 20 std::vector<int>* samples() { return &samples_; } 21 22 private: 23 std::vector<int> samples_; 24}; 25 26 27class AggregatedMemoryHistogramTest : public ::testing::Test { 28 public: 29 AggregatedMemoryHistogramTest() { 30 aggregated_ = AggregatedMemoryHistogram<MockHistogram>(&mock_); 31 } 32 virtual ~AggregatedMemoryHistogramTest() {} 33 34 void AddSample(double current_ms, double current_value) { 35 aggregated_.AddSample(current_ms, current_value); 36 } 37 38 std::vector<int>* samples() { return mock_.samples(); } 39 40 private: 41 AggregatedMemoryHistogram<MockHistogram> aggregated_; 42 MockHistogram mock_; 43}; 44 45} // namespace 46 47 48TEST_F(AggregatedMemoryHistogramTest, OneSample1) { 49 FLAG_histogram_interval = 10; 50 AddSample(10, 1000); 51 AddSample(20, 1000); 52 EXPECT_EQ(1U, samples()->size()); 53 EXPECT_EQ(1000, (*samples())[0]); 54} 55 56 57TEST_F(AggregatedMemoryHistogramTest, OneSample2) { 58 FLAG_histogram_interval = 10; 59 AddSample(10, 500); 60 AddSample(20, 1000); 61 EXPECT_EQ(1U, samples()->size()); 62 EXPECT_EQ(750, (*samples())[0]); 63} 64 65 66TEST_F(AggregatedMemoryHistogramTest, OneSample3) { 67 FLAG_histogram_interval = 10; 68 AddSample(10, 500); 69 AddSample(15, 500); 70 AddSample(15, 1000); 71 AddSample(20, 1000); 72 EXPECT_EQ(1U, samples()->size()); 73 EXPECT_EQ(750, (*samples())[0]); 74} 75 76 77TEST_F(AggregatedMemoryHistogramTest, OneSample4) { 78 FLAG_histogram_interval = 10; 79 AddSample(10, 500); 80 AddSample(15, 750); 81 AddSample(20, 1000); 82 EXPECT_EQ(1U, samples()->size()); 83 EXPECT_EQ(750, (*samples())[0]); 84} 85 86 87TEST_F(AggregatedMemoryHistogramTest, TwoSamples1) { 88 FLAG_histogram_interval = 10; 89 AddSample(10, 1000); 90 AddSample(30, 1000); 91 EXPECT_EQ(2U, samples()->size()); 92 EXPECT_EQ(1000, (*samples())[0]); 93 EXPECT_EQ(1000, (*samples())[1]); 94} 95 96 97TEST_F(AggregatedMemoryHistogramTest, TwoSamples2) { 98 FLAG_histogram_interval = 10; 99 AddSample(10, 1000); 100 AddSample(20, 1000); 101 AddSample(30, 1000); 102 EXPECT_EQ(2U, samples()->size()); 103 EXPECT_EQ(1000, (*samples())[0]); 104 EXPECT_EQ(1000, (*samples())[1]); 105} 106 107 108TEST_F(AggregatedMemoryHistogramTest, TwoSamples3) { 109 FLAG_histogram_interval = 10; 110 AddSample(10, 1000); 111 AddSample(20, 1000); 112 AddSample(20, 500); 113 AddSample(30, 500); 114 EXPECT_EQ(2U, samples()->size()); 115 EXPECT_EQ(1000, (*samples())[0]); 116 EXPECT_EQ(500, (*samples())[1]); 117} 118 119 120TEST_F(AggregatedMemoryHistogramTest, TwoSamples4) { 121 FLAG_histogram_interval = 10; 122 AddSample(10, 1000); 123 AddSample(30, 0); 124 EXPECT_EQ(2U, samples()->size()); 125 EXPECT_EQ(750, (*samples())[0]); 126 EXPECT_EQ(250, (*samples())[1]); 127} 128 129 130TEST_F(AggregatedMemoryHistogramTest, TwoSamples5) { 131 FLAG_histogram_interval = 10; 132 AddSample(10, 0); 133 AddSample(30, 1000); 134 EXPECT_EQ(2U, samples()->size()); 135 EXPECT_EQ(250, (*samples())[0]); 136 EXPECT_EQ(750, (*samples())[1]); 137} 138 139 140TEST_F(AggregatedMemoryHistogramTest, TwoSamples6) { 141 FLAG_histogram_interval = 10; 142 AddSample(10, 0); 143 AddSample(15, 1000); 144 AddSample(30, 1000); 145 EXPECT_EQ(2U, samples()->size()); 146 EXPECT_EQ((500 + 1000) / 2, (*samples())[0]); 147 EXPECT_EQ(1000, (*samples())[1]); 148} 149 150 151TEST_F(AggregatedMemoryHistogramTest, TwoSamples7) { 152 FLAG_histogram_interval = 10; 153 AddSample(10, 0); 154 AddSample(15, 1000); 155 AddSample(25, 0); 156 AddSample(30, 1000); 157 EXPECT_EQ(2U, samples()->size()); 158 EXPECT_EQ((500 + 750) / 2, (*samples())[0]); 159 EXPECT_EQ((250 + 500) / 2, (*samples())[1]); 160} 161 162 163TEST_F(AggregatedMemoryHistogramTest, TwoSamples8) { 164 FLAG_histogram_interval = 10; 165 AddSample(10, 1000); 166 AddSample(15, 0); 167 AddSample(25, 1000); 168 AddSample(30, 0); 169 EXPECT_EQ(2U, samples()->size()); 170 EXPECT_EQ((500 + 250) / 2, (*samples())[0]); 171 EXPECT_EQ((750 + 500) / 2, (*samples())[1]); 172} 173 174 175TEST_F(AggregatedMemoryHistogramTest, ManySamples1) { 176 FLAG_histogram_interval = 10; 177 const int kMaxSamples = 1000; 178 AddSample(0, 0); 179 AddSample(10 * kMaxSamples, 10 * kMaxSamples); 180 EXPECT_EQ(static_cast<unsigned>(kMaxSamples), samples()->size()); 181 for (int i = 0; i < kMaxSamples; i++) { 182 EXPECT_EQ(i * 10 + 5, (*samples())[i]); 183 } 184} 185 186 187TEST_F(AggregatedMemoryHistogramTest, ManySamples2) { 188 FLAG_histogram_interval = 10; 189 const int kMaxSamples = 1000; 190 AddSample(0, 0); 191 AddSample(10 * (2 * kMaxSamples), 10 * (2 * kMaxSamples)); 192 EXPECT_EQ(static_cast<unsigned>(kMaxSamples), samples()->size()); 193 for (int i = 0; i < kMaxSamples; i++) { 194 EXPECT_EQ(i * 10 + 5, (*samples())[i]); 195 } 196} 197 198 199} // namespace internal 200} // namespace v8 201