Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jan 24, 2024
1 parent 7c1a025 commit 8e0f0f5
Showing 1 changed file with 174 additions and 3 deletions.
177 changes: 174 additions & 3 deletions src/Benchmarks/AttributedBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,190 @@
// limitations under the License.

using BenchmarkDotNet.Attributes;
using Destructurama;
using Destructurama.Attributed;
using Serilog;
using Serilog.Core;

namespace Benchmarks;

public class AttributedBenchmarks
{
private class LogAsScalarClass
{
[LogAsScalar]
public string? Name { get; set; }

[LogAsScalar]
public LogAsScalarClass? Inner { get; set; }

[LogAsScalar(isMutable: true)]
public LogAsScalarClass? Inner2 { get; set; }
}

private class LogMaskedClass
{
[LogMasked]
public string? Password1 { get; set; }

[LogMasked(ShowFirst = 3)]
public string? Password2 { get; set; }

[LogMasked(ShowLast = 3)]
public string? Password3 { get; set; }

[LogMasked(ShowFirst = 3, ShowLast = 3)]
public string? Password4 { get; set; }
}

private class LogReplacedClass
{
private const string REGEX_WITH_VERTICAL_BARS = @"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)";

/// <summary>
/// 123|456|789 results in "***|456|789"
/// </summary>
[LogReplaced(REGEX_WITH_VERTICAL_BARS, "***|$2|$3")]
public string? RegexReplaceFirst { get; set; }
}

private class LogWithNameClass
{
[LogWithName("OtherName1")]
public string? Name { get; set; }

[LogWithName("OtherName2")]
public LogWithNameClass? Inner { get; set; }
}

private class NotLoggedClass
{
[NotLogged]
public string? Name { get; set; }

[NotLogged]
public NotLoggedClass? Inner { get; set; }
}

private class NotLoggedIfDefaultClass
{
[NotLoggedIfDefault]
public string? Name { get; set; }

[NotLoggedIfDefault]
public int Age { get; set; }
}

private class NotLoggedIfNullClass
{
[NotLoggedIfNull]
public string? Name { get; set; }

[NotLoggedIfNull]
public int Age { get; set; }
}

private readonly LogAsScalarClass _logAsScalar = new()
{
Name = "Tom",
Inner = new LogAsScalarClass(),
Inner2 = new LogAsScalarClass(),
};

private readonly LogMaskedClass _logMasked = new()
{
Password1 = "abcdef123456",
Password2 = "abcdef123456",
Password3 = "abcdef123456",
Password4 = "abcdef123456",
};

private readonly LogReplacedClass _logReplaced = new()
{
RegexReplaceFirst = "123|456|789",
};

private readonly LogWithNameClass _logWithName = new()
{
Name = "Tome",
Inner = new LogWithNameClass(),
};

private readonly NotLoggedClass _notLogged = new()
{
Name = "Tom",
Inner = new NotLoggedClass(),
};

private readonly NotLoggedIfDefaultClass _notLoggedIfDefault = new()
{
};

private readonly NotLoggedIfNullClass _notLoggedIfNull = new()
{
};

private ILogEventPropertyValueFactory _factory = null!;
private IDestructuringPolicy _policy = null!;

[GlobalSetup]
public void Setup()
{
(_policy, _factory) = Build(c => c.Destructure.UsingAttributes());
}

private static (IDestructuringPolicy, ILogEventPropertyValueFactory) Build(Func<LoggerConfiguration, LoggerConfiguration> configure)
{
var configuration = new LoggerConfiguration();
var logger = configure(configuration).CreateLogger();

var processor = logger.GetType().GetField("_messageTemplateProcessor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(logger)!;
var converter = processor.GetType().GetField("_propertyValueConverter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(processor)!;
var factory = (ILogEventPropertyValueFactory)converter;
var policies = (IDestructuringPolicy[])converter.GetType().GetField("_destructuringPolicies", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(converter)!;
var policy = policies.First(p => p is AttributedDestructuringPolicy);
return (policy, factory);
}

[Benchmark]
public void LogAsScalar()
{
_policy.TryDestructure(_logAsScalar, _factory, out _);
}

[Benchmark]
public void LogMasked()
{
_policy.TryDestructure(_logMasked, _factory, out _);
}

[Benchmark]
public void LogReplaced()
{
_policy.TryDestructure(_logReplaced, _factory, out _);
}

[Benchmark]
public void LogWithName()
{
_policy.TryDestructure(_logWithName, _factory, out _);
}

[Benchmark]
public void NotLogged()
{
_policy.TryDestructure(_notLogged, _factory, out _);
}

[Benchmark]
public void NotLoggedIfDefault()
{
_policy.TryDestructure(_notLoggedIfDefault, _factory, out _);
}

//[Benchmark]
public void Execute()
[Benchmark]
public void NotLoggedIfNull()
{
//TODO: implement
_policy.TryDestructure(_notLoggedIfNull, _factory, out _);
}
}

0 comments on commit 8e0f0f5

Please sign in to comment.