OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/performance_monitor/metric.h" | 5 #include "chrome/browser/performance_monitor/metric.h" |
6 | 6 |
| 7 #include "base/basictypes.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/time.h" |
| 11 #include "chrome/browser/performance_monitor/constants.h" |
9 | 12 |
10 namespace performance_monitor { | 13 namespace performance_monitor { |
11 | 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 |
12 Metric::Metric() { | 69 Metric::Metric() { |
13 value = 0.0; | 70 value = 0.0; |
14 } | 71 } |
15 | 72 |
16 Metric::Metric(const base::Time& metric_time, const double metric_value) | 73 Metric::Metric(MetricType metric_type, |
17 : time(metric_time), value(metric_value) { | 74 const base::Time& metric_time, |
| 75 const double metric_value) |
| 76 : type(metric_type), time(metric_time), value(metric_value) { |
18 } | 77 } |
19 | 78 |
20 Metric::Metric(const std::string& metric_time, | 79 Metric::Metric(MetricType metric_type, |
21 const std::string& metric_value) { | 80 const std::string& metric_time, |
| 81 const std::string& metric_value) : type(metric_type) { |
22 int64 conversion = 0; | 82 int64 conversion = 0; |
23 base::StringToInt64(metric_time, &conversion); | 83 base::StringToInt64(metric_time, &conversion); |
24 time = base::Time::FromInternalValue(conversion); | 84 time = base::Time::FromInternalValue(conversion); |
25 CHECK(base::StringToDouble(metric_value, &value)); | 85 CHECK(base::StringToDouble(metric_value, &value)); |
26 } | 86 } |
27 | 87 |
28 Metric::~Metric() { | 88 Metric::~Metric() { |
29 } | 89 } |
30 | 90 |
| 91 bool Metric::IsValid() const { |
| 92 return type < METRIC_NUMBER_OF_METRICS && |
| 93 (value < kMetricBounds[type].max || |
| 94 kMetricBounds[type].max == kNoMaximum) && |
| 95 value >= kMetricBounds[type].min; |
| 96 } |
| 97 |
| 98 std::string Metric::ValueAsString() const { |
| 99 return base::DoubleToString(value); |
| 100 } |
| 101 |
31 } // namespace performance_monitor | 102 } // namespace performance_monitor |
OLD | NEW |