OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart.profiler; | 5 library dart.profiler; |
6 | 6 |
| 7 import 'dart:convert'; |
| 8 |
7 /// A UserTag can be used to group samples in the Observatory profiler. | 9 /// A UserTag can be used to group samples in the Observatory profiler. |
8 abstract class UserTag { | 10 abstract class UserTag { |
9 /// The maximum number of UserTag instances that can be created by a program. | 11 /// The maximum number of UserTag instances that can be created by a program. |
10 static const MAX_USER_TAGS = 64; | 12 static const MAX_USER_TAGS = 64; |
11 | 13 |
12 factory UserTag(String label) => new _FakeUserTag(label); | 14 factory UserTag(String label) => new _FakeUserTag(label); |
13 | 15 |
14 /// Label of [this]. | 16 /// Label of [this]. |
15 String get label; | 17 String get label; |
16 | 18 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 58 |
57 static final UserTag _defaultTag = new _FakeUserTag('Default'); | 59 static final UserTag _defaultTag = new _FakeUserTag('Default'); |
58 } | 60 } |
59 | 61 |
60 var _currentTag = _FakeUserTag._defaultTag; | 62 var _currentTag = _FakeUserTag._defaultTag; |
61 | 63 |
62 /// Returns the current [UserTag] for the isolate. | 64 /// Returns the current [UserTag] for the isolate. |
63 UserTag getCurrentTag() { | 65 UserTag getCurrentTag() { |
64 return _currentTag; | 66 return _currentTag; |
65 } | 67 } |
| 68 |
| 69 /// Abstract [Metric] class. Metric names must be unique, are hierarchical, |
| 70 /// and use periods as separators. For example, 'a.b.c'. Uniqueness is only |
| 71 /// enforced when a Metric is registered. The name of a metric cannot contain |
| 72 /// the slash ('/') character. |
| 73 abstract class Metric { |
| 74 /// [name] of this metric. |
| 75 final String name; |
| 76 /// [description] of this metric. |
| 77 final String description; |
| 78 |
| 79 Metric(this.name, this.description) { |
| 80 if (name.contains('/')) { |
| 81 throw new ArgumentError('Invalid Metric name.'); |
| 82 } |
| 83 } |
| 84 |
| 85 Map _toJSON(); |
| 86 } |
| 87 |
| 88 /// A measured value with a min and max. Initial value is min. Value will |
| 89 /// be clamped to the interval [min, max]. |
| 90 class Gauge extends Metric { |
| 91 final double min; |
| 92 final double max; |
| 93 |
| 94 double _value; |
| 95 double get value => _value; |
| 96 set value(double v) { |
| 97 if (v < min) { |
| 98 v = min; |
| 99 } else if (v > max) { |
| 100 v = max; |
| 101 } |
| 102 _value = v; |
| 103 } |
| 104 |
| 105 Gauge(String name, String description, this.min, this.max) |
| 106 : super(name, description) { |
| 107 if (min is! double) { |
| 108 throw new ArgumentError('min must be a double'); |
| 109 } |
| 110 if (max is! double) { |
| 111 throw new ArgumentError('max must be a double'); |
| 112 } |
| 113 if (!(min < max)) { |
| 114 throw new ArgumentError('min must be less than max'); |
| 115 } |
| 116 _value = min; |
| 117 } |
| 118 |
| 119 Map _toJSON() { |
| 120 var map = { |
| 121 'type': 'Gauge', |
| 122 'id': 'metrics/$name', |
| 123 'name': name, |
| 124 'description': description, |
| 125 'value': value, |
| 126 'min': min, |
| 127 'max': max, |
| 128 }; |
| 129 return map; |
| 130 } |
| 131 } |
| 132 |
| 133 |
| 134 /// A changing value. Initial value is 0.0. |
| 135 class Counter extends Metric { |
| 136 Counter(String name, String description) |
| 137 : super(name, description); |
| 138 |
| 139 double _value = 0.0; |
| 140 double get value => _value; |
| 141 set value(double v) { |
| 142 _value = v; |
| 143 } |
| 144 |
| 145 Map _toJSON() { |
| 146 var map = { |
| 147 'type': 'Counter', |
| 148 'id': 'metrics/$name', |
| 149 'name': name, |
| 150 'description': description, |
| 151 'value': value, |
| 152 }; |
| 153 return map; |
| 154 } |
| 155 } |
| 156 |
| 157 class Metrics { |
| 158 static final Map<String, Metric> _metrics = new Map<String, Metric>(); |
| 159 |
| 160 /// Register [Metric]s to make them visible to Observatory. |
| 161 static void register(Metric metric) { |
| 162 if (metric is! Metric) { |
| 163 throw new ArgumentError('metric must be a Metric'); |
| 164 } |
| 165 if (_metrics[metric.name] != null) { |
| 166 throw new ArgumentError('Registered metrics have unique names'); |
| 167 } |
| 168 _metrics[metric.name] = metric; |
| 169 } |
| 170 |
| 171 /// Deregister [Metric]s to make them not visible to Observatory. |
| 172 static void deregister(Metric metric) { |
| 173 if (metric is! Metric) { |
| 174 throw new ArgumentError('metric must be a Metric'); |
| 175 } |
| 176 _metrics.remove(metric.name); |
| 177 } |
| 178 |
| 179 static String _printMetric(String id) { |
| 180 var metric = _metrics[id]; |
| 181 if (metric == null) { |
| 182 return null; |
| 183 } |
| 184 return JSON.encode(metric._toJSON()); |
| 185 } |
| 186 |
| 187 static String _printMetrics() { |
| 188 var members = []; |
| 189 for (var metric in _metrics.values) { |
| 190 members.add(metric._toJSON()); |
| 191 } |
| 192 var map = { |
| 193 'type': 'MetricList', |
| 194 'id': 'metrics', |
| 195 'members': members, |
| 196 }; |
| 197 return JSON.encode(map); |
| 198 } |
| 199 } |
OLD | NEW |