| OLD | NEW |
| (Empty) | |
| 1 library perf_api.console_impl; |
| 2 |
| 3 import 'dart:html' as dom; |
| 4 import 'dart:collection'; |
| 5 import 'perf_api.dart'; |
| 6 |
| 7 /** |
| 8 * Simple window.console based implementation. |
| 9 */ |
| 10 class ConsoleProfiler extends Profiler { |
| 11 int _timerIds = 0; |
| 12 Map<int, String> _timers = new Map<int, String>(); |
| 13 Map<int, String> _timerNames = new LinkedHashMap<int, String>(); |
| 14 final dom.Window window; |
| 15 |
| 16 ConsoleProfiler() :this.window = dom.window; |
| 17 |
| 18 ConsoleProfiler.forWindow(this.window); |
| 19 |
| 20 dynamic startTimer(String name, [dynamic extraData]) { |
| 21 var timerId = _timerIds++; |
| 22 _timers[timerId] = _timerName(name, extraData); |
| 23 _timerNames[timerId] = name; |
| 24 window.console.time(_timerStr(timerId, _timers[timerId])); |
| 25 return timerId; |
| 26 } |
| 27 |
| 28 String _timerName(String name, dynamic extraData) => |
| 29 '$name${_stringifyExtraData(extraData)}'; |
| 30 |
| 31 String _stringifyExtraData(extraData) => |
| 32 (extraData == null || extraData is! String) ? '' : ' $extraData'; |
| 33 |
| 34 String _timerStr(id, name) => '${name} ($id)'; |
| 35 |
| 36 void stopTimer(dynamic idOrName) { |
| 37 int timerId; |
| 38 if (idOrName is int) { |
| 39 timerId = idOrName; |
| 40 } else { |
| 41 // TODO: change this to use a multimap. |
| 42 for (var id in _timerNames.keys) { |
| 43 if (_timerNames[id] == idOrName) { |
| 44 timerId = id; |
| 45 break; |
| 46 } |
| 47 } |
| 48 } |
| 49 if (timerId == null) { |
| 50 throw new ProfilerError('Unable for find timer for $idOrName'); |
| 51 } |
| 52 window.console.timeEnd(_timerStr(timerId, _timers[timerId])); |
| 53 _timerNames.remove(timerId); |
| 54 _timers.remove(timerId); |
| 55 } |
| 56 |
| 57 void markTime(String name, [dynamic extraData]) { |
| 58 window.console.timeStamp(_timerName(name, extraData)); |
| 59 } |
| 60 } |
| OLD | NEW |