Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce file-scoped namespaces #1725

Merged
merged 1 commit into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -16,3 +16,6 @@ end_of_line = lf

[*.{cmd, bat}]
end_of_line = crlf

# C# formatting settings - Namespace options
csharp_style_namespace_declarations = file_scoped:suggestion
147 changes: 73 additions & 74 deletions test/Serilog.PerformanceTests/AllocationsBenchmark.cs
Expand Up @@ -6,100 +6,99 @@
using System.Collections.Generic;
using System.Linq;

namespace Serilog.PerformanceTests
namespace Serilog.PerformanceTests;

[MemoryDiagnoser]
public class AllocationsBenchmark
{
[MemoryDiagnoser]
public class AllocationsBenchmark
{
readonly ILogger _logger;
readonly ILogger _enrichedLogger;
readonly LogEvent _emptyEvent;
readonly object _dictionaryValue;
readonly object _anonymousObject;
readonly object _sequence;
readonly ILogger _logger;
readonly ILogger _enrichedLogger;
readonly LogEvent _emptyEvent;
readonly object _dictionaryValue;
readonly object _anonymousObject;
readonly object _sequence;

public AllocationsBenchmark()
{
_logger = new LoggerConfiguration().CreateLogger();
public AllocationsBenchmark()
{
_logger = new LoggerConfiguration().CreateLogger();

_enrichedLogger = _logger.ForContext(new PropertyEnricher("Prop", "Value"));
_enrichedLogger = _logger.ForContext(new PropertyEnricher("Prop", "Value"));

_emptyEvent = new(
DateTimeOffset.Now,
LogEventLevel.Information,
null,
new(Enumerable.Empty<MessageTemplateToken>()),
Enumerable.Empty<LogEventProperty>());
_emptyEvent = new(
DateTimeOffset.Now,
LogEventLevel.Information,
null,
new(Enumerable.Empty<MessageTemplateToken>()),
Enumerable.Empty<LogEventProperty>());

_anonymousObject = new
_anonymousObject = new
{
Level11 = "Val1",
Level12 = new
{
Level11 = "Val1",
Level12 = new
Level21 = (int?)42,
Level22 = new
{
Level21 = (int?)42,
Level22 = new
Level31 = System.Reflection.BindingFlags.FlattenHierarchy,
Level32 = new
{
Level31 = System.Reflection.BindingFlags.FlattenHierarchy,
Level32 = new
{
X = 3,
Y = "4",
Z = (short?)5
}
X = 3,
Y = "4",
Z = (short?)5
}
}
};
}
};

_dictionaryValue = new Dictionary<string, object> {
{ "Level11", "Val1" },
{ "Level12", new Dictionary<string, object> {
{ "Level21", (int?)42 },
{ "Level22", new Dictionary<string, object> {
{ "Level31", System.Reflection.BindingFlags.FlattenHierarchy },
{ "Level32", new { X = 3, Y = "4", Z = (short?)5 } }
}
_dictionaryValue = new Dictionary<string, object> {
{ "Level11", "Val1" },
{ "Level12", new Dictionary<string, object> {
{ "Level21", (int?)42 },
{ "Level22", new Dictionary<string, object> {
{ "Level31", System.Reflection.BindingFlags.FlattenHierarchy },
{ "Level32", new { X = 3, Y = "4", Z = (short?)5 } }
}
}
}
};
}
};

_sequence = new List<object> { "1", 2, (int?)3, "4", (short)5 };
}
_sequence = new List<object> { "1", 2, (int?)3, "4", (short)5 };
}

[Benchmark(Baseline = true)]
public void LogEmpty()
{
_logger.Write(_emptyEvent);
}
[Benchmark(Baseline = true)]
public void LogEmpty()
{
_logger.Write(_emptyEvent);
}

[Benchmark]
public void LogEmptyWithEnricher()
{
_enrichedLogger.Write(_emptyEvent);
}
[Benchmark]
public void LogEmptyWithEnricher()
{
_enrichedLogger.Write(_emptyEvent);
}

[Benchmark]
public void LogScalar()
{
_logger.Information("Template: {ScalarValue}", "42");
}
[Benchmark]
public void LogScalar()
{
_logger.Information("Template: {ScalarValue}", "42");
}

[Benchmark]
public void LogDictionary()
{
_logger.Information("Template: {DictionaryValue}", _dictionaryValue);
}
[Benchmark]
public void LogDictionary()
{
_logger.Information("Template: {DictionaryValue}", _dictionaryValue);
}

[Benchmark]
public void LogSequence()
{
_logger.Information("Template: {SequenceValue}", _sequence);
}
[Benchmark]
public void LogSequence()
{
_logger.Information("Template: {SequenceValue}", _sequence);
}

[Benchmark]
public void LogAnonymous()
{
_logger.Information("Template: {@AnonymousObject}.", _anonymousObject);
}
[Benchmark]
public void LogAnonymous()
{
_logger.Information("Template: {@AnonymousObject}.", _anonymousObject);
}
}
85 changes: 42 additions & 43 deletions test/Serilog.PerformanceTests/BindingBenchmark.cs
Expand Up @@ -17,56 +17,55 @@
using Serilog.Events;
using Serilog.PerformanceTests.Support;

namespace Serilog.PerformanceTests
namespace Serilog.PerformanceTests;

/// <summary>
/// Tests the cost of writing through the logging pipeline.
/// </summary>
[MemoryDiagnoser]
public class BindingBenchmark
{
/// <summary>
/// Tests the cost of writing through the logging pipeline.
/// </summary>
[MemoryDiagnoser]
public class BindingBenchmark
{
const string
MT0 = "Zero",
MT1 = "Zero{A}one",
MT5 = "Zero{A}one{B}two{C}three{D}four{E}five";
const string
MT0 = "Zero",
MT1 = "Zero{A}one",
MT5 = "Zero{A}one{B}two{C}three{D}four{E}five";

ILogger _log = null!;
object[] _zero= null!, _one= null!, _five = null!;
ILogger _log = null!;
object[] _zero= null!, _one= null!, _five = null!;

[GlobalSetup]
public void Setup()
{
_log = new LoggerConfiguration()
.WriteTo.Sink(new NullSink())
.CreateLogger();
[GlobalSetup]
public void Setup()
{
_log = new LoggerConfiguration()
.WriteTo.Sink(new NullSink())
.CreateLogger();

_zero = new object[0];
_one = new object[] { 1 };
_five = new object[] { 1, 2, 3, 4, 5 };
}
_zero = new object[0];
_one = new object[] { 1 };
_five = new object[] { 1, 2, 3, 4, 5 };
}

// The benchmarks run p.Count() to force enumeration; this will be representative of how the API
// is consumed (there's not much point benchmarking time to return a lazy enumerator).
// The benchmarks run p.Count() to force enumeration; this will be representative of how the API
// is consumed (there's not much point benchmarking time to return a lazy enumerator).

[Benchmark(Baseline = true)]
public (MessageTemplate, int) BindZero()
{
_log.BindMessageTemplate(MT0, _zero, out var mt, out var p);
return (mt, p!.Count())!;
}
[Benchmark(Baseline = true)]
public (MessageTemplate, int) BindZero()
{
_log.BindMessageTemplate(MT0, _zero, out var mt, out var p);
return (mt, p!.Count())!;
}

[Benchmark]
public (MessageTemplate, int) BindOne()
{
_log.BindMessageTemplate(MT1, _one, out var mt, out var p);
return (mt, p!.Count())!;
}
[Benchmark]
public (MessageTemplate, int) BindOne()
{
_log.BindMessageTemplate(MT1, _one, out var mt, out var p);
return (mt, p!.Count())!;
}

[Benchmark]
public (MessageTemplate, int) BindFive()
{
_log.BindMessageTemplate(MT5, _five, out var mt, out var p);
return (mt, p!.Count())!;
}
[Benchmark]
public (MessageTemplate, int) BindFive()
{
_log.BindMessageTemplate(MT5, _five, out var mt, out var p);
return (mt, p!.Count())!;
}
}