-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 1b5368f into dev
- Loading branch information
Showing
29 changed files
with
829 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
pkg/analysis_server/tool/code_completion/benchmark/flutter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) 2024, 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 'package:analysis_server/src/services/completion/dart/completion_manager.dart'; | ||
import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart'; | ||
import 'package:analyzer/dart/analysis/results.dart'; | ||
import 'package:analyzer/file_system/overlay_file_system.dart'; | ||
import 'package:analyzer/file_system/physical_file_system.dart'; | ||
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart'; | ||
import 'package:analyzer/src/util/performance/operation_performance.dart'; | ||
|
||
import 'sliding_statistics.dart'; | ||
|
||
/// A tool to see the current performance of code completion at an interesting | ||
/// location. To see details, run it with `--observe:5000` and then use | ||
/// DevTools CPU profiler. | ||
/// | ||
/// Currently this is not a tool to track performance, it does not record it | ||
/// anywhere. | ||
/// | ||
/// Update the marked code as necessary to construct a case for checking | ||
/// performance. The code below is just one example that we saw to have | ||
/// significant cost. | ||
/// | ||
/// Don't forget to update [flutterPackagePath]. | ||
Future<void> main() async { | ||
await _runForever( | ||
path: '$flutterPackagePath/lib/test.dart', | ||
markedCode: r''' | ||
import 'package:flutter/widgets.dart'; | ||
Widget x = ^; | ||
''', | ||
); | ||
} | ||
|
||
const String flutterEnvironmentPath = | ||
'/Users/scheglov/dart/flutter_elements/environment'; | ||
|
||
/// This should be the path of the `package:flutter` in a local checkout. | ||
/// You don't have to use [flutterEnvironmentPath] from above. | ||
const String flutterPackagePath = '$flutterEnvironmentPath/packages/flutter'; | ||
|
||
Future<void> _runForever({ | ||
required String path, | ||
required String markedCode, | ||
}) async { | ||
final offset = markedCode.indexOf('^'); | ||
if (offset == -1) { | ||
throw ArgumentError('No ^ marker'); | ||
} | ||
|
||
final rawCode = | ||
markedCode.substring(0, offset) + markedCode.substring(offset + 1); | ||
if (rawCode.contains('^')) { | ||
throw ArgumentError('Duplicate ^ marker'); | ||
} | ||
|
||
final resourceProvider = OverlayResourceProvider( | ||
PhysicalResourceProvider.INSTANCE, | ||
); | ||
|
||
resourceProvider.setOverlay( | ||
path, | ||
content: rawCode, | ||
modificationStamp: -1, | ||
); | ||
|
||
var collection = AnalysisContextCollectionImpl( | ||
resourceProvider: resourceProvider, | ||
includedPaths: [path], | ||
sdkPath: '/Users/scheglov/Applications/dart-sdk', | ||
); | ||
var analysisContext = collection.contextFor(path); | ||
var analysisSession = analysisContext.currentSession; | ||
var unitResult = await analysisSession.getResolvedUnit(path); | ||
unitResult as ResolvedUnitResult; | ||
|
||
var dartRequest = DartCompletionRequest.forResolvedUnit( | ||
resolvedUnit: unitResult, | ||
offset: offset, | ||
); | ||
|
||
final statistics = SlidingStatistics(100); | ||
while (true) { | ||
var timer = Stopwatch()..start(); | ||
var budget = CompletionBudget(Duration(seconds: 30)); | ||
List<CompletionSuggestionBuilder> suggestions = []; | ||
for (var i = 0; i < 10; i++) { | ||
suggestions = await DartCompletionManager( | ||
budget: budget, | ||
notImportedSuggestions: NotImportedSuggestions(), | ||
).computeSuggestions( | ||
dartRequest, | ||
OperationPerformanceImpl('<root>'), | ||
useFilter: false, | ||
); | ||
} | ||
|
||
final responseTime = timer.elapsedMilliseconds; | ||
statistics.add(responseTime); | ||
if (statistics.isReady) { | ||
print( | ||
'[${DateTime.now().millisecondsSinceEpoch}]' | ||
'[time: $responseTime ms][mean: ${statistics.mean.toStringAsFixed(1)}]' | ||
'[stdDev: ${statistics.standardDeviation.toStringAsFixed(3)}]' | ||
'[min: ${statistics.min.toStringAsFixed(1)}]' | ||
'[max: ${statistics.max.toStringAsFixed(1)}]', | ||
); | ||
} else { | ||
print('[time: $responseTime ms][suggestions: ${suggestions.length}]'); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
pkg/analysis_server/tool/code_completion/benchmark/sliding_statistics.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2024, 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:math'; | ||
import 'dart:typed_data'; | ||
|
||
class SlidingStatistics { | ||
final Uint32List _values; | ||
int _index = 0; | ||
bool _isReady = false; | ||
|
||
SlidingStatistics(int length) : _values = Uint32List(length); | ||
|
||
bool get isReady => _isReady; | ||
|
||
int get max { | ||
var result = 0; | ||
for (final value in _values) { | ||
if (value > result) { | ||
result = value; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
double get mean { | ||
assert(isReady); | ||
var sum = 0.0; | ||
for (final value in _values) { | ||
sum += value; | ||
} | ||
return sum / _values.length; | ||
} | ||
|
||
int get min { | ||
var result = 1 << 20; | ||
for (final value in _values) { | ||
if (value < result) { | ||
result = value; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
double get standardDeviation { | ||
assert(isReady); | ||
final mean = this.mean; | ||
var sum = 0.0; | ||
for (final value in _values) { | ||
final diff = value - mean; | ||
sum += diff * diff; | ||
} | ||
return sqrt(sum / _values.length); | ||
} | ||
|
||
void add(int value) { | ||
_values[_index] = value; | ||
_index++; | ||
if (_index == _values.length) { | ||
_isReady = true; | ||
} | ||
_index %= _values.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.