histogram_unittest.cc revision 3a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6
181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Copyright (c) 2012 The Chromium Authors. All rights reserved.
281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Use of this source code is governed by a BSD-style license that can be
381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// found in the LICENSE file.
481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/histogram.h"
681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include <limits.h>
881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include <stddef.h>
981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include <stdint.h>
1081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
1181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include <climits>
1281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include <memory>
134e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o#include <string>
1481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include <vector>
154e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o
1681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/logging.h"
1781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/bucket_ranges.h"
1881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/histogram_macros.h"
1981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/persistent_histogram_allocator.h"
2081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/persistent_memory_allocator.h"
2181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/sample_vector.h"
2281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/metrics/statistics_recorder.h"
2381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/pickle.h"
2481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/strings/stringprintf.h"
2581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/test/gtest_util.h"
2681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "base/time/time.h"
2781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong#include "testing/gtest/include/gtest/gtest.h"
2881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
2981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wongnamespace base {
3081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
3181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Test parameter indicates if a persistent memory allocator should be used
3281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// for histogram allocation. False will allocate histograms from the process
3381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// heap.
3481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wongclass HistogramTest : public testing::TestWithParam<bool> {
3581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong protected:
3681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const int32_t kAllocatorMemorySize = 8 << 20;  // 8 MiB
3781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
3881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramTest() : use_persistent_histogram_allocator_(GetParam()) {}
3969df84967f17d0c3d78ee982101ade85077f8662Theodore Ts'o
4069df84967f17d0c3d78ee982101ade85077f8662Theodore Ts'o  void SetUp() override {
4181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    if (use_persistent_histogram_allocator_)
4281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CreatePersistentHistogramAllocator();
4381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
4481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // Each test will have a clean state (no Histogram / BucketRanges
4581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // registered).
4681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    InitializeStatisticsRecorder();
4781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
4881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
4981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  void TearDown() override {
5081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    if (allocator_) {
5181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      ASSERT_FALSE(allocator_->IsFull());
5281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      ASSERT_FALSE(allocator_->IsCorrupt());
5381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    }
5481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    UninitializeStatisticsRecorder();
5581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    DestroyPersistentHistogramAllocator();
5681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
5781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
5881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  void InitializeStatisticsRecorder() {
5981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    DCHECK(!statistics_recorder_);
6081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    statistics_recorder_ = StatisticsRecorder::CreateTemporaryForTesting();
6181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
6281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
6381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  void UninitializeStatisticsRecorder() {
6481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    statistics_recorder_.reset();
6581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
6681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
6781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  void CreatePersistentHistogramAllocator() {
6881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // By getting the results-histogram before any persistent allocator
6981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // is attached, that histogram is guaranteed not to be stored in
7081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // any persistent memory segment (which simplifies some tests).
7181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    GlobalHistogramAllocator::GetCreateHistogramResultHistogram();
7281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
7381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    GlobalHistogramAllocator::CreateWithLocalMemory(
7481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong        kAllocatorMemorySize, 0, "HistogramAllocatorTest");
7581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    allocator_ = GlobalHistogramAllocator::Get()->memory_allocator();
7681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
7781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
7881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  void DestroyPersistentHistogramAllocator() {
7981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    allocator_ = nullptr;
8081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    GlobalHistogramAllocator::ReleaseForTesting();
8181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
8281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
8381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const bool use_persistent_histogram_allocator_;
8481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
8581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<StatisticsRecorder> statistics_recorder_;
8681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<char[]> allocator_memory_;
8781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  PersistentMemoryAllocator* allocator_ = nullptr;
8881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
8981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong private:
9081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  DISALLOW_COPY_AND_ASSIGN(HistogramTest);
9181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong};
9281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
9381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Run all HistogramTest cases with both heap and persistent memory.
9481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongINSTANTIATE_TEST_CASE_P(HeapAndPersistent, HistogramTest, testing::Bool());
9581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
9681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
9781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Check for basic syntax and use.
981fc23b5e773a7a30130842a8091af546d5dd9875Theodore Ts'oTEST_P(HistogramTest, BasicTest) {
991fc23b5e773a7a30130842a8091af546d5dd9875Theodore Ts'o  // Try basic construction
1001fc23b5e773a7a30130842a8091af546d5dd9875Theodore Ts'o  HistogramBase* histogram = Histogram::FactoryGet(
1011fc23b5e773a7a30130842a8091af546d5dd9875Theodore Ts'o      "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
10281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(histogram);
10381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
10481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
10581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
10681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(linear_histogram);
10781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
10881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<int> custom_ranges;
10981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(1);
11081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(5);
11181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
11281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
11381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(custom_histogram);
11481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
11581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Macros that create hitograms have an internal static variable which will
11681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // continue to point to those from the very first run of this method even
11781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // during subsequent runs.
11881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  static bool already_run = false;
11981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  if (already_run)
12081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    return;
12181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  already_run = true;
12281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
12381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Use standard macros (but with fixed samples)
12481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
12581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30);
12681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
12781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130);
12881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
12981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
13081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Check that the macro correctly matches histograms by name and records their
13181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// data together.
13281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, NameMatchTest) {
13381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Macros that create hitograms have an internal static variable which will
13481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // continue to point to those from the very first run of this method even
13581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // during subsequent runs.
13681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  static bool already_run = false;
13781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  if (already_run)
13881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    return;
13981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  already_run = true;
14081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
14181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10);
14281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10);
14381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* histogram = LinearHistogram::FactoryGet(
14481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags);
14581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
14681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
14781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->TotalCount());
14881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->GetCount(10));
14981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
15081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
15181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Check that delta calculations work correctly.
15281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, DeltaTest) {
15381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* histogram =
15481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("DeltaHistogram", 1, 64, 8,
15581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                            HistogramBase::kNoFlags);
15681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(1);
15781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(10);
15881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(50);
15981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
16081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples = histogram->SnapshotDelta();
16181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(3, samples->TotalCount());
16281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(1));
16381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(10));
16481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(50));
16581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(samples->TotalCount(), samples->redundant_count());
16681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
16781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  samples = histogram->SnapshotDelta();
16881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, samples->TotalCount());
16981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
17081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(10);
17181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(10);
17281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  samples = histogram->SnapshotDelta();
17381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->TotalCount());
17481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->GetCount(10));
17581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
17681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  samples = histogram->SnapshotDelta();
17781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, samples->TotalCount());
17881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
17981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
18081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Check that final-delta calculations work correctly.
18181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, FinalDeltaTest) {
18281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* histogram =
18381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("FinalDeltaHistogram", 1, 64, 8,
18481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                            HistogramBase::kNoFlags);
18581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(1);
18681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(10);
18781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(50);
18881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
18981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples = histogram->SnapshotDelta();
19081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(3, samples->TotalCount());
19181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(1));
19281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(10));
19381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(50));
19481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(samples->TotalCount(), samples->redundant_count());
1954e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o
19681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(2);
19781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(50);
19881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
19981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  samples = histogram->SnapshotFinalDelta();
20081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->TotalCount());
20181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(2));
20281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, samples->GetCount(50));
20381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(samples->TotalCount(), samples->redundant_count());
20481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
20581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
20681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, ExponentialRangesTest) {
20781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Check that we got a nice exponential when there was enough room.
2084e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  BucketRanges ranges(9);
20981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram::InitializeBucketRanges(1, 64, &ranges);
2104e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  EXPECT_EQ(0, ranges.range(0));
21181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int power_of_2 = 1;
21281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 1; i < 8; i++) {
21381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    EXPECT_EQ(power_of_2, ranges.range(i));
21481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    power_of_2 *= 2;
21581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
21681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges.range(8));
21781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
21881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Check the corresponding Histogram will use the correct ranges.
21981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
22081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
22181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(ranges.Equals(histogram->bucket_ranges()));
22281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
22381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // When bucket count is limited, exponential ranges will partially look like
22481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // linear.
22581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  BucketRanges ranges2(16);
22681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram::InitializeBucketRanges(1, 32, &ranges2);
22781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
22881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges2.range(0));
22981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, ranges2.range(1));
23081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, ranges2.range(2));
23181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(3, ranges2.range(3));
23281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(4, ranges2.range(4));
23381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(5, ranges2.range(5));
23481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(6, ranges2.range(6));
23581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(7, ranges2.range(7));
23681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(9, ranges2.range(8));
23781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(11, ranges2.range(9));
23881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(14, ranges2.range(10));
23981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(17, ranges2.range(11));
24081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(21, ranges2.range(12));
24181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(26, ranges2.range(13));
24281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(32, ranges2.range(14));
24381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges2.range(15));
24481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
24581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Check the corresponding Histogram will use the correct ranges.
24681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram2 = static_cast<Histogram*>(
24781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Histogram2", 1, 32, 15, HistogramBase::kNoFlags));
2484e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  EXPECT_TRUE(ranges2.Equals(histogram2->bucket_ranges()));
24981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
25081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
25181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, LinearRangesTest) {
25281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  BucketRanges ranges(9);
25381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LinearHistogram::InitializeBucketRanges(1, 7, &ranges);
25481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Gets a nice linear set of bucket ranges.
25581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 0; i < 8; i++)
25681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    EXPECT_EQ(i, ranges.range(i));
25781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges.range(8));
25881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
25981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // The correspoding LinearHistogram should use the correct ranges.
26081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
26181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      LinearHistogram::FactoryGet("Linear", 1, 7, 8, HistogramBase::kNoFlags));
2624e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  EXPECT_TRUE(ranges.Equals(histogram->bucket_ranges()));
26381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
2644e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  // Linear ranges are not divisible.
26581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  BucketRanges ranges2(6);
26681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  LinearHistogram::InitializeBucketRanges(1, 6, &ranges2);
26781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges2.range(0));
26881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, ranges2.range(1));
26981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(3, ranges2.range(2));
27081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(4, ranges2.range(3));
27181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(6, ranges2.range(4));
27281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges2.range(5));
27381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // The correspoding LinearHistogram should use the correct ranges.
27481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram2 = static_cast<Histogram*>(
27581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      LinearHistogram::FactoryGet("Linear2", 1, 6, 5, HistogramBase::kNoFlags));
27681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(ranges2.Equals(histogram2->bucket_ranges()));
27781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
27881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
27981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, ArrayToCustomRangesTest) {
28081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const HistogramBase::Sample ranges[3] = {5, 10, 20};
28181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<HistogramBase::Sample> ranges_vec =
28281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::ArrayToCustomRanges(ranges, 3);
28381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ASSERT_EQ(6u, ranges_vec.size());
28481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(5, ranges_vec[0]);
28581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(6, ranges_vec[1]);
28681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(10, ranges_vec[2]);
28781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(11, ranges_vec[3]);
28881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(20, ranges_vec[4]);
28981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(21, ranges_vec[5]);
29081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
29181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
29281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, CustomHistogramTest) {
29381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // A well prepared custom ranges.
29481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<HistogramBase::Sample> custom_ranges;
29581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(1);
29681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(2);
29781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
29881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
29981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet("TestCustomHistogram1", custom_ranges,
30081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                                  HistogramBase::kNoFlags));
30181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const BucketRanges* ranges = histogram->bucket_ranges();
30281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ASSERT_EQ(4u, ranges->size());
30381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges->range(0));  // Auto added.
30481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, ranges->range(1));
30581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, ranges->range(2));
30681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(3));  // Auto added.
30781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
30881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // A unordered custom ranges.
30981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.clear();
31081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(2);
31181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(1);
31269df84967f17d0c3d78ee982101ade85077f8662Theodore Ts'o  histogram = static_cast<Histogram*>(
31369df84967f17d0c3d78ee982101ade85077f8662Theodore Ts'o      CustomHistogram::FactoryGet("TestCustomHistogram2", custom_ranges,
31469df84967f17d0c3d78ee982101ade85077f8662Theodore Ts'o                                  HistogramBase::kNoFlags));
31569df84967f17d0c3d78ee982101ade85077f8662Theodore Ts'o  ranges = histogram->bucket_ranges();
31681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ASSERT_EQ(4u, ranges->size());
31781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges->range(0));
31881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, ranges->range(1));
31981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, ranges->range(2));
32081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(3));
32181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
32281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // A custom ranges with duplicated values.
32381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.clear();
32481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(4);
32581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(1);
32681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(4);
32781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram = static_cast<Histogram*>(
32881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet("TestCustomHistogram3", custom_ranges,
32981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                                  HistogramBase::kNoFlags));
33081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ranges = histogram->bucket_ranges();
33181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ASSERT_EQ(4u, ranges->size());
33281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges->range(0));
33381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, ranges->range(1));
33481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(4, ranges->range(2));
33581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(3));
33681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
33781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
33881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, CustomHistogramWithOnly2Buckets) {
33981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // This test exploits the fact that the CustomHistogram can have 2 buckets,
34081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // while the base class Histogram is *supposed* to have at least 3 buckets.
34181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // We should probably change the restriction on the base class (or not inherit
34281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // the base class!).
34381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
34481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<HistogramBase::Sample> custom_ranges;
34581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(4);
34681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
34781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
34881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges,
34981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                                  HistogramBase::kNoFlags));
35081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const BucketRanges* ranges = histogram->bucket_ranges();
35181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ASSERT_EQ(3u, ranges->size());
35281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges->range(0));
35381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(4, ranges->range(1));
35481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2));
35581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
35681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
35781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, AddCountTest) {
35881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const size_t kBucketCount = 50;
35981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
36081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("AddCountHistogram", 10, 100, kBucketCount,
36181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                            HistogramBase::kNoFlags));
3624e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o
36381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(20, 15);
36481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(30, 14);
36581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
36681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
36781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(29, samples->TotalCount());
36881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(15, samples->GetCount(20));
36981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(14, samples->GetCount(30));
37081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
37181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(20, 25);
37281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(30, 24);
37381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
37481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples();
37581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(78, samples2->TotalCount());
37681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(40, samples2->GetCount(20));
37781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(38, samples2->GetCount(30));
37881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
37981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
38081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, AddCount_LargeValuesDontOverflow) {
38181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const size_t kBucketCount = 50;
38281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
38381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("AddCountHistogram", 10, 1000000000, kBucketCount,
38481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                            HistogramBase::kNoFlags));
38581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
38681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(200000000, 15);
38781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(300000000, 14);
38881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
38981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
39081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(29, samples->TotalCount());
39181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(15, samples->GetCount(200000000));
39281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(14, samples->GetCount(300000000));
39381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
39481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(200000000, 25);
39581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->AddCount(300000000, 24);
39681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
39781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples();
39881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(78, samples2->TotalCount());
39981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(40, samples2->GetCount(200000000));
40081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(38, samples2->GetCount(300000000));
40181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(19400000000LL, samples2->sum());
40281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
40381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
40481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Make sure histogram handles out-of-bounds data gracefully.
40581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, BoundsTest) {
40681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const size_t kBucketCount = 50;
40781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
40881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Bounded", 10, 100, kBucketCount,
40981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                            HistogramBase::kNoFlags));
41081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
41181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Put two samples "out of bounds" above and below.
41281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(5);
41381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(-50);
41481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
41581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(100);
41681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(10000);
41781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
41881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Verify they landed in the underflow, and overflow buckets.
41981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<SampleVector> samples = histogram->SnapshotSampleVector();
42081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->GetCountAtIndex(0));
42181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, samples->GetCountAtIndex(1));
42281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  size_t array_size = histogram->bucket_count();
42381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(kBucketCount, array_size);
42481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, samples->GetCountAtIndex(array_size - 2));
42581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, samples->GetCountAtIndex(array_size - 1));
42681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
42781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<int> custom_ranges;
42881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(10);
42981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(50);
43081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(100);
43181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* test_custom_histogram = static_cast<Histogram*>(
43281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet("TestCustomRangeBoundedHistogram",
43381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                                  custom_ranges, HistogramBase::kNoFlags));
43481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
43581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Put two samples "out of bounds" above and below.
43681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  test_custom_histogram->Add(5);
43781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  test_custom_histogram->Add(-50);
43881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  test_custom_histogram->Add(100);
43981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  test_custom_histogram->Add(1000);
44081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  test_custom_histogram->Add(INT_MAX);
44181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
44281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Verify they landed in the underflow, and overflow buckets.
44381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<SampleVector> custom_samples =
44481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      test_custom_histogram->SnapshotSampleVector();
44581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, custom_samples->GetCountAtIndex(0));
44681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, custom_samples->GetCountAtIndex(1));
44781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  size_t bucket_count = test_custom_histogram->bucket_count();
44881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, custom_samples->GetCountAtIndex(bucket_count - 2));
44981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(3, custom_samples->GetCountAtIndex(bucket_count - 1));
45081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
45181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
45281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// Check to be sure samples land as expected is "correct" buckets.
45381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, BucketPlacementTest) {
45481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
45581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
45681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
45781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Add i+1 samples to the i'th bucket.
45881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(0);
45981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int power_of_2 = 1;
46081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 1; i < 8; i++) {
46181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    for (int j = 0; j <= i; j++)
46281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      histogram->Add(power_of_2);
46381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    power_of_2 *= 2;
46481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
46581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
46681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Check to see that the bucket counts reflect our additions.
46781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<SampleVector> samples = histogram->SnapshotSampleVector();
46881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 0; i < 8; i++)
46981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    EXPECT_EQ(i + 1, samples->GetCountAtIndex(i));
47081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
47181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
47281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, CorruptSampleCounts) {
47381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // The internal code creates histograms via macros and thus keeps static
47481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // pointers to them. If those pointers are to persistent memory which will
47581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // be free'd then any following calls to that code will crash with a
47681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // segmentation violation.
47781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  if (use_persistent_histogram_allocator_)
47881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    return;
47981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
48081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
48181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
48281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
48381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Add some samples.
48481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(20);
48581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->Add(40);
48681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
48781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<SampleVector> snapshot = histogram->SnapshotSampleVector();
48881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES,
48981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
49081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, snapshot->redundant_count());
49181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(2, snapshot->TotalCount());
49281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
49381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  snapshot->counts_[3] += 100;  // Sample count won't match redundant count.
49481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::COUNT_LOW_ERROR,
49581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
49681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  snapshot->counts_[2] -= 200;
49781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::COUNT_HIGH_ERROR,
49881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
49981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
50081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // But we can't spot a corruption if it is compensated for.
50181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  snapshot->counts_[1] += 100;
50281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES,
50381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
50481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
50581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
50681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, CorruptBucketBounds) {
50781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
50881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
50981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
51081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::unique_ptr<HistogramSamples> snapshot = histogram->SnapshotSamples();
51181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES,
51281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
51381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
51481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  BucketRanges* bucket_ranges =
51581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      const_cast<BucketRanges*>(histogram->bucket_ranges());
51681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase::Sample tmp = bucket_ranges->range(1);
51781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(1, bucket_ranges->range(2));
51881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(2, tmp);
51981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(
52081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR,
52181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      histogram->FindCorruption(*snapshot));
52281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
52381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(2, bucket_ranges->range(1));
52481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(1, tmp);
52581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0U, histogram->FindCorruption(*snapshot));
52681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
52781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Show that two simple changes don't offset each other
52881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(3, bucket_ranges->range(3) + 1);
52981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR,
53081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
53181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
53281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(4, bucket_ranges->range(4) - 1);
53381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR,
53481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            histogram->FindCorruption(*snapshot));
53581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
53681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Repair histogram so that destructor won't DCHECK().
53781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(3, bucket_ranges->range(3) - 1);
53881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bucket_ranges->set_range(4, bucket_ranges->range(4) + 1);
53981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
54081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
54181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, HistogramSerializeInfo) {
54281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* histogram = static_cast<Histogram*>(
54381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      Histogram::FactoryGet("Histogram", 1, 64, 8,
54481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                            HistogramBase::kIPCSerializationSourceFlag));
54581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Pickle pickle;
54681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  histogram->SerializeInfo(&pickle);
54781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
54881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  PickleIterator iter(pickle);
54981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
55081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int type;
55181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&type));
55281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HISTOGRAM, type);
55381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
55481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::string name;
55581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadString(&name));
55681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ("Histogram", name);
55781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
55881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int flag;
55981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&flag));
56081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag,
56181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong            flag & ~HistogramBase::kIsPersistent);
56281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
56381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int min;
56481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&min));
56581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(1, min);
56681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
56781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int max;
56881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&max));
56981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(64, max);
57081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
57181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  uint32_t bucket_count;
57281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadUInt32(&bucket_count));
57381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(8u, bucket_count);
57481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
57581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  uint32_t checksum;
57681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadUInt32(&checksum));
57781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(histogram->bucket_ranges()->checksum(), checksum);
57881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
57981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // No more data in the pickle.
58081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_FALSE(iter.SkipBytes(1));
58181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
58281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
58381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, CustomHistogramSerializeInfo) {
58481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<int> custom_ranges;
58581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(10);
58681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(100);
58781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
58881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
58981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "TestCustomRangeBoundedHistogram",
59081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      custom_ranges,
59181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      HistogramBase::kNoFlags);
59281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Pickle pickle;
59381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_histogram->SerializeInfo(&pickle);
59481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
59581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Validate the pickle.
59681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  PickleIterator iter(pickle);
59781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
59881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int i;
59981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::string s;
60081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  uint32_t bucket_count;
60181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  uint32_t ui32;
60281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&i) && iter.ReadString(&s) && iter.ReadInt(&i) &&
60381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong              iter.ReadInt(&i) && iter.ReadInt(&i) &&
60481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong              iter.ReadUInt32(&bucket_count) && iter.ReadUInt32(&ui32));
60581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(3u, bucket_count);
60681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
60781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int range;
60881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&range));
60981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(10, range);
61081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(iter.ReadInt(&range));
61181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(100, range);
61281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
61381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // No more data in the pickle.
61481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_FALSE(iter.SkipBytes(1));
61581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
61681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
61781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, BadConstruction) {
61881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* histogram = Histogram::FactoryGet(
61981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadConstruction", 0, 100, 8, HistogramBase::kNoFlags);
62081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(histogram->HasConstructionArguments(1, 100, 8));
62181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
62281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Try to get the same histogram name with different arguments.
6234e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  HistogramBase* bad_histogram = Histogram::FactoryGet(
62481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadConstruction", 0, 100, 7, HistogramBase::kNoFlags);
62581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(NULL, bad_histogram);
62681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bad_histogram = Histogram::FactoryGet(
62781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadConstruction", 0, 99, 8, HistogramBase::kNoFlags);
62881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(NULL, bad_histogram);
62981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
63081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
63181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadConstructionLinear", 0, 100, 8, HistogramBase::kNoFlags);
63281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(linear_histogram->HasConstructionArguments(1, 100, 8));
63381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
63481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Try to get the same histogram name with different arguments.
63581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bad_histogram = LinearHistogram::FactoryGet(
63681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadConstructionLinear", 0, 100, 7, HistogramBase::kNoFlags);
63781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(NULL, bad_histogram);
63881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  bad_histogram = LinearHistogram::FactoryGet(
63981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadConstructionLinear", 10, 100, 8, HistogramBase::kNoFlags);
64081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(NULL, bad_histogram);
64181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
64281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
64381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST_P(HistogramTest, FactoryTime) {
64481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const int kTestCreateCount = 1 << 14;  // Must be power-of-2.
64581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const int kTestLookupCount = 100000;
64681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const int kTestAddCount = 1000000;
64781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
64881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Create all histogram names in advance for accurate timing below.
64981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<std::string> histogram_names;
65081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 0; i < kTestCreateCount; ++i) {
65181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    histogram_names.push_back(
65281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong        StringPrintf("TestHistogram.%d", i % kTestCreateCount));
65381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
65481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
65581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Calculate cost of creating histograms.
65681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  TimeTicks create_start = TimeTicks::Now();
65781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 0; i < kTestCreateCount; ++i) {
65881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    Histogram::FactoryGet(histogram_names[i], 1, 100, 10,
65981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                          HistogramBase::kNoFlags);
66081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
66181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  TimeDelta create_ticks = TimeTicks::Now() - create_start;
66281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int64_t create_ms = create_ticks.InMilliseconds();
66381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
66481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  VLOG(1) << kTestCreateCount << " histogram creations took " << create_ms
66581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << "ms or about "
66681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << (create_ms * 1000000) / kTestCreateCount
66781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << "ns each.";
66881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
66981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Calculate cost of looking up existing histograms.
67081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  TimeTicks lookup_start = TimeTicks::Now();
67181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 0; i < kTestLookupCount; ++i) {
67281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // 6007 is co-prime with kTestCreateCount and so will do lookups in an
67381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // order less likely to be cacheable (but still hit them all) should the
67481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    // underlying storage use the exact histogram name as the key.
67581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    const int i_mult = 6007;
67681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    static_assert(i_mult < INT_MAX / kTestCreateCount, "Multiplier too big");
67781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    int index = (i * i_mult) & (kTestCreateCount - 1);
67881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    Histogram::FactoryGet(histogram_names[index], 1, 100, 10,
67981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                          HistogramBase::kNoFlags);
68081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  }
68181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  TimeDelta lookup_ticks = TimeTicks::Now() - lookup_start;
68281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int64_t lookup_ms = lookup_ticks.InMilliseconds();
68381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
68481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  VLOG(1) << kTestLookupCount << " histogram lookups took " << lookup_ms
68581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << "ms or about "
68681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << (lookup_ms * 1000000) / kTestLookupCount
68781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << "ns each.";
68881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
68981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // Calculate cost of accessing histograms.
69081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* histogram = Histogram::FactoryGet(
69181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      histogram_names[0], 1, 100, 10, HistogramBase::kNoFlags);
6924e222d9b88b7ff145ad03272acf752c1e7a18964Theodore Ts'o  ASSERT_TRUE(histogram);
69381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  TimeTicks add_start = TimeTicks::Now();
69481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  for (int i = 0; i < kTestAddCount; ++i)
69581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong    histogram->Add(i & 127);
69681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  TimeDelta add_ticks = TimeTicks::Now() - add_start;
69781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  int64_t add_ms = add_ticks.InMilliseconds();
69881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
69981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  VLOG(1) << kTestAddCount << " histogram adds took " << add_ms
70081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << "ms or about "
70181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << (add_ms * 1000000) / kTestAddCount
70281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          << "ns each.";
70381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
70481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
70581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// For Histogram, LinearHistogram and CustomHistogram, the minimum for a
70681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX -
70781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// 1). But we accept ranges exceeding those limits, and silently clamped to
70881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong// those limits. This is for backwards compatibility.
70981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. WongTEST(HistogramDeathTest, BadRangesTest) {
71081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* histogram = Histogram::FactoryGet(
71181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadRanges", 0, HistogramBase::kSampleType_MAX, 8,
71281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      HistogramBase::kNoFlags);
71381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(
71481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      histogram->HasConstructionArguments(
71581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          1, HistogramBase::kSampleType_MAX - 1, 8));
71681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
71781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
71881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      "BadRangesLinear", 0, HistogramBase::kSampleType_MAX, 8,
71981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      HistogramBase::kNoFlags);
72081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_TRUE(
72181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      linear_histogram->HasConstructionArguments(
72281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          1, HistogramBase::kSampleType_MAX - 1, 8));
72381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
72481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  std::vector<int> custom_ranges;
72581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(0);
72681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(5);
72781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  Histogram* custom_histogram = static_cast<Histogram*>(
72881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet(
72981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong          "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags));
73081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  const BucketRanges* ranges = custom_histogram->bucket_ranges();
73181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  ASSERT_EQ(3u, ranges->size());
73281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(0, ranges->range(0));
73381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(5, ranges->range(1));
73481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2));
73581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
73681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // CustomHistogram does not accepts kSampleType_MAX as range.
73781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(HistogramBase::kSampleType_MAX);
73881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_DEATH_IF_SUPPORTED(
73981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet("BadRangesCustom2", custom_ranges,
74081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                                  HistogramBase::kNoFlags),
74181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong               "");
74281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
74381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  // CustomHistogram needs at least 1 valid range.
74481cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.clear();
74581cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  custom_ranges.push_back(0);
74681cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong  EXPECT_DEATH_IF_SUPPORTED(
74781cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong      CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
74881cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong                                  HistogramBase::kNoFlags),
74981cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong               "");
75081cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}
75181cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong
75281cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong}  // namespace base
75381cbf1ef4f5dab855edbde7a1d4dfa55ea785c38Darrick J. Wong