| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library services.completion.manager; |
| 6 |
| 7 import 'package:analyzer/src/generated/source.dart'; |
| 8 |
| 9 /** |
| 10 * Overall performance of a code completion operation. |
| 11 */ |
| 12 class CompletionPerformance { |
| 13 final DateTime start = new DateTime.now(); |
| 14 final Map<String, Duration> _startTimes = new Map<String, Duration>(); |
| 15 final Stopwatch _stopwatch = new Stopwatch(); |
| 16 final List<OperationPerformance> operations = <OperationPerformance>[]; |
| 17 |
| 18 Source source; |
| 19 String snippet = ''; |
| 20 int notificationCount = -1; |
| 21 int suggestionCountFirst = -1; |
| 22 int suggestionCountLast = -1; |
| 23 Duration _firstNotification; |
| 24 |
| 25 CompletionPerformance() { |
| 26 _stopwatch.start(); |
| 27 } |
| 28 |
| 29 int get elapsedInMilliseconds => |
| 30 operations.length > 0 ? operations.last.elapsed.inMilliseconds : 0; |
| 31 |
| 32 int get firstNotificationInMilliseconds => |
| 33 _firstNotification != null ? _firstNotification.inMilliseconds : 0; |
| 34 |
| 35 String get startTimeAndMs => '${start.millisecondsSinceEpoch} - $start'; |
| 36 |
| 37 String get suggestionCount { |
| 38 if (notificationCount < 1) return ''; |
| 39 if (notificationCount == 1) return '$suggestionCountFirst'; |
| 40 return '$suggestionCountFirst, $suggestionCountLast'; |
| 41 } |
| 42 |
| 43 void complete([String tag = null]) { |
| 44 _stopwatch.stop(); |
| 45 _logDuration(tag != null ? tag : 'total time', _stopwatch.elapsed); |
| 46 } |
| 47 |
| 48 logElapseTime(String tag, [f() = null]) { |
| 49 Duration start; |
| 50 Duration end = _stopwatch.elapsed; |
| 51 var result; |
| 52 if (f == null) { |
| 53 start = _startTimes[tag]; |
| 54 if (start == null) { |
| 55 _logDuration(tag, null); |
| 56 return null; |
| 57 } |
| 58 } else { |
| 59 result = f(); |
| 60 start = end; |
| 61 end = _stopwatch.elapsed; |
| 62 } |
| 63 _logDuration(tag, end - start); |
| 64 return result; |
| 65 } |
| 66 |
| 67 void logFirstNotificationComplete(String tag) { |
| 68 _firstNotification = _stopwatch.elapsed; |
| 69 _logDuration(tag, _firstNotification); |
| 70 } |
| 71 |
| 72 void logStartTime(String tag) { |
| 73 _startTimes[tag] = _stopwatch.elapsed; |
| 74 } |
| 75 |
| 76 void setContentsAndOffset(String contents, int offset) { |
| 77 snippet = _computeSnippet(contents, offset); |
| 78 } |
| 79 |
| 80 void _logDuration(String tag, Duration elapsed) { |
| 81 operations.add(new OperationPerformance(tag, elapsed)); |
| 82 } |
| 83 |
| 84 static String _computeSnippet(String contents, int offset) { |
| 85 if (contents == null || |
| 86 offset == null || |
| 87 offset < 0 || |
| 88 contents.length < offset) { |
| 89 return '???'; |
| 90 } |
| 91 int start = offset; |
| 92 while (start > 0) { |
| 93 String ch = contents[start - 1]; |
| 94 if (ch == '\r' || ch == '\n') { |
| 95 break; |
| 96 } |
| 97 --start; |
| 98 } |
| 99 int end = offset; |
| 100 while (end < contents.length) { |
| 101 String ch = contents[end]; |
| 102 if (ch == '\r' || ch == '\n') { |
| 103 break; |
| 104 } |
| 105 ++end; |
| 106 } |
| 107 String prefix = contents.substring(start, offset); |
| 108 String suffix = contents.substring(offset, end); |
| 109 return '$prefix^$suffix'; |
| 110 } |
| 111 } |
| 112 |
| 113 /** |
| 114 * The performance of an operation when computing code completion. |
| 115 */ |
| 116 class OperationPerformance { |
| 117 /** |
| 118 * The name of the operation |
| 119 */ |
| 120 final String name; |
| 121 |
| 122 /** |
| 123 * The elapse time or `null` if undefined. |
| 124 */ |
| 125 final Duration elapsed; |
| 126 |
| 127 OperationPerformance(this.name, this.elapsed); |
| 128 } |
| OLD | NEW |