OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library src.domain_diagnostic; | 5 library src.domain_diagnostic; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:core' hide Resource; | 9 import 'dart:core' hide Resource; |
10 | 10 |
11 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 11 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
12 import 'package:analysis_server/src/analysis_server.dart'; | 12 import 'package:analysis_server/src/analysis_server.dart'; |
| 13 import 'package:analysis_server/src/utilities/average.dart'; |
13 import 'package:analyzer/file_system/file_system.dart'; | 14 import 'package:analyzer/file_system/file_system.dart'; |
14 import 'package:analyzer/src/context/cache.dart'; | 15 import 'package:analyzer/src/context/cache.dart'; |
15 import 'package:analyzer/src/context/context.dart'; | 16 import 'package:analyzer/src/context/context.dart'; |
16 import 'package:analyzer/src/generated/engine.dart' | 17 import 'package:analyzer/src/generated/engine.dart' |
17 hide AnalysisCache, AnalysisContextImpl; | 18 hide AnalysisCache, AnalysisContextImpl; |
18 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
19 import 'package:analyzer/src/generated/utilities_collection.dart'; | 20 import 'package:analyzer/src/generated/utilities_collection.dart'; |
20 import 'package:analyzer/src/task/driver.dart'; | 21 import 'package:analyzer/src/task/driver.dart'; |
21 import 'package:analyzer/task/model.dart'; | 22 import 'package:analyzer/task/model.dart'; |
22 | 23 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 static const int maxSampleCount = 30; | 122 static const int maxSampleCount = 30; |
122 | 123 |
123 /// Current sample count. | 124 /// Current sample count. |
124 int sampleCount = 0; | 125 int sampleCount = 0; |
125 | 126 |
126 /// The shared timer. | 127 /// The shared timer. |
127 Timer timer; | 128 Timer timer; |
128 | 129 |
129 /// Map of contexts (tracked as folders to avoid leaks) to averages. | 130 /// Map of contexts (tracked as folders to avoid leaks) to averages. |
130 /// TOOD(pq): consider adding GC to remove mappings for deleted folders | 131 /// TOOD(pq): consider adding GC to remove mappings for deleted folders |
131 Map<Folder, _Average> averages = new HashMap<Folder, _Average>(); | 132 Map<Folder, Average> averages = new HashMap<Folder, Average>(); |
132 | 133 |
133 final AnalysisServer server; | 134 final AnalysisServer server; |
134 Sampler(this.server) { | 135 Sampler(this.server) { |
135 start(); | 136 start(); |
136 _sample(); | 137 _sample(); |
137 } | 138 } |
138 | 139 |
139 /// Get the average for the context associated with the given [folder]. | 140 /// Get the average for the context associated with the given [folder]. |
140 int getAverage(Folder folder) { | 141 int getAverage(Folder folder) { |
141 resetTimerCountdown(); | 142 resetTimerCountdown(); |
(...skipping 25 matching lines...) Expand all Loading... |
167 /// Stop sampling. | 168 /// Stop sampling. |
168 void stop() { | 169 void stop() { |
169 timer.cancel(); | 170 timer.cancel(); |
170 } | 171 } |
171 | 172 |
172 /// Take a sample. | 173 /// Take a sample. |
173 void _sample() { | 174 void _sample() { |
174 try { | 175 try { |
175 server.folderMap.forEach((Folder folder, AnalysisContext context) { | 176 server.folderMap.forEach((Folder folder, AnalysisContext context) { |
176 if (context is AnalysisContextImpl) { | 177 if (context is AnalysisContextImpl) { |
177 _Average average = averages[folder]; | 178 Average average = averages[folder]; |
178 if (average == null) { | 179 if (average == null) { |
179 average = new _Average(); | 180 average = new Average(); |
180 averages[folder] = average; | 181 averages[folder] = average; |
181 } | 182 } |
182 average.addSample(_workItemCount(context)); | 183 average.addSample(_workItemCount(context)); |
183 } | 184 } |
184 }); | 185 }); |
185 } on Exception { | 186 } on Exception { |
186 stop(); | 187 stop(); |
187 } | 188 } |
188 } | 189 } |
189 } | 190 } |
190 | |
191 /// Simple rolling average sample counter. | |
192 class _Average { | |
193 num _val; | |
194 | |
195 final int sampleCount; | |
196 _Average([this.sampleCount = 20]); | |
197 | |
198 num get value => _val ?? 0; | |
199 | |
200 void addSample(num sample) { | |
201 if (_val == null) { | |
202 _val = sample; | |
203 } else { | |
204 _val = | |
205 _val * ((sampleCount - 1) / sampleCount) + sample * (1 / sampleCount); | |
206 } | |
207 } | |
208 | |
209 @override | |
210 String toString() => 'average: ${value}'; | |
211 } | |
OLD | NEW |