Index: sdk/lib/developer/timeline.dart |
diff --git a/sdk/lib/developer/timeline.dart b/sdk/lib/developer/timeline.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..08451fe8e5c15b63331faf8a47103429f5b04a0e |
--- /dev/null |
+++ b/sdk/lib/developer/timeline.dart |
@@ -0,0 +1,92 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+part of dart.developer; |
+ |
+typedef dynamic TimelineSyncFunction(); |
+ |
+/// Add to the timeline. |
+class Timeline { |
+ /// Open a synchronous block of time for |category| with |name|. |
+ /// A |SyncBlock| should not be kept open across isolate messages. |
+ static SyncBlock openSync(String category, String name) { |
+ if (category is! String) { |
+ throw new ArgumentError.value(category, |
+ 'category', |
+ 'Must be a String'); |
+ } |
+ if (name is! String) { |
+ throw new ArgumentError.value(name, |
+ 'name', |
+ 'Must be a String'); |
+ } |
+ return new SyncBlock._(category, name, _getTraceClock()); |
+ } |
+ |
+ /// A utility method to time a synchronous |function|. Opens a block |
+ /// for |category| with |name|. |
+ static dynamic timeSync(String category, |
+ String name, |
+ TimelineSyncFunction function, |
+ {Map arguments}) { |
+ final block = openSync(category, name); |
+ final returnValue = function(); |
rmacnak
2015/09/29 22:36:41
try {
return function();
} finally {
if (argum
Cutch
2015/09/30 14:42:29
Done.
|
+ if (arguments is Map) { |
+ block.arguments.addAll(arguments); |
+ } |
+ block.close(); |
+ return returnValue; |
+ } |
+} |
+ |
+/// A synchronous block of time on the timeline. This block should not be |
+/// kept open across isolate messages. |
+class SyncBlock { |
+ /// The category this block belongs to. |
+ final String category; |
+ /// The name of this block. |
+ final String name; |
+ /// An (optional) set of arguments which will be serialized to JSON and |
+ /// associated with this block. |
+ final Map arguments = {}; |
+ // The start time stamp. |
+ final int _start; |
+ // Has this block been closed? |
+ bool _closed = false; |
+ |
+ SyncBlock._(this.category, |
+ this.name, |
+ this._start); |
+ |
+ /// Close this block of time. At this point, this block can no longer be |
+ /// used. |
+ void close() { |
+ if (_closed) { |
+ throw new StateError( |
+ 'It is illegal to call close twice on the same SyncBlock'); |
+ } |
+ _closed = true; |
+ var end = _getTraceClock(); |
+ |
+ // Encode arguments map as JSON before reporting. |
+ var argumentsAsJson = JSON.encode(arguments); |
+ |
+ // Report event to runtime. |
+ _reportCompleteEvent(_start, |
+ end, |
+ category, |
+ name, |
+ argumentsAsJson); |
+ } |
+} |
+ |
+/// Returns the current value from the trace clock. |
+external int _getTraceClock(); |
+ |
+/// Reports a complete synchronous event. |
+external void _reportCompleteEvent(int start, |
+ int end, |
+ String category, |
+ String name, |
+ String argumentsAsJson); |