Index: sdk/lib/profiler/profiler.dart |
diff --git a/sdk/lib/profiler/profiler.dart b/sdk/lib/profiler/profiler.dart |
index 0bcae568dfd45b678de3e79710afdcd1fbc74b87..e30fd1dfb2e133ba65803d25df552f725bdce7ab 100644 |
--- a/sdk/lib/profiler/profiler.dart |
+++ b/sdk/lib/profiler/profiler.dart |
@@ -4,6 +4,8 @@ |
library dart.profiler; |
+import 'dart:convert'; |
+ |
/// A UserTag can be used to group samples in the Observatory profiler. |
abstract class UserTag { |
/// The maximum number of UserTag instances that can be created by a program. |
@@ -63,3 +65,135 @@ var _currentTag = _FakeUserTag._defaultTag; |
UserTag getCurrentTag() { |
return _currentTag; |
} |
+ |
+/// Abstract [Metric] class. Metric names must be unique, are hierarchical, |
+/// and use periods as separators. For example, 'a.b.c'. Uniqueness is only |
+/// enforced when a Metric is registered. The name of a metric cannot contain |
+/// the slash ('/') character. |
+abstract class Metric { |
+ /// [name] of this metric. |
+ final String name; |
+ /// [description] of this metric. |
+ final String description; |
+ |
+ Metric(this.name, this.description) { |
+ if (name.contains('/')) { |
+ throw new ArgumentError('Invalid Metric name.'); |
+ } |
+ } |
+ |
+ Map _toJSON(); |
+} |
+ |
+/// A measured value with a min and max. Initial value is min. Value will |
+/// be clamped to the interval [min, max]. |
+class Gauge extends Metric { |
+ final double min; |
+ final double max; |
+ |
+ double _value; |
+ double get value => _value; |
+ set value(double v) { |
+ if (v < min) { |
+ v = min; |
+ } else if (v > max) { |
+ v = max; |
+ } |
+ _value = v; |
+ } |
+ |
+ Gauge(String name, String description, this.min, this.max) |
+ : super(name, description) { |
+ if (min is! double) { |
+ throw new ArgumentError('min must be a double'); |
+ } |
+ if (max is! double) { |
+ throw new ArgumentError('max must be a double'); |
+ } |
+ if (!(min < max)) { |
+ throw new ArgumentError('min must be less than max'); |
+ } |
+ _value = min; |
+ } |
+ |
+ Map _toJSON() { |
+ var map = { |
+ 'type': 'Gauge', |
+ 'id': 'metrics/$name', |
+ 'name': name, |
+ 'description': description, |
+ 'value': value, |
+ 'min': min, |
+ 'max': max, |
+ }; |
+ return map; |
+ } |
+} |
+ |
+ |
+/// A changing value. Initial value is 0.0. |
+class Counter extends Metric { |
+ Counter(String name, String description) |
+ : super(name, description); |
+ |
+ double _value = 0.0; |
+ double get value => _value; |
+ set value(double v) { |
+ _value = v; |
+ } |
+ |
+ Map _toJSON() { |
+ var map = { |
+ 'type': 'Counter', |
+ 'id': 'metrics/$name', |
+ 'name': name, |
+ 'description': description, |
+ 'value': value, |
+ }; |
+ return map; |
+ } |
+} |
+ |
+class Metrics { |
+ static final Map<String, Metric> _metrics = new Map<String, Metric>(); |
+ |
+ /// Register [Metric]s to make them visible to Observatory. |
+ static void register(Metric metric) { |
+ if (metric is! Metric) { |
+ throw new ArgumentError('metric must be a Metric'); |
+ } |
+ if (_metrics[metric.name] != null) { |
+ throw new ArgumentError('Registered metrics have unique names'); |
+ } |
+ _metrics[metric.name] = metric; |
+ } |
+ |
+ /// Deregister [Metric]s to make them not visible to Observatory. |
+ static void deregister(Metric metric) { |
+ if (metric is! Metric) { |
+ throw new ArgumentError('metric must be a Metric'); |
+ } |
+ _metrics.remove(metric.name); |
+ } |
+ |
+ static String _printMetric(String id) { |
+ var metric = _metrics[id]; |
+ if (metric == null) { |
+ return null; |
+ } |
+ return JSON.encode(metric._toJSON()); |
+ } |
+ |
+ static String _printMetrics() { |
+ var members = []; |
+ for (var metric in _metrics.values) { |
+ members.add(metric._toJSON()); |
+ } |
+ var map = { |
+ 'type': 'MetricList', |
+ 'id': 'metrics', |
+ 'members': members, |
+ }; |
+ return JSON.encode(map); |
+ } |
+} |