From 8338ba741cf256e5549f387c41b794f3ef57c609 Mon Sep 17 00:00:00 2001 From: John Salem Date: Mon, 14 Oct 2019 09:19:49 -0700 Subject: [PATCH] [test] Profile-Provider Merging Tests (#541) --- .../Eventing/Provider.cs | 34 ++++++++++++++ .../CommandLine/Commands/CollectCommand.cs | 28 +---------- src/Tools/dotnet-trace/Profile.cs | 31 +++++++++++++ .../dotnet-trace/ProfileProviderMerging.cs | 46 +++++++++++++++++++ 4 files changed, 112 insertions(+), 27 deletions(-) create mode 100644 src/tests/dotnet-trace/ProfileProviderMerging.cs diff --git a/src/Microsoft.Diagnostics.Tools.RuntimeClient/Eventing/Provider.cs b/src/Microsoft.Diagnostics.Tools.RuntimeClient/Eventing/Provider.cs index 34ba88c43..16f3ead9c 100644 --- a/src/Microsoft.Diagnostics.Tools.RuntimeClient/Eventing/Provider.cs +++ b/src/Microsoft.Diagnostics.Tools.RuntimeClient/Eventing/Provider.cs @@ -37,5 +37,39 @@ namespace Microsoft.Diagnostics.Tools.RuntimeClient public string ToDisplayString() => String.Format("{0, -40}", Name) + String.Format("0x{0, -18}", $"{Keywords:X16}") + String.Format("{0, -8}", EventLevel.ToString() + $"({(int)EventLevel})"); + + public static bool operator ==(Provider left, Provider right) + { + return left.Name == right.Name && + left.Keywords == right.Keywords && + left.EventLevel == right.EventLevel && + left.FilterData == right.FilterData; + } + + public static bool operator !=(Provider left, Provider right) + { + return !(left == right); + } + + public override bool Equals(object obj) + { + + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + return this == (Provider)obj; + } + + public override int GetHashCode() + { + int hash = 0; + hash ^= this.Name.GetHashCode(); + hash ^= this.Keywords.GetHashCode(); + hash ^= this.EventLevel.GetHashCode(); + hash ^= this.FilterData.GetHashCode(); + return hash; + } } } diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index dc93f1a9e..fac9c13b1 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -66,7 +66,6 @@ namespace Microsoft.Diagnostics.Tools.Trace if (profile.Length != 0) { - var profileProviders = new List(); var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles .FirstOrDefault(p => p.Name.Equals(profile, StringComparison.OrdinalIgnoreCase)); if (selectedProfile == null) @@ -75,32 +74,7 @@ namespace Microsoft.Diagnostics.Tools.Trace return ErrorCodes.ArgumentError; } - // If user defined a different key/level on the same provider via --providers option that was specified via --profile option, - // --providers option takes precedence. Go through the list of providers specified and only add it if it wasn't specified - // via --providers options. - if (selectedProfile.Providers != null) - { - foreach (Provider selectedProfileProvider in selectedProfile.Providers) - { - bool shouldAdd = true; - - foreach (Provider providerCollectionProvider in providerCollection) - { - if (providerCollectionProvider.Name.Equals(selectedProfileProvider.Name)) - { - shouldAdd = false; - break; - } - } - - if (shouldAdd) - { - enabledBy[selectedProfileProvider.Name] = "--profile "; - profileProviders.Add(selectedProfileProvider); - } - } - } - providerCollection.AddRange(profileProviders); + Profile.MergeProfileAndProviders(selectedProfile, providerCollection, enabledBy); } diff --git a/src/Tools/dotnet-trace/Profile.cs b/src/Tools/dotnet-trace/Profile.cs index 4f8e2c088..7bc28c088 100644 --- a/src/Tools/dotnet-trace/Profile.cs +++ b/src/Tools/dotnet-trace/Profile.cs @@ -22,5 +22,36 @@ namespace Microsoft.Diagnostics.Tools.Trace public IEnumerable Providers { get; } public string Description { get; } + + public static void MergeProfileAndProviders(Profile selectedProfile, List providerCollection, Dictionary enabledBy) + { + var profileProviders = new List(); + // If user defined a different key/level on the same provider via --providers option that was specified via --profile option, + // --providers option takes precedence. Go through the list of providers specified and only add it if it wasn't specified + // via --providers options. + if (selectedProfile.Providers != null) + { + foreach (Provider selectedProfileProvider in selectedProfile.Providers) + { + bool shouldAdd = true; + + foreach (Provider providerCollectionProvider in providerCollection) + { + if (providerCollectionProvider.Name.Equals(selectedProfileProvider.Name)) + { + shouldAdd = false; + break; + } + } + + if (shouldAdd) + { + enabledBy[selectedProfileProvider.Name] = "--profile "; + profileProviders.Add(selectedProfileProvider); + } + } + } + providerCollection.AddRange(profileProviders); + } } } diff --git a/src/tests/dotnet-trace/ProfileProviderMerging.cs b/src/tests/dotnet-trace/ProfileProviderMerging.cs new file mode 100644 index 000000000..6cf17b120 --- /dev/null +++ b/src/tests/dotnet-trace/ProfileProviderMerging.cs @@ -0,0 +1,46 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Xunit; +using Microsoft.Diagnostics.Tools.RuntimeClient; +using System.Collections.Generic; +using System.Linq; +using System.Diagnostics.Tracing; + +namespace Microsoft.Diagnostics.Tools.Trace +{ + public class ProfileProviderMergeTests + { + [Theory] + [InlineData("cpu-sampling", "Microsoft-Windows-DotNETRuntime")] + [InlineData("gc-verbose", "Microsoft-Windows-DotNETRuntime")] + [InlineData("gc-collect", "Microsoft-Windows-DotNETRuntime")] + public void DuplicateProvider_CorrectlyOverrides(string profileName, string providerToParse) + { + Dictionary enabledBy = new Dictionary(); + + List parsedProviders = Extensions.ToProviders(providerToParse); + + foreach (var provider in parsedProviders) + { + enabledBy[provider.Name] = "--providers"; + } + + var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles + .FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase)); + Assert.NotNull(selectedProfile); + + Profile.MergeProfileAndProviders(selectedProfile, parsedProviders, enabledBy); + + var enabledProvider = parsedProviders.SingleOrDefault(p => p.Name == "Microsoft-Windows-DotNETRuntime"); + Assert.True(enabledProvider != default(Provider)); + + // Assert that our specified provider overrides the version in the profile + Assert.True(enabledProvider.Keywords == UInt64.MaxValue); + Assert.True(enabledProvider.EventLevel == EventLevel.Verbose); + Assert.True(enabledBy[enabledProvider.Name] == "--providers"); + } + } +} \ No newline at end of file -- 2.34.1