|
| 1 | +// Copyright (c) 2024, 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 'package:analysis_server/src/services/completion/dart/completion_manager.dart'; |
| 6 | +import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart'; |
| 7 | +import 'package:analyzer/dart/analysis/results.dart'; |
| 8 | +import 'package:analyzer/file_system/overlay_file_system.dart'; |
| 9 | +import 'package:analyzer/file_system/physical_file_system.dart'; |
| 10 | +import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart'; |
| 11 | +import 'package:analyzer/src/util/performance/operation_performance.dart'; |
| 12 | + |
| 13 | +import 'sliding_statistics.dart'; |
| 14 | + |
| 15 | +/// A tool to see the current performance of code completion at an interesting |
| 16 | +/// location. To see details, run it with `--observe:5000` and then use |
| 17 | +/// DevTools CPU profiler. |
| 18 | +/// |
| 19 | +/// Currently this is not a tool to track performance, it does not record it |
| 20 | +/// anywhere. |
| 21 | +/// |
| 22 | +/// Update the marked code as necessary to construct a case for checking |
| 23 | +/// performance. The code below is just one example that we saw to have |
| 24 | +/// significant cost. |
| 25 | +/// |
| 26 | +/// Don't forget to update [flutterPackagePath]. |
| 27 | +Future<void> main() async { |
| 28 | + await _runForever( |
| 29 | + path: '$flutterPackagePath/lib/test.dart', |
| 30 | + markedCode: r''' |
| 31 | +import 'package:flutter/widgets.dart'; |
| 32 | +Widget x = ^; |
| 33 | +''', |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +const String flutterEnvironmentPath = |
| 38 | + '/Users/scheglov/dart/flutter_elements/environment'; |
| 39 | + |
| 40 | +/// This should be the path of the `package:flutter` in a local checkout. |
| 41 | +/// You don't have to use [flutterEnvironmentPath] from above. |
| 42 | +const String flutterPackagePath = '$flutterEnvironmentPath/packages/flutter'; |
| 43 | + |
| 44 | +Future<void> _runForever({ |
| 45 | + required String path, |
| 46 | + required String markedCode, |
| 47 | +}) async { |
| 48 | + final offset = markedCode.indexOf('^'); |
| 49 | + if (offset == -1) { |
| 50 | + throw ArgumentError('No ^ marker'); |
| 51 | + } |
| 52 | + |
| 53 | + final rawCode = |
| 54 | + markedCode.substring(0, offset) + markedCode.substring(offset + 1); |
| 55 | + if (rawCode.contains('^')) { |
| 56 | + throw ArgumentError('Duplicate ^ marker'); |
| 57 | + } |
| 58 | + |
| 59 | + final resourceProvider = OverlayResourceProvider( |
| 60 | + PhysicalResourceProvider.INSTANCE, |
| 61 | + ); |
| 62 | + |
| 63 | + resourceProvider.setOverlay( |
| 64 | + path, |
| 65 | + content: rawCode, |
| 66 | + modificationStamp: -1, |
| 67 | + ); |
| 68 | + |
| 69 | + var collection = AnalysisContextCollectionImpl( |
| 70 | + resourceProvider: resourceProvider, |
| 71 | + includedPaths: [path], |
| 72 | + sdkPath: '/Users/scheglov/Applications/dart-sdk', |
| 73 | + ); |
| 74 | + var analysisContext = collection.contextFor(path); |
| 75 | + var analysisSession = analysisContext.currentSession; |
| 76 | + var unitResult = await analysisSession.getResolvedUnit(path); |
| 77 | + unitResult as ResolvedUnitResult; |
| 78 | + |
| 79 | + var dartRequest = DartCompletionRequest.forResolvedUnit( |
| 80 | + resolvedUnit: unitResult, |
| 81 | + offset: offset, |
| 82 | + ); |
| 83 | + |
| 84 | + final statistics = SlidingStatistics(100); |
| 85 | + while (true) { |
| 86 | + var timer = Stopwatch()..start(); |
| 87 | + var budget = CompletionBudget(Duration(seconds: 30)); |
| 88 | + List<CompletionSuggestionBuilder> suggestions = []; |
| 89 | + for (var i = 0; i < 10; i++) { |
| 90 | + suggestions = await DartCompletionManager( |
| 91 | + budget: budget, |
| 92 | + notImportedSuggestions: NotImportedSuggestions(), |
| 93 | + ).computeSuggestions( |
| 94 | + dartRequest, |
| 95 | + OperationPerformanceImpl('<root>'), |
| 96 | + useFilter: false, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + final responseTime = timer.elapsedMilliseconds; |
| 101 | + statistics.add(responseTime); |
| 102 | + if (statistics.isReady) { |
| 103 | + print( |
| 104 | + '[${DateTime.now().millisecondsSinceEpoch}]' |
| 105 | + '[time: $responseTime ms][mean: ${statistics.mean.toStringAsFixed(1)}]' |
| 106 | + '[stdDev: ${statistics.standardDeviation.toStringAsFixed(3)}]' |
| 107 | + '[min: ${statistics.min.toStringAsFixed(1)}]' |
| 108 | + '[max: ${statistics.max.toStringAsFixed(1)}]', |
| 109 | + ); |
| 110 | + } else { |
| 111 | + print('[time: $responseTime ms][suggestions: ${suggestions.length}]'); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments