-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScopeBenchmark.cs
53 lines (47 loc) · 1.74 KB
/
ScopeBenchmark.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
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Logging;
using SqliteLogger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Benchmarks
{
[MemoryDiagnoser]
[MarkdownExporterAttribute.GitHub]
public class ScopeBenchmark
{
private ILogger<Program> logger = null!;
private ILoggerFactory loggerFactory = null!;
public static IEnumerable<IReadOnlyCollection<KeyValuePair<string, object?>>> ScopeGenerator()
{
yield return new List<KeyValuePair<string, object?>>() { KeyValuePair.Create<string, object?>("scopeName", "scopeValue") };
yield return new Dictionary<string, object?>() { { "key1", "value1" }, { "key2", 1234 } };
yield return new Dictionary<string, object?>() { { "key1", "value1" }, { "key2", 1234 }, { "key3", new[] { new { a = 1, b = new { b1 = "b2" } } } } };
}
[GlobalSetup]
public void GlobalSetup()
{
loggerFactory =
LoggerFactory.Create(builder =>
builder.AddSqliteLogger(options =>
{
options.FilePath = "test.db";
}));
logger = loggerFactory.CreateLogger<Program>();
}
[GlobalCleanup]
public void GlobalCleanup()
{
loggerFactory?.Dispose();
}
[Benchmark]
[ArgumentsSource(nameof(ScopeGenerator))]
public void SqliteLoggerInMemory(IReadOnlyCollection<KeyValuePair<string, object?>> scopeCollection)
{
using IDisposable? scope = logger.BeginScope(scopeCollection);
logger.LogInformation("Hello World!");
}
}
}