• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "base/test/statistics_delta_reader.h"
6 
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/histogram_samples.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/stl_util.h"
11 
12 namespace base {
13 
StatisticsDeltaReader()14 StatisticsDeltaReader::StatisticsDeltaReader() {
15   StatisticsRecorder::Initialize();  // Safe to call multiple times.
16 
17   // Record any histogram data that exists when the object is created so it can
18   // be subtracted later.
19   StatisticsRecorder::Histograms histograms;
20   StatisticsRecorder::GetSnapshot(std::string(), &histograms);
21   for (size_t i = 0; i < histograms.size(); ++i) {
22     original_samples_[histograms[i]->histogram_name()] =
23         histograms[i]->SnapshotSamples().release();
24   }
25 }
26 
~StatisticsDeltaReader()27 StatisticsDeltaReader::~StatisticsDeltaReader() {
28   STLDeleteValues(&original_samples_);
29 }
30 
31 scoped_ptr<HistogramSamples>
GetHistogramSamplesSinceCreation(const std::string & histogram_name)32     StatisticsDeltaReader::GetHistogramSamplesSinceCreation(
33         const std::string& histogram_name) {
34   HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name);
35   if (!histogram)
36     return scoped_ptr<HistogramSamples>();
37   scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples());
38   HistogramSamples* named_original_samples = original_samples_[histogram_name];
39   if (named_original_samples)
40     named_samples->Subtract(*named_original_samples);
41   return named_samples.Pass();
42 }
43 
44 }  // namespace base
45