blob: 5a573d8a991c4e9d291d8697cd9b041b489b6eeb [file] [log] [blame]
// Copyright 2018 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 <string>
#include "base/component_export.h"
// This namespace contains functions for creating histograms bucketed by number
// of tabs.
// All bucket parameters --- number of buckets, bucket sizes, bucket names ---
// are determined at compile time, and these methods are safe to call from any
// thread.
// A typical example of creating a histogram bucketed by tab count using
// STATIC_HISTOGRAM_POINTER_GROUP looks something like this:
// const size_t live_tab_count = GetLiveTabCount();
// const size_t bucket =
// tab_count_metrics::BucketForTabCount(live_tab_count);
// tab_count_metrics::HistogramName(constant_histogram_prefix,
// live_tabs_only, bucket),
// static_cast<int>(bucket),
// static_cast<int>(tab_count_metrics::kNumTabCountBuckets),
// Add(sample),
// base::Histogram::FactoryGet(
// tab_count_metrics::HistogramName(constant_histogram_prefix,
// live_tabs_only, bucket),
// base::HistogramBase::kUmaTargetedHistogramFlag));
// }
namespace tab_count_metrics {
// |kNumTabCountBuckets| is used in various constexpr arrays and as a bound
// on the histogram array when using STATIC_HISTOGRAM_POINTER_GROUP. This value
// must be equal to the length of the array of tab count bucket min values
// (|kTabCountBucketMins|) and the array of bucket names
// (|kTabCountBucketNames|) found in the corresponding .cc file.
constexpr size_t kNumTabCountBuckets = 8;
// Returns the histogram name for |bucket|. The histogram name is the
// concatenation of |prefix| and the name corresponding to |bucket|, which is of
// the form |prefix| + ".ByTabCount."/".ByLiveTabCount." + <BucketRangeText>,
// where <BucketRangeText> is a string describing the bucket range, e.g. "1Tab",
// "3To4Tabs", etc. See |kTabCountBucketNames| for all of the bucket names.
// |bucket| must be in the interval [0, |kNumTabCountBuckets|).
std::string HistogramName(const std::string prefix,
bool live_tabs_only,
size_t bucket);
// Return the bucket index for the |num_tabs|.
size_t BucketForTabCount(size_t num_tabs);
// These are exposed for unit tests.
namespace internal {
// Returns the number of tabs corresponding to the minimum value of |bucket|.
// |bucket| must be in the interval [0, |kNumTabCountBuckets|).
size_t BucketMin(size_t bucket);
// Returns the number of tabs corresponding to the maximum value of |bucket|.
// |bucket| must be in the interval [0, |kNumTabCountBuckets|).
size_t BucketMax(size_t bucket);
// Returns true if |num_tabs| falls within |bucket|.
// |bucket| must be in the interval [0, |kNumTabCountBuckets|).
bool IsInBucket(size_t num_tabs, size_t bucket);
} // namespace internal
} // namespace tab_count_metrics