Skip to content

Commit a46b4bb

Browse files
keertipCommit Queue
authored andcommitted
Collect analytics on type of workpace and number of packages for contexts.
Change-Id: I2274536383f92f21912bd39dc5a6796dd2177fd5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/456800 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 6482495 commit a46b4bb

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import 'package:analyzer/src/generated/sdk.dart';
8080
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
8181
import 'package:analyzer/src/util/performance/operation_performance.dart';
8282
import 'package:analyzer/src/utilities/extensions/analysis_session.dart';
83+
import 'package:analyzer/src/workspace/pub.dart';
8384
import 'package:analyzer_plugin/protocol/protocol.dart';
8485
import 'package:analyzer_plugin/src/protocol/protocol_internal.dart'
8586
as analyzer_plugin;
@@ -938,13 +939,29 @@ abstract class AnalysisServer {
938939
var transitiveFilePaths = <String>{};
939940
var transitiveFileUniqueLineCount = 0;
940941
var libraryCycles = <LibraryCycle>{};
942+
var workspaceTypes = <int>[0, 0, 0];
943+
var numberOfPackages = <int>[];
941944
var driverMap = contextManager.driverMap;
942945
for (var entry in driverMap.entries) {
943946
var rootPath = entry.key.path;
944947
var driver = entry.value;
945-
var contextRoot = driver.analysisContext?.contextRoot;
946-
if (contextRoot != null) {
948+
var analysisContext = driver.analysisContext;
949+
if (analysisContext != null) {
950+
var contextRoot = analysisContext.contextRoot;
947951
packagesFileMap[rootPath] = contextRoot.packagesFile;
952+
var workspace = contextRoot.workspace;
953+
if (workspace is PackageConfigWorkspace) {
954+
if (workspace.isPubWorkspace) {
955+
// Pub workspace index = 2, Package workspace index = 1
956+
workspaceTypes[2]++;
957+
numberOfPackages.add(workspace.allPackages.length);
958+
} else {
959+
workspaceTypes[1]++;
960+
}
961+
} else {
962+
// Blaze, GN or other workspace index = 0
963+
workspaceTypes[0]++;
964+
}
948965
}
949966
var fileSystemState = driver.fsState;
950967
// Capture the known files before the loop to prevent a concurrent
@@ -995,6 +1012,8 @@ abstract class AnalysisServer {
9951012
transitiveFileUniqueLineCount: transitiveFileUniqueLineCount,
9961013
libraryCycleLibraryCounts: libraryCycleLibraryCounts,
9971014
libraryCycleLineCounts: libraryCycleLineCounts,
1015+
contextWorkspaceType: workspaceTypes,
1016+
numberOfPackagesInWorkspace: numberOfPackages,
9981017
);
9991018
}
10001019

pkg/analysis_server/lib/src/analytics/analytics_manager.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class AnalyticsManager {
119119
required int transitiveFileUniqueLineCount,
120120
required List<int> libraryCycleLibraryCounts,
121121
required List<int> libraryCycleLineCounts,
122+
required List<int> numberOfPackagesInWorkspace,
123+
required List<int> contextWorkspaceType,
122124
}) {
123125
// This is currently keeping the first report of completed analysis, but we
124126
// might want to consider alternatives, such as keeping the "largest"
@@ -133,6 +135,8 @@ class AnalyticsManager {
133135
transitiveFileUniqueLineCount: transitiveFileUniqueLineCount,
134136
libraryCycleLibraryCounts: libraryCycleLibraryCounts,
135137
libraryCycleLineCounts: libraryCycleLineCounts,
138+
contextWorkspaceType: contextWorkspaceType,
139+
numberOfPackagesInWorkspace: numberOfPackagesInWorkspace,
136140
);
137141
}
138142

@@ -474,6 +478,12 @@ class AnalyticsManager {
474478
h3('Analysis data');
475479
buffer.writeln('<ul>');
476480
li('numberOfContexts: ${json.encode(analysisData.numberOfContexts)}');
481+
li(
482+
'contextWorkspaceType: ${json.encode(analysisData.contextWorkspaceType.toString())}',
483+
);
484+
li(
485+
'numberOfPackagesPerWorkspace: ${json.encode(analysisData.numberOfPackagesInWorkspace.toString())}',
486+
);
477487
li('immediateFileCount: ${json.encode(analysisData.immediateFileCount)}');
478488
li(
479489
'immediateFileLineCount: ${json.encode(analysisData.immediateFileLineCount)}',

pkg/analysis_server/lib/src/analytics/context_structure.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class ContextStructure {
4747
/// [transitiveFileUniqueCount].
4848
final int transitiveFileUniqueLineCount;
4949

50+
/// The type of the workspaces in all of the contexts. A list of 3 elements,
51+
/// index 0 - number of Blaze/GN/Basic workspace
52+
/// index 1 - number of workspaces with one package
53+
/// index 2 - number of Pub workspaces
54+
final List<int> contextWorkspaceType;
55+
56+
/// The number of packages in the workspace. This value is calculated only
57+
/// for Pub workspaces (monorepo).
58+
final PercentileCalculator numberOfPackagesInWorkspace;
59+
5060
final PercentileCalculator libraryCycleLibraryCounts;
5161
final PercentileCalculator libraryCycleLineCounts;
5262

@@ -61,10 +71,15 @@ class ContextStructure {
6171
required this.transitiveFileUniqueLineCount,
6272
required List<int> libraryCycleLibraryCounts,
6373
required List<int> libraryCycleLineCounts,
74+
this.contextWorkspaceType = const <int>[0, 0, 0],
75+
List<int> numberOfPackagesInWorkspace = const <int>[],
6476
}) : libraryCycleLibraryCounts = PercentileCalculator.from(
6577
libraryCycleLibraryCounts,
6678
),
6779
libraryCycleLineCounts = PercentileCalculator.from(
6880
libraryCycleLineCounts,
81+
),
82+
numberOfPackagesInWorkspace = PercentileCalculator.from(
83+
numberOfPackagesInWorkspace,
6984
);
7085
}

pkg/analyzer/lib/src/workspace/pub.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ class PackageConfigWorkspace extends SimpleWorkspace {
180180
/// The contents of the package config file.
181181
late final String? _packageConfigContent;
182182

183+
/// Indicates whether this is a PubWorkspace as per
184+
/// https://dart.dev/tools/pub/workspaces
185+
bool _isPubWorkspace = false;
186+
183187
final Map<String, WorkspacePackageImpl> _workspacePackages = {};
184188

185189
factory PackageConfigWorkspace(
@@ -208,6 +212,15 @@ class PackageConfigWorkspace extends SimpleWorkspace {
208212
this.packageConfigFile,
209213
) {
210214
_packageConfigContent = packageConfigFile.readAsStringSync();
215+
var pubspecFile = provider
216+
.getFolder(root)
217+
.getChildAssumingFile(file_paths.pubspecYaml);
218+
if (pubspecFile.exists) {
219+
var pubspec = Pubspec.parse(pubspecFile.readAsStringSync());
220+
if (pubspec.workspace != null) {
221+
_isPubWorkspace = true;
222+
}
223+
}
211224
}
212225

213226
Iterable<WorkspacePackageImpl> get allPackages =>
@@ -218,6 +231,8 @@ class PackageConfigWorkspace extends SimpleWorkspace {
218231
return _fileContentOrNull(packageConfigFile) == _packageConfigContent;
219232
}
220233

234+
bool get isPubWorkspace => _isPubWorkspace;
235+
221236
@override
222237
UriResolver get packageUriResolver {
223238
return PackageConfigPackageUriResolver(

pkg/analyzer/test/src/workspace/pub_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,44 @@ class PackageConfigWorkspaceTest with ResourceProviderMixin {
549549
);
550550
}
551551

552+
void test_multiple_packages_pub_workspace() {
553+
var aFilePath = '/workspace/a/lib/a.dart';
554+
var bFilePath = '/workspace/b/b.dart';
555+
_addResources(['/workspace/a/lib/', '/workspace/b/', aFilePath, bFilePath]);
556+
newPubspecYamlFile('/workspace', '''
557+
name: works
558+
workspace:
559+
- a
560+
- b
561+
''');
562+
newPubspecYamlFile('/workspace/a', 'name: a');
563+
PackageConfigWorkspace workspace = _createWorkspace('/workspace', [
564+
'project',
565+
'foo',
566+
]);
567+
568+
expect(workspace.root, convertPath('/workspace'));
569+
workspace.findPackageFor(convertPath(aFilePath));
570+
workspace.findPackageFor(convertPath(bFilePath));
571+
expect(workspace.allPackages.length, 2);
572+
expect(workspace.isPubWorkspace, true);
573+
}
574+
575+
void test_single_package_workspace() {
576+
var aFilePath = '/workspace/a/lib/a.dart';
577+
_addResources(['/workspace/a/lib/', aFilePath]);
578+
newPubspecYamlFile('/workspace/a', 'name: a');
579+
PackageConfigWorkspace workspace = _createWorkspace('/workspace', [
580+
'project',
581+
'foo',
582+
]);
583+
584+
expect(workspace.root, convertPath('/workspace'));
585+
workspace.findPackageFor(convertPath(aFilePath));
586+
expect(workspace.allPackages.length, 1);
587+
expect(workspace.isPubWorkspace, false);
588+
}
589+
552590
void _addResources(List<String> paths) {
553591
for (String path in paths) {
554592
if (path.endsWith('/')) {

0 commit comments

Comments
 (0)