Skip to content

Commit 90146f8

Browse files
authored
Updated logging benchmarks to include source generation (#2800)
1 parent 110b5c2 commit 90146f8

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

test/Benchmarks/Logs/Food.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// <copyright file="Food.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Microsoft.Extensions.Logging;
18+
19+
namespace Benchmarks.Logs
20+
{
21+
public static partial class Food
22+
{
23+
[LoggerMessage(
24+
EventId = 0,
25+
Level = LogLevel.Information,
26+
Message = "Hello from {food} {price}.")]
27+
public static partial void SayHello(
28+
ILogger logger, string food, double price);
29+
}
30+
}

test/Benchmarks/Logs/LogBenchmarks.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919
using OpenTelemetry;
2020
using OpenTelemetry.Logs;
2121

22+
/*
23+
// * Summary *
24+
25+
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.1466 (21H2)
26+
Intel Core i7-4790 CPU 3.60GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
27+
.NET SDK=6.0.101
28+
[Host] : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT
29+
DefaultJob : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT
30+
31+
32+
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
33+
|--------------------------------------- |-----------:|----------:|----------:|-------:|----------:|
34+
| NoListener | 72.365 ns | 0.9425 ns | 0.8817 ns | 0.0153 | 64 B |
35+
| NoListenerWithLoggerMessageGenerator | 4.769 ns | 0.0161 ns | 0.0142 ns | - | - |
36+
| OneProcessor | 168.330 ns | 0.6198 ns | 0.5494 ns | 0.0553 | 232 B |
37+
| OneProcessorWithLoggerMessageGenerator | 142.898 ns | 0.5233 ns | 0.4086 ns | 0.0401 | 168 B |
38+
| TwoProcessors | 173.727 ns | 0.5978 ns | 0.4992 ns | 0.0553 | 232 B |
39+
| ThreeProcessors | 174.295 ns | 0.7697 ns | 0.7200 ns | 0.0553 | 232 B |
40+
*/
41+
2242
namespace Benchmarks.Logs
2343
{
2444
[MemoryDiagnoser]
@@ -62,25 +82,37 @@ public LogBenchmarks()
6282
[Benchmark]
6383
public void NoListener()
6484
{
65-
this.loggerWithNoListener.LogInformation("Hello, World!");
85+
this.loggerWithNoListener.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
86+
}
87+
88+
[Benchmark]
89+
public void NoListenerWithLoggerMessageGenerator()
90+
{
91+
Food.SayHello(this.loggerWithNoListener, "tomato", 2.99);
6692
}
6793

6894
[Benchmark]
6995
public void OneProcessor()
7096
{
71-
this.loggerWithOneProcessor.LogInformation("Hello, World!");
97+
this.loggerWithOneProcessor.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
98+
}
99+
100+
[Benchmark]
101+
public void OneProcessorWithLoggerMessageGenerator()
102+
{
103+
Food.SayHello(this.loggerWithOneProcessor, "tomato", 2.99);
72104
}
73105

74106
[Benchmark]
75107
public void TwoProcessors()
76108
{
77-
this.loggerWithTwoProcessors.LogInformation("Hello, World!");
109+
this.loggerWithTwoProcessors.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
78110
}
79111

80112
[Benchmark]
81113
public void ThreeProcessors()
82114
{
83-
this.loggerWithThreeProcessors.LogInformation("Hello, World!");
115+
this.loggerWithThreeProcessors.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
84116
}
85117

86118
internal class DummyLogProcessor : BaseProcessor<LogRecord>

0 commit comments

Comments
 (0)