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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'dart:async';
6
7 /// This class is used to gather and print performance information.
8 class PerformanceLog {
9 final StringSink sink;
10 int _level = 0;
11
12 PerformanceLog(this.sink);
13
14 /// Enter a new execution section, which starts at one point of code, runs
15 /// some time, and then ends at the other point of code.
16 ///
17 /// The client must call [PerformanceLogSection.exit] for every [enter].
18 PerformanceLogSection enter(String msg) {
19 writeln('+++ $msg.');
20 _level++;
21 return new PerformanceLogSection(this, msg);
22 }
23
24 /// Return the result of the function [f] invocation and log the elapsed time.
25 ///
26 /// Each invocation of [run] creates a new enclosed section in the log,
27 /// which begins with printing [msg], then any log output produced during
28 /// [f] invocation, and ends with printing [msg] with the elapsed time.
29 T run<T>(String msg, T f()) {
30 Stopwatch timer = new Stopwatch()..start();
31 try {
32 writeln('+++ $msg.');
33 _level++;
34 return f();
35 } finally {
36 _level--;
37 int ms = timer.elapsedMilliseconds;
38 writeln('--- $msg in $ms ms.');
39 }
40 }
41
42 /// Return the result of the function [f] invocation and log the elapsed time.
43 ///
44 /// Each invocation of [run] creates a new enclosed section in the log,
45 /// which begins with printing [msg], then any log output produced during
46 /// [f] invocation, and ends with printing [msg] with the elapsed time.
47 Future<T> runAsync<T>(String msg, Future<T> f()) async {
48 Stopwatch timer = new Stopwatch()..start();
49 try {
50 writeln('+++ $msg.');
51 _level++;
52 return await f();
53 } finally {
54 _level--;
55 int ms = timer.elapsedMilliseconds;
56 writeln('--- $msg in $ms ms.');
57 }
58 }
59
60 /// Write a new line into the log.
61 void writeln(String msg) {
62 if (sink != null) {
63 String indent = '\t' * _level;
64 sink.writeln('$indent$msg');
65 }
66 }
67 }
68
69 /**
70 * The performance measurement section for operations that start and end
71 * at different place in code, so cannot be run using [PerformanceLog.run].
72 *
73 * The client must call [exit] for every [PerformanceLog.enter].
74 */
75 class PerformanceLogSection {
76 final PerformanceLog _logger;
77 final String _msg;
78 final Stopwatch _timer = new Stopwatch()..start();
79
80 PerformanceLogSection(this._logger, this._msg);
81
82 /**
83 * Stop the timer, log the time.
84 */
85 void exit() {
86 _timer.stop();
87 _logger._level--;
88 int ms = _timer.elapsedMilliseconds;
89 _logger.writeln('--- $_msg in $ms ms.');
90 }
91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698