| OLD | NEW |
| (Empty) | |
| 1 library perf_api; |
| 2 |
| 3 import 'dart:async'; |
| 4 |
| 5 /** |
| 6 * A simple profiler api. |
| 7 */ |
| 8 class Profiler { |
| 9 final Counters counters = new Counters(); |
| 10 |
| 11 /** |
| 12 * Const constructor allows instances of this class to be used as a no-op |
| 13 * implementation. |
| 14 */ |
| 15 const Profiler(); |
| 16 |
| 17 /** |
| 18 * Starts a new timer for a given action [name]. A timer id will be |
| 19 * returned which can be used in [stopTimer] to stop the timer. |
| 20 * |
| 21 * [extraData] is additional information about the timed action. Implementing |
| 22 * profiler should not assume any semantic or syntactic structure of that |
| 23 * data and is free to ignore it in aggregate reports. |
| 24 */ |
| 25 dynamic startTimer(String name, [dynamic extraData]) => null; |
| 26 |
| 27 /** |
| 28 * Stop a timer for a given [idOrName]. [idOrName] can either be a timer |
| 29 * identifier returned from [startTimer] or a timer name string. If [idOrName] |
| 30 * is invalid or timer for that [idOrName] was already stopped then |
| 31 * [ProfilerError] will be thrown. If [idOrName] is a String timer name then |
| 32 * the latest active timer with that name will be stopped. |
| 33 */ |
| 34 void stopTimer(dynamic idOrName) {} |
| 35 |
| 36 /** |
| 37 * A simple zero-duration marker. |
| 38 */ |
| 39 void markTime(String name, [dynamic extraData]) {} |
| 40 |
| 41 /** |
| 42 * Times execution of the [functionOrFuture]. Body can either be a no argument |
| 43 * function or a [Future]. If function, it is executed synchronously and its |
| 44 * return value is returned. If it's a Future, then timing is stopped when the |
| 45 * future completes either successfully or with error. |
| 46 */ |
| 47 dynamic time(String name, functionOrFuture, [dynamic extraData]) { |
| 48 var id = startTimer(name, extraData); |
| 49 if (functionOrFuture is Function) { |
| 50 try { |
| 51 return functionOrFuture(); |
| 52 } finally { |
| 53 stopTimer(id); |
| 54 } |
| 55 } |
| 56 if (functionOrFuture is Future) { |
| 57 return functionOrFuture.then( |
| 58 (v) { |
| 59 stopTimer(id); |
| 60 return v; |
| 61 }, |
| 62 onError: (e) { |
| 63 stopTimer(id); |
| 64 throw e; |
| 65 }); |
| 66 } |
| 67 throw new ProfilerError( |
| 68 'Invalid functionOrFuture or type ${functionOrFuture.runtimeType}'); |
| 69 } |
| 70 } |
| 71 |
| 72 class Counters { |
| 73 |
| 74 final Map<String, int> _counters = <String, int>{}; |
| 75 |
| 76 const Counters(); |
| 77 |
| 78 /** |
| 79 * Increments the counter under [counterName] by [delta]. Default [delta] |
| 80 * is 1. If counter is not yet initilalized, its value is assumed to be 0. |
| 81 * [delta] is allowed to be negative and it is possible for the counter value |
| 82 * to become negative. |
| 83 */ |
| 84 int increment(String counterName, [int delta = 1]) { |
| 85 _counters.putIfAbsent(counterName, _initWithZero); |
| 86 _counters[counterName] += delta; |
| 87 return _counters[counterName]; |
| 88 } |
| 89 |
| 90 /** |
| 91 * Returns the current value of the counter. If the counter value is not |
| 92 * initialized then null is returned. |
| 93 */ |
| 94 int operator [](String counterName) => _counters[counterName]; |
| 95 |
| 96 /** |
| 97 * Sets a [value] for a [counterName]. Any previous value is overridden. |
| 98 */ |
| 99 operator []=(String counterName, int value) => _counters[counterName] = value; |
| 100 |
| 101 /** |
| 102 * Returns an immutable map of all known counter values. |
| 103 */ |
| 104 Map<String, int> get all => new _UnmodifiableMap(_counters); |
| 105 } |
| 106 |
| 107 int _initWithZero() => 0; |
| 108 |
| 109 class ProfilerError extends Error { |
| 110 final String message; |
| 111 ProfilerError(String this.message); |
| 112 toString() => message; |
| 113 } |
| 114 |
| 115 class _UnmodifiableMap<K, V> implements Map<K, V> { |
| 116 final Map _map; |
| 117 |
| 118 const _UnmodifiableMap(this._map); |
| 119 |
| 120 bool containsValue(Object value) => _map.containsValue(value); |
| 121 |
| 122 bool containsKey(Object key) => _map.containsKey(key); |
| 123 |
| 124 V operator [](Object key) => _map[key]; |
| 125 |
| 126 void operator []=(K key, V value) { |
| 127 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 128 } |
| 129 |
| 130 V putIfAbsent(K key, V ifAbsent()) { |
| 131 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 132 } |
| 133 |
| 134 addAll(Map other) { |
| 135 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 136 } |
| 137 |
| 138 V remove(Object key) { |
| 139 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 140 } |
| 141 |
| 142 void clear() { |
| 143 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
| 144 } |
| 145 |
| 146 void forEach(void f(K key, V value)) => _map.forEach(f); |
| 147 |
| 148 Iterable<K> get keys => _map.keys; |
| 149 |
| 150 Iterable<V> get values => _map.values; |
| 151 |
| 152 int get length => _map.length; |
| 153 |
| 154 bool get isEmpty => _map.isEmpty; |
| 155 |
| 156 bool get isNotEmpty => _map.isNotEmpty; |
| 157 } |
| OLD | NEW |