Index: runtime/lib/profiler.dart |
diff --git a/runtime/lib/profiler.dart b/runtime/lib/profiler.dart |
index 2c60141a492552be8f36be050e44dc0d772f7422..5647b891872bcd85cce7c712876fdc4f8ed16480 100644 |
--- a/runtime/lib/profiler.dart |
+++ b/runtime/lib/profiler.dart |
@@ -3,6 +3,7 @@ |
// BSD-style license that can be found in the LICENSE file. |
import "dart:_internal"; |
+import 'dart:convert'; |
patch class UserTag { |
/* patch */ factory UserTag(String label) { |
@@ -22,3 +23,130 @@ patch UserTag getCurrentTag() => _getCurrentTag(); |
UserTag _getCurrentTag() native "Profiler_getCurrentTag"; |
UserTag _getDefaultTag() native "UserTag_defaultTag"; |
+ |
+/// A measurement. |
+abstract class Metric { |
+ final String _id; |
+ final String name; |
+ final String description; |
+ double _value = 0.0; |
+ |
+ double get value => _value; |
+ |
+ Metric(this.name, this.description) |
+ : _id = '${Metrics._idCounter++}'; |
+ |
+ Map _toJSON(); |
+} |
+ |
+/// A measured value, with an optional min and max. |
+class Gauge extends Metric { |
+ final double min; |
+ final double max; |
+ |
+ Gauge(String name, String description) |
+ : min = null, |
+ max = null, |
srdjan
2014/07/29 00:30:01
Why setting it to null a second time (first at all
|
+ super(name, description); |
+ |
+ Gauge.range(String name, String description, this.min, this.max) |
+ : super(name, description) { |
+ if ((min == null) || (max == null)) { |
+ throw new ArgumentError('min and max cannot be null.'); |
srdjan
2014/07/29 00:30:01
the min/max is! double should also cover min/max =
|
+ } |
+ if ((min is! double) || (max is! double)) { |
+ throw new ArgumentError('min and max must be doubles.'); |
+ } |
+ if (min >= max) { |
+ throw new ArgumentError('min must be less than max.'); |
+ } |
+ } |
+ |
+ set value(double v) { |
+ _value = v; |
+ } |
+ |
+ Map _toJSON() { |
+ var map = { |
+ 'type': 'Gauge', |
+ 'id': 'metrics/$_id', |
+ 'name': name, |
+ 'description': description, |
+ 'value': _value, |
+ }; |
+ if (min != null) { |
+ map['min'] = min; |
+ map['max'] = max; |
+ } |
+ return map; |
+ } |
+} |
+ |
+/// A monotonically changing value. |
+class Counter extends Metric { |
+ Counter(String name, String description) |
+ : super(name, description); |
+ |
+ void add(double v) { |
+ _value += v; |
+ } |
+ |
+ Map _toJSON() { |
+ var map = { |
+ 'type': 'Counter', |
+ 'id': 'metrics/$_id', |
+ 'name': name, |
+ 'description': description, |
+ 'value': _value, |
+ }; |
+ return map; |
+ } |
+} |
+ |
+/// Manager of register metrics. |
+class Metrics { |
+ static int _idCounter = 0; |
+ static final List<Metric> _metrics = new List<Metric>(); |
+ |
+ static void add(Metric m) { |
+ if (m == null) { |
+ return; |
+ } |
+ if (_metrics.contains(m)) { |
+ return; |
+ } |
+ _metrics.add(m); |
+ } |
+ |
+ static void remove(Metric m) { |
+ _metrics.remove(m); |
+ } |
+ |
+ static bool contains(Metric m) { |
+ return _metrics.contains(m); |
+ } |
+ |
+ static String _printMetric(String id) { |
+ for (var i = 0; i < _metrics.length; i++) { |
+ var m = _metrics[i]; |
+ if (m._id == id) { |
+ return JSON.encode(m._toJSON()); |
+ } |
+ } |
+ return null; |
+ } |
+ |
+ static String _printMetrics() { |
+ var l = []; |
+ for (var i = 0; i < _metrics.length; i++) { |
+ var m = _metrics[i]; |
+ l.add(m._toJSON()); |
+ } |
+ var map = { |
+ 'type': 'MetricList', |
+ 'id': 'metrics', |
+ 'members': l, |
+ }; |
+ return JSON.encode(map); |
+ } |
+} |