Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Unified Diff: pkg/front_end/lib/src/base/performace_logger.dart

Issue 2883633002: Extract PerformanceLogger from AnalysisDriver. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/front_end/lib/src/base/performace_logger.dart
diff --git a/pkg/front_end/lib/src/base/performace_logger.dart b/pkg/front_end/lib/src/base/performace_logger.dart
new file mode 100644
index 0000000000000000000000000000000000000000..47af4cdb9c0d2d693d7a71e0bfa7693e7b45b629
--- /dev/null
+++ b/pkg/front_end/lib/src/base/performace_logger.dart
@@ -0,0 +1,91 @@
+// Copyright (c) 2016, 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.
+
+import 'dart:async';
+
+/// This class is used to gather and print performance information.
+class PerformanceLog {
+ final StringSink sink;
+ int _level = 0;
+
+ PerformanceLog(this.sink);
+
+ /// Enter a new execution section, which starts at one point of code, runs
+ /// some time, and then ends at the other point of code.
+ ///
+ /// The client must call [PerformanceLogSection.exit] for every [enter].
+ PerformanceLogSection enter(String msg) {
+ writeln('+++ $msg.');
+ _level++;
+ return new PerformanceLogSection(this, msg);
+ }
+
+ /// Return the result of the function [f] invocation and log the elapsed time.
+ ///
+ /// Each invocation of [run] creates a new enclosed section in the log,
+ /// which begins with printing [msg], then any log output produced during
+ /// [f] invocation, and ends with printing [msg] with the elapsed time.
+ T run<T>(String msg, T f()) {
+ Stopwatch timer = new Stopwatch()..start();
+ try {
+ writeln('+++ $msg.');
+ _level++;
+ return f();
+ } finally {
+ _level--;
+ int ms = timer.elapsedMilliseconds;
+ writeln('--- $msg in $ms ms.');
+ }
+ }
+
+ /// Return the result of the function [f] invocation and log the elapsed time.
+ ///
+ /// Each invocation of [run] creates a new enclosed section in the log,
+ /// which begins with printing [msg], then any log output produced during
+ /// [f] invocation, and ends with printing [msg] with the elapsed time.
+ Future<T> runAsync<T>(String msg, Future<T> f()) async {
+ Stopwatch timer = new Stopwatch()..start();
+ try {
+ writeln('+++ $msg.');
+ _level++;
+ return await f();
+ } finally {
+ _level--;
+ int ms = timer.elapsedMilliseconds;
+ writeln('--- $msg in $ms ms.');
+ }
+ }
+
+ /// Write a new line into the log.
+ void writeln(String msg) {
+ if (sink != null) {
+ String indent = '\t' * _level;
+ sink.writeln('$indent$msg');
+ }
+ }
+}
+
+/**
+ * The performance measurement section for operations that start and end
+ * at different place in code, so cannot be run using [PerformanceLog.run].
+ *
+ * The client must call [exit] for every [PerformanceLog.enter].
+ */
+class PerformanceLogSection {
+ final PerformanceLog _logger;
+ final String _msg;
+ final Stopwatch _timer = new Stopwatch()..start();
+
+ PerformanceLogSection(this._logger, this._msg);
+
+ /**
+ * Stop the timer, log the time.
+ */
+ void exit() {
+ _timer.stop();
+ _logger._level--;
+ int ms = _timer.elapsedMilliseconds;
+ _logger.writeln('--- $_msg in $ms ms.');
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698