Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored resolvers, added string caching and xml toggling to improve memory usage #377

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// // Copyright (c) Microsoft Corporation.
// // Licensed under the MIT License.

using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Reflection;
using EventLogExpert.Eventing.EventResolvers;
using EventLogExpert.Eventing.Models;
using EventLogExpert.Eventing.Providers;
using NSubstitute;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Reflection;
using Xunit.Abstractions;

namespace EventLogExpert.Eventing.Tests.EventResolvers;
Expand All @@ -16,17 +16,20 @@ public sealed class EventResolverTests(ITestOutputHelper outputHelper)
{
internal class UnitTestEventResolver : EventResolverBase, IEventResolver
{
public string Status { get; private set; } = string.Empty;
internal UnitTestEventResolver(List<ProviderDetails> providerDetailsList) :
base((s, log) => Debug.WriteLine(s)) =>
providerDetailsList.ForEach(p => providerDetails.TryAdd(p.ProviderName, p));

public event EventHandler<string>? StatusChanged;

private readonly List<ProviderDetails> _providerDetailsList;

internal UnitTestEventResolver(List<ProviderDetails> providerDetailsList) : base((s, log) => Debug.WriteLine(s)) => _providerDetailsList = providerDetailsList;

public DisplayEventModel Resolve(EventRecord eventRecord, string owningLog) => ResolveFromProviderDetails(eventRecord, eventRecord.Properties, _providerDetailsList[0], owningLog);
public void ResolveProviderDetails(EventRecord eventRecord, string owningLogName)
{
if (providerDetails.ContainsKey(eventRecord.ProviderName))
{
return;
}

public void Dispose() { }
var details = new EventMessageProvider(eventRecord.ProviderName, tracer).LoadProviderDetails();
providerDetails.TryAdd(eventRecord.ProviderName, details);
}
}

private readonly ITestOutputHelper _outputHelper = outputHelper;
Expand Down Expand Up @@ -94,12 +97,13 @@ public void CanResolveMSExchangeRepl4114()
};

var resolver = new UnitTestEventResolver([providerDetails]);
var result = resolver.Resolve(eventRecord, "Test");
var description = resolver.ResolveDescription(eventRecord);
var taskName = resolver.ResolveTaskName(eventRecord);

var expectedDescription = "Database redundancy health check passed.\r\nDatabase copy: SERVER1\r\nRedundancy count: 4\r\nIsSuppressed: False\r\n\r\nErrors:\r\nLots of copy status text";

Assert.Equal(expectedDescription, result.Description);
Assert.Equal("Service", result.TaskCategory);
Assert.Equal(expectedDescription, description);
Assert.Equal("Service", taskName);
}

[Fact]
Expand All @@ -113,7 +117,7 @@ public void PerfTest()

while (null != (er = eventLogReader.ReadEvent()))
{
resolver.Resolve(er, "Test");
resolver.ResolveProviderDetails(er, "Test");
}

sw.Stop();
Expand Down Expand Up @@ -143,7 +147,7 @@ public void PerfTest2()

foreach (var record in eventRecords)
{
resolver.Resolve(record, "Test");
resolver.ResolveProviderDetails(record, "Test");
}

sw.Stop();
Expand All @@ -157,7 +161,6 @@ public void Test1()

var resolvers = new List<IEventResolver>
{
new EventReaderEventResolver(),
new LocalProviderEventResolver(),
/* new EventProviderDatabaseEventResolver(
s => {
Expand All @@ -167,75 +170,58 @@ public void Test1()
}) */
};

EventRecord er;
HashSet<string> uniqueDescriptions = [];
HashSet<string> uniqueXml = [];
HashSet<string> uniqueKeywords = [];

var totalCount = 0;
var mismatchCount = 0;
var xmlMismatchCount = 0;
var keywordsMismatchCount = 0;
var mismatches = new List<List<string>>();
var xmlMismatches = new List<List<string>>();
var keywordMismatches = new List<List<string>>();

while (null != (er = eventLogReader.ReadEvent()))
while (eventLogReader.ReadEvent() is { } er)
{
uniqueDescriptions.Clear();
uniqueXml.Clear();
uniqueKeywords.Clear();

foreach (var r in resolvers)
{
var resolved = r.Resolve(er, "Test");
r.ResolveProviderDetails(er, "Test");

var description = r.ResolveDescription(er);
var keywords = r.GetKeywordsFromBitmask(er);

uniqueDescriptions.Add(resolved.Description
uniqueDescriptions.Add(description
.Replace("\r", "") // I can't figure out the logic of FormatMessage() for when it leaves
.Replace("\n", "") // CRLFs and spaces in or takes them out, so I'm just giving up for now.
.Replace(" ", "") // If we're this close to matching FormatMessage() then we're close enough.
.Replace("\u200E", "") // Remove LRM marks from dates.
.Trim());

uniqueXml.Add(resolved.Xml);

if (r is EventReaderEventResolver && resolved.KeywordsDisplayNames.Count() < 1)
if (!keywords.Any())
{
// Don't bother adding it. EventReader fails to resolve a lot of keywords for some reason.
}
else
{
uniqueKeywords.Add(string.Join(" ", resolved.KeywordsDisplayNames.OrderBy(n => n)));
uniqueKeywords.Add(string.Join(" ", keywords.OrderBy(n => n)));
}
}

if (uniqueDescriptions.Count > 1)
{
mismatchCount++;
mismatches.Add(uniqueDescriptions.ToList());
}

if (uniqueXml.Count > 1)
{
xmlMismatchCount++;
xmlMismatches.Add(uniqueXml.ToList());
}

if (uniqueKeywords.Count > 1)
{
keywordsMismatchCount++;
keywordMismatches.Add(uniqueKeywords.ToList());
}

totalCount++;
}

foreach (var resolver in resolvers)
{
resolver?.Dispose();
}
var totalMismatchCount = mismatchCount + keywordsMismatchCount;

var mismatchPercent = mismatchCount / totalCount * 100;
var mismatchPercent = totalMismatchCount > 0 && totalCount > 0 ? totalMismatchCount / totalCount * 100 : 0;

Assert.True(mismatchPercent < 1);
}
Expand Down
Loading