-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (66 loc) · 2.64 KB
/
Copy pathProgram.cs
File metadata and controls
69 lines (66 loc) · 2.64 KB
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
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace testperformancelogging
{
class Program
{
static void Main(string[] args)
{
BenchmarkDotNet.Running.BenchmarkRunner.Run<LoggerBenchmark>();
}
}
[BenchmarkDotNet.Attributes.MemoryDiagnoser]
[BenchmarkDotNet.Attributes.MinColumn]
[BenchmarkDotNet.Attributes.MaxColumn]
[BenchmarkDotNet.Attributes.RankColumn]
public class LoggerBenchmark
{
private static readonly Action<ILogger, string, string, int, Exception> _fastLogMessageWithParameters;
private static readonly Action<ILogger, Exception> _fastLogMessage;
private readonly ServiceProvider service;
private readonly ILogger<LoggerBenchmark> logger;
const string Val1 = "This is a sample Parameter string";
const string Val2 = " the length of these values should not matter for boxing";
const int Val3 = 96;
static LoggerBenchmark()
{
_fastLogMessageWithParameters = LoggerMessage.Define<string, string, int>(LogLevel.Critical, new EventId(1, "BenchmarkEvent"), "My simple test string {val1} {val2} {val3}");
_fastLogMessage = LoggerMessage.Define(LogLevel.Critical, new EventId(1, "BenchmarkEvent"), "My simple test string without parameters");
}
public LoggerBenchmark()
{
var services = new ServiceCollection()
.AddLogging(builder =>
{
builder.AddDebug();
});
this.service = services.BuildServiceProvider();
logger = this.service.GetRequiredService<ILogger<LoggerBenchmark>>();
}
[BenchmarkDotNet.Attributes.Benchmark(Baseline = true)]
public object NormalLoggerCallWithParameter()
{
logger.LogCritical(null, "My simple test string {val1} {val2} {val3}", Val1, Val2, Val3);
return logger;
}
[BenchmarkDotNet.Attributes.Benchmark]
public object NormalLoggerCall()
{
logger.LogCritical(null, "My simple test string {val1} {val2} {val3}", Val1, Val2, Val3);
return logger;
}
[BenchmarkDotNet.Attributes.Benchmark]
public object FastLoggerWithParameters()
{
_fastLogMessageWithParameters(logger, Val1, Val2, Val3, null);
return logger;
}
[BenchmarkDotNet.Attributes.Benchmark]
public object FastLogger()
{
_fastLogMessage(logger, null);
return logger;
}
}
}