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

Add benchmarks #94

Merged
merged 2 commits into from
Jan 24, 2024
Merged
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
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 _);
}
}
2 changes: 0 additions & 2 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using Benchmarks;

new AttributedBenchmarks().Setup();
var config = ManualConfig
.Create(DefaultConfig.Instance)
.AddDiagnoser(MemoryDiagnoser.Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Serilog" Version="[2.10.0,4.0.0)" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="All" />
<InternalsVisibleTo Include="Destructurama.Attributed.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100638a43140e8a1271c1453df1379e64b40b67a1f333864c1aef5ac318a0fa2008545c3d35a82ef005edf0de1ad1e1ea155722fe289df0e462f78c40a668cbc96d7be1d487faef5714a54bb4e57909c86b3924c2db6d55ccf59939b99eb0cab6e8a91429ba0ce630c08a319b323bddcbbd509f1afe4ae77a6cbb8b447f588febc3" />
<InternalsVisibleTo Include="Benchmarks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100638a43140e8a1271c1453df1379e64b40b67a1f333864c1aef5ac318a0fa2008545c3d35a82ef005edf0de1ad1e1ea155722fe289df0e462f78c40a668cbc96d7be1d487faef5714a54bb4e57909c86b3924c2db6d55ccf59939b99eb0cab6e8a91429ba0ce630c08a319b323bddcbbd509f1afe4ae77a6cbb8b447f588febc3" />
</ItemGroup>

</Project>
Loading