Skip to content

Commit

Permalink
Add benchmark (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Jan 22, 2024
1 parent 7f271fe commit b3950ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
44 changes: 41 additions & 3 deletions src/Benchmarks/JsonNetBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,57 @@
// limitations under the License.

using BenchmarkDotNet.Attributes;
using Destructurama;
using Destructurama.JsonNet;
using Newtonsoft.Json;
using Serilog;
using Serilog.Core;

namespace Benchmarks;

public class JsonNetBenchmarks
{
private class HasName
{
public string? Name { get; set; }
}

private ILogEventPropertyValueFactory _factory = null!;
private object _value = null!;
private readonly JsonNetDestructuringPolicy _policy = new();

[GlobalSetup]
public void Setup()
{
var test = new
{
HN = new HasName { Name = "Some name" },
Arr = new[] { 1, 2, 3 },
S = "Some string",
D = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" } },
E = (object?)null,
ESPN = JsonConvert.DeserializeObject("{\"\":\"Empty string property name\"}"),
WSPN = JsonConvert.DeserializeObject("{\"\r\n\":\"Whitespace property name\"}")
};

string ser = JsonConvert.SerializeObject(test, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
_value = JsonConvert.DeserializeObject<dynamic>(ser)!;

var log = new LoggerConfiguration()
.Destructure.JsonNetTypes()
.CreateLogger();

var processor = log.GetType().GetField("_messageTemplateProcessor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(log)!;
var converter = processor.GetType().GetField("_propertyValueConverter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(processor)!;
_factory = (ILogEventPropertyValueFactory)converter;
}

//[Benchmark]
public void Execute()
[Benchmark]
public void Destructure()
{
//TODO: implement
_policy.TryDestructure(_value, _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 JsonNetBenchmarks().Setup();
var config = ManualConfig
.Create(DefaultConfig.Instance)
.AddDiagnoser(MemoryDiagnoser.Default);
Expand Down
1 change: 1 addition & 0 deletions src/Destructurama.JsonNet/Destructurama.JsonNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="All" />
<InternalsVisibleTo Include="Benchmarks, PublicKey=00240000048000009400000006020000002400005253413100040000010001000fe215af9b769897395aac526ca6744222fcf543787b16f58980844d6c49f65bbf0a62502d69646c58f14a8a29ba60d43c85a2a5ef1ed103c9a0e18d6491e4b0bafd2c25e290028d19203d4366ae5651aafd48abaf485588fd6a25fbe23c2c9a644a24337a041966b09cbb73dc3a080bf79564d15a26e97c1bff4f345ee256a9" />
</ItemGroup>

</Project>

0 comments on commit b3950ef

Please sign in to comment.