|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | using BenchmarkDotNet.Attributes;
|
| 16 | +using Destructurama; |
| 17 | +using Destructurama.JsonNet; |
| 18 | +using Newtonsoft.Json; |
| 19 | +using Serilog; |
| 20 | +using Serilog.Core; |
16 | 21 |
|
17 | 22 | namespace Benchmarks;
|
18 | 23 |
|
19 | 24 | public class JsonNetBenchmarks
|
20 | 25 | {
|
| 26 | + private class HasName |
| 27 | + { |
| 28 | + public string? Name { get; set; } |
| 29 | + } |
| 30 | + |
| 31 | + private ILogEventPropertyValueFactory _factory = null!; |
| 32 | + private object _value = null!; |
| 33 | + private readonly JsonNetDestructuringPolicy _policy = new(); |
| 34 | + |
21 | 35 | [GlobalSetup]
|
22 | 36 | public void Setup()
|
23 | 37 | {
|
| 38 | + var test = new |
| 39 | + { |
| 40 | + HN = new HasName { Name = "Some name" }, |
| 41 | + Arr = new[] { 1, 2, 3 }, |
| 42 | + S = "Some string", |
| 43 | + D = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" } }, |
| 44 | + E = (object?)null, |
| 45 | + ESPN = JsonConvert.DeserializeObject("{\"\":\"Empty string property name\"}"), |
| 46 | + WSPN = JsonConvert.DeserializeObject("{\"\r\n\":\"Whitespace property name\"}") |
| 47 | + }; |
| 48 | + |
| 49 | + string ser = JsonConvert.SerializeObject(test, new JsonSerializerSettings |
| 50 | + { |
| 51 | + TypeNameHandling = TypeNameHandling.Auto |
| 52 | + }); |
| 53 | + _value = JsonConvert.DeserializeObject<dynamic>(ser)!; |
| 54 | + |
| 55 | + var log = new LoggerConfiguration() |
| 56 | + .Destructure.JsonNetTypes() |
| 57 | + .CreateLogger(); |
| 58 | + |
| 59 | + var processor = log.GetType().GetField("_messageTemplateProcessor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(log)!; |
| 60 | + var converter = processor.GetType().GetField("_propertyValueConverter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(processor)!; |
| 61 | + _factory = (ILogEventPropertyValueFactory)converter; |
24 | 62 | }
|
25 | 63 |
|
26 |
| - //[Benchmark] |
27 |
| - public void Execute() |
| 64 | + [Benchmark] |
| 65 | + public void Destructure() |
28 | 66 | {
|
29 |
| - //TODO: implement |
| 67 | + _policy.TryDestructure(_value, _factory, out _); |
30 | 68 | }
|
31 | 69 | }
|
0 commit comments