forked from anton-gogolev/nlog.loki
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added option to order logs by timestamps. Saves 20% allocations by th…
…e serializer when set to false. Option true by default to avoid breaking changes on Loki v2.3 and below. You are advised to set option to false when using Loki v2.4+. (#11) * Added a benchmark to highlight cost of ordering events by timestamp. Results for 100 ordered events: - ordering: 11448 B allocated - no ordering: 9176 B So, 2.2kB for 100 events (already ordered) saved. * Implemented configuration to inject option. * Documented orderWrites and set it to true by default to avoid breaking changes. * Added unit testing on the ordering of the serialized logs sent to Loki.
- Loading branch information
1 parent
b6dea29
commit 2556149
Showing
10 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Json; | ||
using BenchmarkDotNet.Attributes; | ||
using NLog.Loki.Model; | ||
|
||
namespace Benchmark; | ||
|
||
[MemoryDiagnoser] | ||
public class LokiEventsSerializer | ||
{ | ||
private readonly MemoryStream stream = new(); | ||
private readonly JsonSerializerOptions serializerOptionsWithoutOrdering = new(); | ||
private readonly JsonSerializerOptions serializerOptionsWithOrdering = new(); | ||
|
||
private readonly LokiEvent @event; | ||
private readonly IEnumerable<LokiEvent> manyLokiEvents; | ||
|
||
public LokiEventsSerializer() | ||
{ | ||
@event = new LokiEvent( | ||
new LokiLabels(new LokiLabel("env", "benchmark"), new LokiLabel("job", "WriteLogEventsAsync")), | ||
DateTime.Now, | ||
"Info|Receive message from \"A\" with destination \"B\"."); | ||
var events = new List<LokiEvent>(100); | ||
for(var i = 0; i < 100; i++) | ||
events.Add(new LokiEvent(@event.Labels, DateTime.Now, @event.Line)); | ||
manyLokiEvents = events; | ||
|
||
serializerOptionsWithoutOrdering = new JsonSerializerOptions(); | ||
serializerOptionsWithoutOrdering.Converters.Add(new NLog.Loki.LokiEventsSerializer(orderWrites: false)); | ||
serializerOptionsWithoutOrdering.Converters.Add(new NLog.Loki.LokiEventSerializer()); | ||
|
||
serializerOptionsWithOrdering = new JsonSerializerOptions(); | ||
serializerOptionsWithOrdering.Converters.Add(new NLog.Loki.LokiEventsSerializer(orderWrites: true)); | ||
serializerOptionsWithOrdering.Converters.Add(new NLog.Loki.LokiEventSerializer()); | ||
} | ||
|
||
[Benchmark] | ||
public void SerializeManyEventsWithoutOrdering() | ||
{ | ||
stream.Position = 0; | ||
JsonSerializer.Serialize(stream, manyLokiEvents, serializerOptionsWithoutOrdering); | ||
} | ||
|
||
[Benchmark] | ||
public void SerializeManyEventsWithOrdering() | ||
{ | ||
stream.Position = 0; | ||
JsonSerializer.Serialize(stream, manyLokiEvents, serializerOptionsWithOrdering); | ||
} | ||
|
||
[Benchmark] | ||
public void SerializeSingleEvent() | ||
{ | ||
stream.Position = 0; | ||
JsonSerializer.Serialize(stream, @event, serializerOptionsWithoutOrdering); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters