-
Notifications
You must be signed in to change notification settings - Fork 782
/
Copy pathLogBenchmarks.cs
130 lines (109 loc) · 4.64 KB
/
LogBenchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;
/*
BenchmarkDotNet v0.13.10, Windows 11 (10.0.22621.3007/22H2/2022Update/SunValley2)
11th Gen Intel Core i7-1185G7 3.00GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.101
[Host] : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
DefaultJob : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|------------------------------ |-----------:|----------:|----------:|-------:|----------:|
| NoListenerStringInterpolation | 124.458 ns | 2.5188 ns | 2.2329 ns | 0.0114 | 72 B |
| NoListenerExtensionMethod | 36.326 ns | 0.2916 ns | 0.2435 ns | 0.0102 | 64 B |
| NoListener | 1.375 ns | 0.0586 ns | 0.0896 ns | - | - |
| CreateLoggerRepeatedly | 48.295 ns | 0.5951 ns | 0.4970 ns | 0.0038 | 24 B |
| OneProcessor | 98.133 ns | 1.8805 ns | 1.5703 ns | 0.0063 | 40 B |
| TwoProcessors | 105.414 ns | 0.4610 ns | 0.3850 ns | 0.0063 | 40 B |
| ThreeProcessors | 102.023 ns | 1.4187 ns | 1.1847 ns | 0.0063 | 40 B |
*/
namespace Benchmarks.Logs;
public class LogBenchmarks
{
private const double FoodPrice = 2.99;
private static readonly string FoodName = "tomato";
private readonly ILogger loggerWithNoListener;
private readonly ILogger loggerWithOneProcessor;
private readonly ILogger loggerWithTwoProcessors;
private readonly ILogger loggerWithThreeProcessors;
private readonly ILoggerFactory loggerFactoryWithNoListener;
private readonly ILoggerFactory loggerFactoryWithOneProcessor;
private readonly ILoggerFactory loggerFactoryWithTwoProcessor;
private readonly ILoggerFactory loggerFactoryWithThreeProcessor;
public LogBenchmarks()
{
this.loggerFactoryWithNoListener = LoggerFactory.Create(builder => { });
this.loggerWithNoListener = this.loggerFactoryWithNoListener.CreateLogger<LogBenchmarks>();
this.loggerFactoryWithOneProcessor = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options => options
.AddProcessor(new DummyLogProcessor()));
});
this.loggerWithOneProcessor = this.loggerFactoryWithOneProcessor.CreateLogger<LogBenchmarks>();
this.loggerFactoryWithTwoProcessor = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options => options
.AddProcessor(new DummyLogProcessor())
.AddProcessor(new DummyLogProcessor()));
});
this.loggerWithTwoProcessors = this.loggerFactoryWithTwoProcessor.CreateLogger<LogBenchmarks>();
this.loggerFactoryWithThreeProcessor = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options => options
.AddProcessor(new DummyLogProcessor())
.AddProcessor(new DummyLogProcessor())
.AddProcessor(new DummyLogProcessor()));
});
this.loggerWithThreeProcessors = this.loggerFactoryWithThreeProcessor.CreateLogger<LogBenchmarks>();
}
[GlobalCleanup]
public void GlobalCleanup()
{
this.loggerFactoryWithNoListener.Dispose();
this.loggerFactoryWithOneProcessor.Dispose();
this.loggerFactoryWithTwoProcessor.Dispose();
this.loggerFactoryWithThreeProcessor.Dispose();
}
[Benchmark]
public void NoListenerStringInterpolation()
{
this.loggerWithNoListener.LogInformation($"Hello from {FoodName} {FoodPrice}.");
}
[Benchmark]
public void NoListenerExtensionMethod()
{
this.loggerWithNoListener.LogInformation("Hello from {name} {price}.", FoodName, FoodPrice);
}
[Benchmark]
public void NoListener()
{
Food.SayHello(this.loggerWithNoListener, FoodName, FoodPrice);
}
[Benchmark]
public void CreateLoggerRepeatedly()
{
var logger = this.loggerFactoryWithNoListener.CreateLogger<LogBenchmarks>();
Food.SayHello(logger, FoodName, FoodPrice);
}
[Benchmark]
public void OneProcessor()
{
Food.SayHello(this.loggerWithOneProcessor, FoodName, FoodPrice);
}
[Benchmark]
public void TwoProcessors()
{
Food.SayHello(this.loggerWithTwoProcessors, FoodName, FoodPrice);
}
[Benchmark]
public void ThreeProcessors()
{
Food.SayHello(this.loggerWithThreeProcessors, FoodName, FoodPrice);
}
internal class DummyLogProcessor : BaseProcessor<LogRecord>
{
}
}