-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathSimpleBench.cs
120 lines (97 loc) · 3.55 KB
/
SimpleBench.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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.Diagnostics;
using ZLogger;
namespace Benchmark
{
public static class SimpleBench
{
public static void Run()
{
//new SeriLogBench().Run();
//new NLogBench().Run();
new ZLoggerBench().Run();
}
abstract class BenchmarkBase<T>
{
protected abstract string Name { get; }
protected abstract T GetLogger();
protected abstract void WriteLog(T logger, int x, int y, int z);
protected virtual void Stop() { }
public void Run()
{
var logger = GetLogger();
var sw = Stopwatch.StartNew();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
sw.Restart();
for (int i = 0; i < 100000; i++)
{
WriteLog(logger, 10, 20, 30);
}
Stop();
sw.Stop();
Console.Write($"{Name} totaltime: " + sw.Elapsed.TotalMilliseconds + "ms");
Console.Write(" line/elapsed: ");
// line / elapsed
Console.WriteLine(((double)1000000 / sw.Elapsed.TotalMilliseconds) + "ns");
}
}
class ZLoggerBench : BenchmarkBase<Microsoft.Extensions.Logging.ILogger>
{
protected override string Name => "ZLogger";
ILoggerProvider factory;
protected override Microsoft.Extensions.Logging.ILogger GetLogger()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(logging =>
{
logging.AddZLoggerFile("ZLogger.log");
//options.AddZLoggerConsole();
});
var serviceProvider = serviceCollection.BuildServiceProvider();
factory = serviceProvider.GetService<ILoggerProvider>();
var logger = factory.CreateLogger("Test");
return logger;
}
protected override void WriteLog(Microsoft.Extensions.Logging.ILogger logger, int x, int y, int z)
{
logger.ZLogInformation($"x:{x} y:{y} z:{z}");
}
protected override void Stop()
{
factory.Dispose();
}
}
class NLogBench : BenchmarkBase<NLog.Logger>
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
protected override string Name => "NLog";
protected override NLog.Logger GetLogger()
{
return logger;
}
protected override void WriteLog(NLog.Logger logger, int x, int y, int z)
{
logger.Info("x:{0} y:{1} z:{2}", x, y, z);
}
}
class SeriLogBench : BenchmarkBase<Serilog.Core.Logger>
{
protected override string Name => "Serilog";
protected override Serilog.Core.Logger GetLogger()
{
return new Serilog.LoggerConfiguration()
.WriteTo.File("seri.txt")
.CreateLogger();
}
protected override void WriteLog(Serilog.Core.Logger logger, int x, int y, int z)
{
logger.Information("x:{x} y:{y} z:{z}", x, y, z);
}
}
}
}