summaryrefslogtreecommitdiffstats
path: root/base/metrics
diff options
context:
space:
mode:
authorjrummell@chromium.org <jrummell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-05 21:21:54 +0000
committerjrummell@chromium.org <jrummell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-05 21:21:54 +0000
commitf2bb320d3911c96b166af096ef0cccc54cdf335a (patch)
tree586791512394847026a9accbd2ea4895ba3ca856 /base/metrics
parentc157cb947705130c8baadee305c6d3b0a01b5785 (diff)
downloadchromium_src-f2bb320d3911c96b166af096ef0cccc54cdf335a.zip
chromium_src-f2bb320d3911c96b166af096ef0cccc54cdf335a.tar.gz
chromium_src-f2bb320d3911c96b166af096ef0cccc54cdf335a.tar.bz2
Implement write methods for SparseHistogram
Implementation based on existing methods in Histogram. BUG= Review URL: https://chromiumcodereview.appspot.com/13469020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192636 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/metrics')
-rw-r--r--base/metrics/histogram.cc28
-rw-r--r--base/metrics/histogram.h9
-rw-r--r--base/metrics/histogram_base.cc32
-rw-r--r--base/metrics/histogram_base.h15
-rw-r--r--base/metrics/sparse_histogram.cc72
-rw-r--r--base/metrics/sparse_histogram.h9
6 files changed, 127 insertions, 38 deletions
diff --git a/base/metrics/histogram.cc b/base/metrics/histogram.cc
index 1f26829..46f2c16 100644
--- a/base/metrics/histogram.cc
+++ b/base/metrics/histogram.cc
@@ -340,12 +340,7 @@ double Histogram::GetBucketSize(Count current, size_t i) const {
}
const string Histogram::GetAsciiBucketRange(size_t i) const {
- string result;
- if (kHexRangePrintingFlag & flags())
- StringAppendF(&result, "%#x", ranges(i));
- else
- StringAppendF(&result, "%d", ranges(i));
- return result;
+ return GetSimpleAsciiBucketRange(ranges(i));
}
//------------------------------------------------------------------------------
@@ -490,27 +485,6 @@ void Histogram::WriteAsciiBucketContext(const int64 past,
}
}
-void Histogram::WriteAsciiBucketValue(Count current,
- double scaled_sum,
- string* output) const {
- StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
-}
-
-void Histogram::WriteAsciiBucketGraph(double current_size,
- double max_size,
- string* output) const {
- const int k_line_length = 72; // Maximal horizontal width of graph.
- int x_count = static_cast<int>(k_line_length * (current_size / max_size)
- + 0.5);
- int x_remainder = k_line_length - x_count;
-
- while (0 < x_count--)
- output->append("-");
- output->append("O");
- while (0 < x_remainder--)
- output->append(" ");
-}
-
void Histogram::GetParameters(DictionaryValue* params) const {
params->SetString("type", HistogramTypeToString(GetHistogramType()));
params->SetInteger("min", declared_min());
diff --git a/base/metrics/histogram.h b/base/metrics/histogram.h
index fe7b794..8d059f9 100644
--- a/base/metrics/histogram.h
+++ b/base/metrics/histogram.h
@@ -511,15 +511,6 @@ class BASE_EXPORT Histogram : public HistogramBase {
const int64 remaining, const size_t i,
std::string* output) const;
- // Write textual description of the bucket contents (relative to histogram).
- // Output is the count in the buckets, as well as the percentage.
- void WriteAsciiBucketValue(Count current, double scaled_sum,
- std::string* output) const;
-
- // Produce actual graph (set of blank vs non blank char's) for a bucket.
- void WriteAsciiBucketGraph(double current_size, double max_size,
- std::string* output) const;
-
// WriteJSON calls these.
virtual void GetParameters(DictionaryValue* params) const OVERRIDE;
diff --git a/base/metrics/histogram_base.cc b/base/metrics/histogram_base.cc
index 5272d03..51689a8 100644
--- a/base/metrics/histogram_base.cc
+++ b/base/metrics/histogram_base.cc
@@ -14,6 +14,7 @@
#include "base/metrics/sparse_histogram.h"
#include "base/pickle.h"
#include "base/process_util.h"
+#include "base/stringprintf.h"
#include "base/values.h"
namespace base {
@@ -124,4 +125,35 @@ void HistogramBase::WriteJSON(std::string* output) const {
serializer.Serialize(root);
}
+void HistogramBase::WriteAsciiBucketGraph(double current_size,
+ double max_size,
+ std::string* output) const {
+ const int k_line_length = 72; // Maximal horizontal width of graph.
+ int x_count = static_cast<int>(k_line_length * (current_size / max_size)
+ + 0.5);
+ int x_remainder = k_line_length - x_count;
+
+ while (0 < x_count--)
+ output->append("-");
+ output->append("O");
+ while (0 < x_remainder--)
+ output->append(" ");
+}
+
+const std::string HistogramBase::GetSimpleAsciiBucketRange(
+ Sample sample) const {
+ std::string result;
+ if (kHexRangePrintingFlag & flags())
+ StringAppendF(&result, "%#x", sample);
+ else
+ StringAppendF(&result, "%d", sample);
+ return result;
+}
+
+void HistogramBase::WriteAsciiBucketValue(Count current,
+ double scaled_sum,
+ std::string* output) const {
+ StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
+}
+
} // namespace base
diff --git a/base/metrics/histogram_base.h b/base/metrics/histogram_base.h
index 12d126d..2fa846e 100644
--- a/base/metrics/histogram_base.h
+++ b/base/metrics/histogram_base.h
@@ -142,6 +142,21 @@ protected:
// counts to |buckets| and the total sample count to |count|.
virtual void GetCountAndBucketData(Count* count,
ListValue* buckets) const = 0;
+
+ //// Produce actual graph (set of blank vs non blank char's) for a bucket.
+ void WriteAsciiBucketGraph(double current_size,
+ double max_size,
+ std::string* output) const;
+
+ // Return a string description of what goes in a given bucket.
+ const std::string GetSimpleAsciiBucketRange(Sample sample) const;
+
+ // Write textual description of the bucket contents (relative to histogram).
+ // Output is the count in the buckets, as well as the percentage.
+ void WriteAsciiBucketValue(Count current,
+ double scaled_sum,
+ std::string* output) const;
+
private:
const std::string histogram_name_;
int32 flags_;
diff --git a/base/metrics/sparse_histogram.cc b/base/metrics/sparse_histogram.cc
index 2952673..df361f7 100644
--- a/base/metrics/sparse_histogram.cc
+++ b/base/metrics/sparse_histogram.cc
@@ -7,6 +7,7 @@
#include "base/metrics/sample_map.h"
#include "base/metrics/statistics_recorder.h"
#include "base/pickle.h"
+#include "base/stringprintf.h"
#include "base/synchronization/lock.h"
using std::map;
@@ -69,11 +70,13 @@ bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
}
void SparseHistogram::WriteHTMLGraph(string* output) const {
- // TODO(kaiwang): Implement.
+ output->append("<PRE>");
+ WriteAsciiImpl(true, "<br>", output);
+ output->append("</PRE>");
}
void SparseHistogram::WriteAscii(string* output) const {
- // TODO(kaiwang): Implement.
+ WriteAsciiImpl(true, "\n", output);
}
bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
@@ -106,4 +109,69 @@ void SparseHistogram::GetCountAndBucketData(Count* count,
// TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
}
+void SparseHistogram::WriteAsciiImpl(bool graph_it,
+ const std::string& newline,
+ std::string* output) const {
+ // Get a local copy of the data so we are consistent.
+ scoped_ptr<HistogramSamples> snapshot = SnapshotSamples();
+ Count total_count = snapshot->TotalCount();
+ double scaled_total_count = total_count / 100.0;
+
+ WriteAsciiHeader(total_count, output);
+ output->append(newline);
+
+ // Determine how wide the largest bucket range is (how many digits to print),
+ // so that we'll be able to right-align starts for the graphical bars.
+ // Determine which bucket has the largest sample count so that we can
+ // normalize the graphical bar-width relative to that sample count.
+ Count largest_count = 0;
+ Sample largest_sample = 0;
+ scoped_ptr<SampleCountIterator> it = snapshot->Iterator();
+ while (!it->Done())
+ {
+ Sample min;
+ Sample max;
+ Count count;
+ it->Get(&min, &max, &count);
+ if (min > largest_sample)
+ largest_sample = min;
+ if (count > largest_count)
+ largest_count = count;
+ it->Next();
+ }
+ size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1;
+
+ // iterate over each item and display them
+ it = snapshot->Iterator();
+ while (!it->Done())
+ {
+ Sample min;
+ Sample max;
+ Count count;
+ it->Get(&min, &max, &count);
+
+ // value is min, so display it
+ string range = GetSimpleAsciiBucketRange(min);
+ output->append(range);
+ for (size_t j = 0; range.size() + j < print_width + 1; ++j)
+ output->push_back(' ');
+
+ if (graph_it)
+ WriteAsciiBucketGraph(count, largest_count, output);
+ WriteAsciiBucketValue(count, scaled_total_count, output);
+ output->append(newline);
+ it->Next();
+ }
+}
+
+void SparseHistogram::WriteAsciiHeader(const Count total_count,
+ std::string* output) const {
+ StringAppendF(output,
+ "Histogram: %s recorded %d samples",
+ histogram_name().c_str(),
+ total_count);
+ if (flags() & ~kHexRangePrintingFlag)
+ StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
+}
+
} // namespace base
diff --git a/base/metrics/sparse_histogram.h b/base/metrics/sparse_histogram.h
index 9c20766..4de2002 100644
--- a/base/metrics/sparse_histogram.h
+++ b/base/metrics/sparse_histogram.h
@@ -90,6 +90,15 @@ class BASE_EXPORT_PRIVATE SparseHistogram : public HistogramBase {
virtual void GetCountAndBucketData(Count* count,
ListValue* buckets) const OVERRIDE;
+ // Helpers for emitting Ascii graphic. Each method appends data to output.
+ void WriteAsciiImpl(bool graph_it,
+ const std::string& newline,
+ std::string* output) const;
+
+ // Write a common header message describing this histogram.
+ void WriteAsciiHeader(const Count total_count,
+ std::string* output) const;
+
// For constuctor calling.
friend class SparseHistogramTest;