Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / performance_monitor / metric.cc
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 #include "chrome/browser/performance_monitor/metric.h"
6
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/performance_monitor/constants.h"
12
13 namespace performance_monitor {
14
15 namespace {
16
17 // For certain metrics (for instance, bytes read), it is possible that there is
18 // no maximum value which we can safely assume.
19 const double kNoMaximum = -1.0;
20
21 // These constants are designed to keep metrics reasonable. However, due to the
22 // variety of system configurations which can run chrome, these values may not
23 // catch *all* erroneous values. For instance, on a one-CPU machine, any CPU
24 // usage > 100 is erroneous, but on a 16-CPU machine, it's perfectly normal.
25 // These are "best-guesses" in order to weed out obviously-false values. A
26 // metric is valid if it is greater than or equal to the minimum and less than
27 // the maximum, i.e. if it falls in the range [min, max).
28 const double kMinUndefined = 0.0;
29 const double kMaxUndefined = 0.0;  // No undefined metric is valid.
30 const double kMinCpuUsage = 0.0;
31 const double kMaxCpuUsage = 100000.0;  // 100% on a 1000-CPU machine.
32 const double kMinPrivateMemoryUsage = 0.0;
33 const double kMaxPrivateMemoryUsage = kBytesPerTerabyte;
34 const double kMinSharedMemoryUsage = 0.0;
35 const double kMaxSharedMemoryUsage = kBytesPerTerabyte;
36 const double kMinStartupTime = 0.0;
37 const double kMaxStartupTime = base::Time::kMicrosecondsPerMinute * 15.0;
38 const double kMinTestStartupTime = 0.0;
39 const double kMaxTestStartupTime = base::Time::kMicrosecondsPerMinute * 15.0;
40 const double kMinSessionRestoreTime = 0.0;
41 const double kMaxSessionRestoreTime = base::Time::kMicrosecondsPerMinute * 15.0;
42 const double kMinPageLoadTime = 0.0;
43 const double kMaxPageLoadTime = base::Time::kMicrosecondsPerMinute * 15.0;
44 const double kMinNetworkBytesRead = 0.0;
45 const double kMaxNetworkBytesRead = kNoMaximum;
46
47 struct MetricBound {
48   double min;
49   double max;
50 };
51
52 const MetricBound kMetricBounds[] = {
53  { kMinUndefined, kMaxUndefined },
54  { kMinCpuUsage, kMaxCpuUsage },
55  { kMinPrivateMemoryUsage, kMaxPrivateMemoryUsage },
56  { kMinSharedMemoryUsage, kMaxSharedMemoryUsage },
57  { kMinStartupTime, kMaxStartupTime },
58  { kMinTestStartupTime, kMaxTestStartupTime },
59  { kMinSessionRestoreTime, kMaxSessionRestoreTime },
60  { kMinPageLoadTime, kMaxPageLoadTime },
61  { kMinNetworkBytesRead, kMaxNetworkBytesRead },
62 };
63
64 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMetricBounds) == METRIC_NUMBER_OF_METRICS,
65                metric_bounds_size_doesnt_match_metric_count);
66
67 }  // namespace
68
69 Metric::Metric() : type(METRIC_UNDEFINED), value(0.0) {
70 }
71
72 Metric::Metric(MetricType metric_type,
73                const base::Time& metric_time,
74                const double metric_value)
75     : type(metric_type), time(metric_time), value(metric_value) {
76 }
77
78 Metric::Metric(MetricType metric_type,
79                const std::string& metric_time,
80                const std::string& metric_value) : type(metric_type) {
81   int64 conversion = 0;
82   base::StringToInt64(metric_time, &conversion);
83   time = base::Time::FromInternalValue(conversion);
84   CHECK(base::StringToDouble(metric_value, &value));
85 }
86
87 Metric::~Metric() {
88 }
89
90 bool Metric::IsValid() const {
91   return type < METRIC_NUMBER_OF_METRICS &&
92       (value < kMetricBounds[type].max ||
93           kMetricBounds[type].max == kNoMaximum) &&
94       value >= kMetricBounds[type].min;
95 }
96
97 std::string Metric::ValueAsString() const {
98   return base::DoubleToString(value);
99 }
100
101 }  // namespace performance_monitor