未验证 提交 1228c957 编写于 作者: G Gen Lu 提交者: GitHub

Merge pull request #38799 from genlu/AddTelemetry

Add telemetry for target type completion
......@@ -21,6 +21,8 @@ internal enum ActionInfo
TypeImportCompletionItemCount,
TypeImportCompletionReferenceCount,
TypeImportCompletionTimeoutCount,
TargetTypeCompletionTicks
}
internal static void LogTypeImportCompletionTicksDataPoint(int count) =>
......@@ -35,6 +37,9 @@ internal enum ActionInfo
internal static void LogTypeImportCompletionTimeout() =>
s_logAggregator.IncreaseCount((int)ActionInfo.TypeImportCompletionTimeoutCount);
internal static void LogTargetTypeCompletionTicksDataPoint(int count) =>
s_statisticLogAggregator.AddDataPoint((int)ActionInfo.TargetTypeCompletionTicks, count);
internal static void ReportTelemetry()
{
Logger.Log(FunctionId.Intellisense_CompletionProviders_Data, KeyValueLogMessage.Create(m =>
......
......@@ -17,6 +17,8 @@ namespace Microsoft.CodeAnalysis.Completion.Providers
{
internal abstract class AbstractRecommendationServiceBasedCompletionProvider : AbstractSymbolCompletionProvider
{
protected override bool ShouldCollectTelemetryForTargetTypeCompletion => true;
protected override Task<ImmutableArray<ISymbol>> GetSymbolsWorker(SyntaxContext context, int position, OptionSet options, CancellationToken cancellationToken)
{
var recommender = context.GetLanguageService<IRecommendationService>();
......
......@@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Completion.Log;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Experiments;
using Microsoft.CodeAnalysis.Internal.Log;
......@@ -126,7 +127,8 @@ private bool IsTypeImplicitlyConvertible(Compilation compilation, ITypeSymbol so
Dictionary<ISymbol, List<ProjectId>> invalidProjectMap,
List<ProjectId> totalProjects,
bool preselect,
ImmutableArray<ITypeSymbol> inferredTypes)
Lazy<ImmutableArray<ITypeSymbol>> inferredTypes,
TelemetryCounter telemetryCounter)
{
var symbolGroups = from symbol in symbols
let texts = GetDisplayAndSuffixAndInsertionText(symbol, contextLookup(symbol))
......@@ -145,7 +147,8 @@ private bool IsTypeImplicitlyConvertible(Compilation compilation, ITypeSymbol so
if (IsTargetTypeCompletionFilterExperimentEnabled(arbitraryFirstContext.Workspace))
{
var inferredTypesWithoutNullability = inferredTypes.SelectAsArray(t => t.WithoutNullability());
var tick = Environment.TickCount;
var inferredTypesWithoutNullability = inferredTypes.Value.SelectAsArray(t => t.WithoutNullability());
foreach (var symbol in symbolGroup)
{
......@@ -156,6 +159,8 @@ private bool IsTypeImplicitlyConvertible(Compilation compilation, ITypeSymbol so
break;
}
}
telemetryCounter.AddTick(Environment.TickCount - tick);
}
itemListBuilder.Add(item);
......@@ -262,13 +267,19 @@ public override async Task ProvideCompletionsAsync(CompletionContext context)
context.IsExclusive = IsExclusive();
using (Logger.LogBlock(FunctionId.Completion_SymbolCompletionProvider_GetItemsWorker, cancellationToken))
using (var telemetryCounter = new TelemetryCounter(ShouldCollectTelemetryForTargetTypeCompletion && IsTargetTypeCompletionFilterExperimentEnabled(document.Project.Solution.Workspace)))
{
var syntaxContext = await GetOrCreateContext(document, position, cancellationToken).ConfigureAwait(false);
var inferredTypes = new Lazy<ImmutableArray<ITypeSymbol>>(() =>
{
var typeInferenceService = document.GetLanguageService<ITypeInferenceService>();
return typeInferenceService.InferTypes(syntaxContext.SemanticModel, position, cancellationToken);
});
var regularItems = await GetItemsWorkerAsync(syntaxContext, document, position, options, preselect: false, cancellationToken: cancellationToken).ConfigureAwait(false);
var regularItems = await GetItemsWorkerAsync(syntaxContext, document, position, inferredTypes, options, preselect: false, telemetryCounter, cancellationToken).ConfigureAwait(false);
context.AddItems(regularItems);
var preselectedItems = await GetItemsWorkerAsync(syntaxContext, document, position, options, preselect: true, cancellationToken: cancellationToken).ConfigureAwait(false);
var preselectedItems = await GetItemsWorkerAsync(syntaxContext, document, position, inferredTypes, options, preselect: true, telemetryCounter, cancellationToken).ConfigureAwait(false);
context.AddItems(preselectedItems);
}
}
......@@ -279,19 +290,16 @@ public override async Task ProvideCompletionsAsync(CompletionContext context)
}
private async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(
SyntaxContext context, Document document, int position, OptionSet options, bool preselect, CancellationToken cancellationToken)
SyntaxContext context, Document document, int position, Lazy<ImmutableArray<ITypeSymbol>> inferredTypes, OptionSet options, bool preselect, TelemetryCounter telemetryCounter, CancellationToken cancellationToken)
{
var relatedDocumentIds = GetRelatedDocumentIds(document, position);
options = GetUpdatedRecommendationOptions(options, document.Project.Language);
var typeInferenceService = document.GetLanguageService<ITypeInferenceService>();
var inferredTypes = typeInferenceService.InferTypes(context.SemanticModel, position, cancellationToken);
if (relatedDocumentIds.IsEmpty)
{
var itemsForCurrentDocument = await GetSymbolsWorker(position, preselect, context, options, cancellationToken).ConfigureAwait(false);
return CreateItems(itemsForCurrentDocument, _ => context, invalidProjectMap: null, totalProjects: null, preselect, inferredTypes);
return CreateItems(itemsForCurrentDocument, _ => context, invalidProjectMap: null, totalProjects: null, preselect, inferredTypes, telemetryCounter);
}
var contextAndSymbolLists = await GetPerContextSymbols(document, position, options, new[] { document.Id }.Concat(relatedDocumentIds), preselect, cancellationToken).ConfigureAwait(false);
......@@ -299,7 +307,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context)
var missingSymbolsMap = FindSymbolsMissingInLinkedContexts(symbolToContextMap, contextAndSymbolLists);
var totalProjects = contextAndSymbolLists.Select(t => t.documentId.ProjectId).ToList();
return CreateItems(symbolToContextMap.Keys, symbol => symbolToContextMap[symbol], missingSymbolsMap, totalProjects, preselect, inferredTypes);
return CreateItems(symbolToContextMap.Keys, symbol => symbolToContextMap[symbol], missingSymbolsMap, totalProjects, preselect, inferredTypes, telemetryCounter);
}
private static ImmutableArray<DocumentId> GetRelatedDocumentIds(Document document, int position)
......@@ -462,5 +470,32 @@ protected virtual string GetInsertionText(CompletionItem item, char ch)
{
return SymbolCompletionItem.GetInsertionText(item);
}
// This is used to decide which provider we'd collect target type completion telemetry from.
protected virtual bool ShouldCollectTelemetryForTargetTypeCompletion => false;
private class TelemetryCounter : IDisposable
{
private readonly bool _shouldReport;
private int _tick;
public TelemetryCounter(bool shouldReport)
{
_shouldReport = shouldReport;
}
public void AddTick(int tick)
{
_tick += tick;
}
public void Dispose()
{
if (_shouldReport)
{
CompletionProvidersLogger.LogTargetTypeCompletionTicksDataPoint(_tick);
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册