Commit f2bb320d authored by jrummell@chromium.org's avatar jrummell@chromium.org

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
parent c157cb94
...@@ -340,12 +340,7 @@ double Histogram::GetBucketSize(Count current, size_t i) const { ...@@ -340,12 +340,7 @@ double Histogram::GetBucketSize(Count current, size_t i) const {
} }
const string Histogram::GetAsciiBucketRange(size_t i) const { const string Histogram::GetAsciiBucketRange(size_t i) const {
string result; return GetSimpleAsciiBucketRange(ranges(i));
if (kHexRangePrintingFlag & flags())
StringAppendF(&result, "%#x", ranges(i));
else
StringAppendF(&result, "%d", ranges(i));
return result;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
...@@ -490,27 +485,6 @@ void Histogram::WriteAsciiBucketContext(const int64 past, ...@@ -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 { void Histogram::GetParameters(DictionaryValue* params) const {
params->SetString("type", HistogramTypeToString(GetHistogramType())); params->SetString("type", HistogramTypeToString(GetHistogramType()));
params->SetInteger("min", declared_min()); params->SetInteger("min", declared_min());
......
...@@ -511,15 +511,6 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -511,15 +511,6 @@ class BASE_EXPORT Histogram : public HistogramBase {
const int64 remaining, const size_t i, const int64 remaining, const size_t i,
std::string* output) const; 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. // WriteJSON calls these.
virtual void GetParameters(DictionaryValue* params) const OVERRIDE; virtual void GetParameters(DictionaryValue* params) const OVERRIDE;
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/metrics/sparse_histogram.h" #include "base/metrics/sparse_histogram.h"
#include "base/pickle.h" #include "base/pickle.h"
#include "base/process_util.h" #include "base/process_util.h"
#include "base/stringprintf.h"
#include "base/values.h" #include "base/values.h"
namespace base { namespace base {
...@@ -124,4 +125,35 @@ void HistogramBase::WriteJSON(std::string* output) const { ...@@ -124,4 +125,35 @@ void HistogramBase::WriteJSON(std::string* output) const {
serializer.Serialize(root); 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 } // namespace base
...@@ -142,6 +142,21 @@ protected: ...@@ -142,6 +142,21 @@ protected:
// counts to |buckets| and the total sample count to |count|. // counts to |buckets| and the total sample count to |count|.
virtual void GetCountAndBucketData(Count* count, virtual void GetCountAndBucketData(Count* count,
ListValue* buckets) const = 0; 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: private:
const std::string histogram_name_; const std::string histogram_name_;
int32 flags_; int32 flags_;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/metrics/sample_map.h" #include "base/metrics/sample_map.h"
#include "base/metrics/statistics_recorder.h" #include "base/metrics/statistics_recorder.h"
#include "base/pickle.h" #include "base/pickle.h"
#include "base/stringprintf.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
using std::map; using std::map;
...@@ -69,11 +70,13 @@ bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { ...@@ -69,11 +70,13 @@ bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
} }
void SparseHistogram::WriteHTMLGraph(string* output) const { 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 { void SparseHistogram::WriteAscii(string* output) const {
// TODO(kaiwang): Implement. WriteAsciiImpl(true, "\n", output);
} }
bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
...@@ -106,4 +109,69 @@ void SparseHistogram::GetCountAndBucketData(Count* count, ...@@ -106,4 +109,69 @@ void SparseHistogram::GetCountAndBucketData(Count* count,
// TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) // 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 } // namespace base
...@@ -90,6 +90,15 @@ class BASE_EXPORT_PRIVATE SparseHistogram : public HistogramBase { ...@@ -90,6 +90,15 @@ class BASE_EXPORT_PRIVATE SparseHistogram : public HistogramBase {
virtual void GetCountAndBucketData(Count* count, virtual void GetCountAndBucketData(Count* count,
ListValue* buckets) const OVERRIDE; 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. // For constuctor calling.
friend class SparseHistogramTest; friend class SparseHistogramTest;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment