1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_UI_WEBUI_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_UI_UTIL_H_
6#define CHROME_BROWSER_UI_WEBUI_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_UI_UTIL_H_
7
8#include "chrome/browser/performance_monitor/database.h"
9#include "chrome/browser/ui/webui/performance_monitor/performance_monitor_ui_constants.h"
10
11namespace performance_monitor {
12
13typedef std::vector<Database::MetricVector> VectorOfMetricVectors;
14
15class PerformanceMonitorUtilTest;
16
17// This is an interface for the various aggregation functions. The base class
18// will handle the separation of the metrics vector into the various intervals,
19// and each derived class represents a different method of aggregation.
20class Aggregator {
21 public:
22  Aggregator();
23  virtual ~Aggregator();
24
25  // Aggregate a full metric vector into a vector of metric vectors, with each
26  // metric vector representing the aggregation for an active interval. |start|
27  // is the start time of the metrics to aggregate, |intervals| is the vector
28  // of all intervals for which the database was active for the span to
29  // aggregate, and |resolution| is the time distance between aggregated points.
30  scoped_ptr<VectorOfMetricVectors> AggregateMetrics(
31      MetricType metric_type,
32      const Database::MetricVector* metrics,
33      const base::Time& start,
34      const std::vector<TimeRange>& intervals,
35      const base::TimeDelta& resolution);
36
37 protected:
38  friend class PerformanceMonitorUtilTest;
39
40  // Aggregate only a single interval, advancing the |metric| iterator to the
41  // end of the active interval (or the end of the vector). |metric| points to
42  // the start of the section to be aggregated, |metric_end| is the hard-limit
43  // for the end of the vector (so we don't advance past it), |time_start| and
44  // |time_end| represent the interval to aggregate, and |resolution| is the
45  // time distance between aggregated points.
46  virtual scoped_ptr<Database::MetricVector> AggregateInterval(
47      MetricType metric_type,
48      Database::MetricVector::const_iterator* metric,
49      const Database::MetricVector::const_iterator& metric_end,
50      const base::Time& time_start,
51      const base::Time& time_end,
52      const base::TimeDelta& resolution) = 0;
53};
54
55// Aggregation method classes.
56class NoAggregation : public Aggregator {
57 private:
58  virtual scoped_ptr<Database::MetricVector> AggregateInterval(
59      MetricType metric_type,
60      Database::MetricVector::const_iterator* metric,
61      const Database::MetricVector::const_iterator& metric_end,
62      const base::Time& time_start,
63      const base::Time& time_end,
64      const base::TimeDelta& resolution) OVERRIDE;
65};
66
67class MedianAggregation : public Aggregator {
68 private:
69  virtual scoped_ptr<Database::MetricVector> AggregateInterval(
70      MetricType metric_type,
71      Database::MetricVector::const_iterator* metric,
72      const Database::MetricVector::const_iterator& metric_end,
73      const base::Time& time_start,
74      const base::Time& time_end,
75      const base::TimeDelta& resolution) OVERRIDE;
76};
77
78class MeanAggregation : public Aggregator {
79 private:
80  virtual scoped_ptr<Database::MetricVector> AggregateInterval(
81      MetricType metric_type,
82      Database::MetricVector::const_iterator* metric,
83      const Database::MetricVector::const_iterator& metric_end,
84      const base::Time& time_start,
85      const base::Time& time_end,
86      const base::TimeDelta& resolution) OVERRIDE;
87};
88
89// Return the factor by which all metrics should be multiplied in order to be in
90// the preferred unit (e.g., memory usage is in bytes, but we display it in
91// megabytes, so we return 1/1024^2).
92double GetConversionFactor(UnitDetails from, UnitDetails to);
93
94// Metric data can be either dense or sporadic, so AggregateMetric() normalizes
95// the metric data in time. |metrics| must be sorted in increasing time.
96// AggregateMetrics() will perform the aggregation according to the |strategy|
97// provided.
98//
99// Median: Use the median of sample values from the window, ignoring
100//         irregularity in sample timings.
101// Mean: Use the mean value within the window.
102// None: Return all samples from the window.
103//
104// In the methods which do aggregation, sampling windows start and end at an
105// integer multiple of |resolution| away from |start| and data points are
106// omitted if there are no points to resample. The time associated with each
107// slice is the time at the end of the slice. This aggregation is performed for
108// each interval within |intervals|.
109//
110// Returns a pointer to a vector of MetricVectors, with one MetricVector per
111// interval given, NULL if |metrics| is empty.
112scoped_ptr<std::vector<Database::MetricVector> > AggregateMetric(
113    MetricType type,
114    const Database::MetricVector* metrics,
115    const base::Time& start,
116    const std::vector<TimeRange>& intervals,
117    const base::TimeDelta& resolution,
118    AggregationMethod method);
119
120}  // namespace performance_monitor
121
122#endif  // CHROME_BROWSER_UI_WEBUI_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_UI_UTIL_H_
123