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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
7 | 7 |
8 library engine.utilities.general; | 8 library engine.utilities.general; |
9 | 9 |
| 10 import 'dart:profiler'; |
| 11 |
10 /** | 12 /** |
11 * Helper for measuring how much time is spent doing some operation. | 13 * Helper class for gathering performance statistics. This class is modeled on |
| 14 * the UserTag class in dart:profiler so that it can interoperate easily with |
| 15 * it. |
12 */ | 16 */ |
13 class TimeCounter { | 17 abstract class PerformanceTag { |
14 static final int NANOS_PER_MILLI = 1000 * 1000; | 18 /** |
15 static final int NANOS_PER_MICRO = 1000; | 19 * Return the [PerformanceTag] that is initially current. This is intended |
16 static TimeCounter _current = null; | 20 * to track time when the system is performing unknown operations. |
17 final Stopwatch _sw = new Stopwatch(); | 21 */ |
| 22 static PerformanceTag get UNKNOWN => _PerformanceTagImpl.UNKNOWN; |
18 | 23 |
19 /** | 24 /** |
20 * @return the number of milliseconds spent between [start] and [stop]. | 25 * Return the current [PerformanceTag] for the isolate. |
21 */ | 26 */ |
22 int get result => _sw.elapsedMilliseconds; | 27 static PerformanceTag get current => _PerformanceTagImpl.current; |
23 | 28 |
24 /** | 29 /** |
25 * Starts counting time. | 30 * Create a [PerformanceTag] having the given [label]. A [UserTag] will also |
| 31 * be created, having the same [label], so that performance information can |
| 32 * be queried using the observatory. |
| 33 */ |
| 34 factory PerformanceTag(String label) = _PerformanceTagImpl; |
| 35 |
| 36 /** |
| 37 * Return the label for this [PerformanceTag]. |
| 38 */ |
| 39 String get label; |
| 40 |
| 41 /** |
| 42 * Return a list of all [PerformanceTag]s which have been created. |
| 43 */ |
| 44 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); |
| 45 |
| 46 /** |
| 47 * Return the total number of milliseconds that this [PerformanceTag] has |
| 48 * been the current [PerformanceTag] for the isolate. |
26 * | 49 * |
27 * @return the [TimeCounterHandle] that should be used to stop counting. | 50 * This call is safe even if this [PerformanceTag] is current. |
28 */ | 51 */ |
29 TimeCounter_TimeCounterHandle start() { | 52 int get elapsedMs; |
30 return new TimeCounter_TimeCounterHandle(this); | 53 |
| 54 /** |
| 55 * Make this the current tag for the isolate, and return the previous tag. |
| 56 */ |
| 57 PerformanceTag makeCurrent(); |
| 58 |
| 59 /** |
| 60 * Reset the total time tracked by all [PerformanceTag]s to zero. |
| 61 */ |
| 62 static void reset() { |
| 63 for (_PerformanceTagImpl tag in _PerformanceTagImpl.all) { |
| 64 tag.stopwatch.reset(); |
| 65 } |
31 } | 66 } |
32 } | 67 } |
33 | 68 |
34 /** | 69 class _PerformanceTagImpl implements PerformanceTag { |
35 * The handle object that should be used to stop and update counter. | 70 /** |
36 */ | 71 * The current performance tag for the isolate. |
37 class TimeCounter_TimeCounterHandle { | 72 */ |
38 final TimeCounter _counter; | 73 static _PerformanceTagImpl current = UNKNOWN; |
39 int _startMicros; | |
40 TimeCounter _prev; | |
41 | 74 |
42 TimeCounter_TimeCounterHandle(this._counter) { | 75 static final _PerformanceTagImpl UNKNOWN = new _PerformanceTagImpl('unknown'); |
43 // if there is some counter running, pause it | 76 |
44 _prev = TimeCounter._current; | 77 /** |
45 if (_prev != null) { | 78 * A list of all performance tags that have been created so far. |
46 _prev._sw.stop(); | 79 */ |
47 } | 80 static List<_PerformanceTagImpl> all = <_PerformanceTagImpl>[]; |
48 TimeCounter._current = _counter; | 81 |
49 // start this counter | 82 @override |
50 _startMicros = _counter._sw.elapsedMicroseconds; | 83 String get label => userTag.label; |
51 _counter._sw.start(); | 84 |
| 85 /** |
| 86 * The [UserTag] associated with this [PerformanceTag]. |
| 87 */ |
| 88 final UserTag userTag; |
| 89 |
| 90 /** |
| 91 * Stopwatch tracking the amount of time this [PerformanceTag] has been the |
| 92 * current tag for the isolate. |
| 93 */ |
| 94 final Stopwatch stopwatch; |
| 95 |
| 96 _PerformanceTagImpl(String label) |
| 97 : userTag = new UserTag(label), stopwatch = new Stopwatch() { |
| 98 all.add(this); |
52 } | 99 } |
53 | 100 |
54 /** | 101 @override |
55 * Stops counting time and updates counter. | 102 PerformanceTag makeCurrent() { |
56 */ | 103 if (identical(this, current)) { |
57 int stop() { | 104 return current; |
58 _counter._sw.stop(); | |
59 int elapsed = | |
60 (_counter._sw.elapsedMicroseconds - _startMicros) * | |
61 TimeCounter.NANOS_PER_MICRO; | |
62 // restore previous counter and resume it | |
63 TimeCounter._current = _prev; | |
64 if (_prev != null) { | |
65 _prev._sw.start(); | |
66 } | 105 } |
67 // done | 106 _PerformanceTagImpl previous = current; |
68 return elapsed; | 107 previous.stopwatch.stop(); |
| 108 stopwatch.start(); |
| 109 current = this; |
| 110 userTag.makeCurrent(); |
| 111 return previous; |
69 } | 112 } |
| 113 |
| 114 @override |
| 115 int get elapsedMs => stopwatch.elapsedMilliseconds; |
70 } | 116 } |
OLD | NEW |