-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
77 lines (64 loc) · 1.87 KB
/
Program.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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.Logging;
BenchmarkRunner.Run<Benchmark>();
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net80)]
public class Benchmark
{
private const int _lines = 10;
private ILogger _log;
private string _message;
private Exception _ex;
[GlobalSetup]
public void GlobalSetup()
{
_log = new ThrowAwayLogger();
_message = "Message";
_ex = new Exception("ExMessage");
}
[Benchmark(Baseline = true)]
public int SourceCodeGenerated()
{
for (int i = 0; i < _lines; i++)
{
SourceCodeGeneratedLog.Sample(_log, _ex, _message);
}
return _lines;
}
[Benchmark]
public int Concat()
{
for (int i = 0; i < _lines; i++)
{
_log.Log(LogLevel.Information, _ex, "Test " + _message);
}
return _lines;
}
[Benchmark]
public int Interpolation()
{
for (int i = 0; i < _lines; i++)
{
_log.Log(LogLevel.Information, _ex, $"Test {_message}");
}
return _lines;
}
}
public static partial class SourceCodeGeneratedLog
{
[LoggerMessage(Level = LogLevel.Information, Message = "Test {message}")]
public static partial void Sample(ILogger logger, Exception ex, string message);
}
// https://github.com/BenjaminAbt/ThrowAwayLogger
public class ThrowAwayLogger : ILogger, IDisposable
{
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { }
public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable BeginScope<TState>(TState state) => this;
public void Dispose() { }
}