-
Notifications
You must be signed in to change notification settings - Fork 6
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
Zero allocating StatsDPublisher #104
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
754ea27
poc
3da35f6
tidy up
00e726e
renaming
59ae9ea
tests
a3ee371
projects cleanup
77ef965
react to code review part one
dffa0ea
react to codereview part two
98b915b
react to code review part two
60c0ca4
refactored utf8 formatter a bit
94d9e99
removed global buffer size
0d0bea0
naming and more tests
4ad8de8
few more fixes
9f375fe
mini fixes
dcc64ee
react to code review 2
2fe60c6
fixes
e449c14
fixed tests
243c185
moved transport switch into the StatsDConfig
06d3c93
bumped version to 3.2.0
70a9e7d
csproj celanup
44c0999
appveyor version bump
d38482e
get rid of v2
0a5b655
include benchmark for adapted statsd publisher
617ce3b
updated benchmarks
2b6f2af
removed redundant call
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,32 @@ | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
using JustEat.StatsD; | ||
using JustEat.StatsD.Buffered; | ||
|
||
namespace Benchmark | ||
{ | ||
[MemoryDiagnoser] | ||
public class Utf8FormatterBenchmark | ||
{ | ||
private static readonly StatsDMessageFormatter FormatterString = new StatsDMessageFormatter("hello.world"); | ||
private static readonly StatsDUtf8Formatter FormatterBuffer = new StatsDUtf8Formatter("hello.world"); | ||
|
||
private static readonly byte[] Buffer = new byte[512]; | ||
|
||
[Benchmark(Baseline = true)] | ||
public void StringBased() | ||
{ | ||
Encoding.UTF8.GetBytes(FormatterString.Gauge(255, "some.neat.bucket")); | ||
Encoding.UTF8.GetBytes(FormatterString.Timing(255, "some.neat.bucket")); | ||
Encoding.UTF8.GetBytes(FormatterString.Increment(255, "some.neat.bucket")); | ||
} | ||
|
||
[Benchmark] | ||
public void BufferBased() | ||
{ | ||
FormatterBuffer.TryFormat(StatsDMessage.Gauge(255, "some.neat.bucket"), 1, Buffer, out _); | ||
FormatterBuffer.TryFormat(StatsDMessage.Timing(255, "some.neat.bucket"), 1, Buffer, out _); | ||
FormatterBuffer.TryFormat(StatsDMessage.Counter(255, "some.neat.bucket"), 1, Buffer, out _); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,4 +6,4 @@ namespace JustEat.StatsD | |
public class UdpListenersCollection : ICollectionFixture<UdpListeners> | ||
{ | ||
} | ||
} | ||
} |
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,123 @@ | ||
using System; | ||
using System.Text; | ||
using JustEat.StatsD.Buffered; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace JustEat.StatsD | ||
{ | ||
public static class Utf8FormatterTests | ||
{ | ||
private static readonly byte[] Buffer = new byte[512]; | ||
private static readonly StatsDUtf8Formatter Formatter = new StatsDUtf8Formatter("prefix"); | ||
|
||
[Fact] | ||
public static void CounterSampled() | ||
{ | ||
var message = StatsDMessage.Counter(128, "bucket"); | ||
Check(message, 0.5, "prefix.bucket:128|c|@0.5"); | ||
} | ||
|
||
[Fact] | ||
public static void CounterRegular() | ||
{ | ||
var message = StatsDMessage.Counter(128, "bucket"); | ||
Check(message, "prefix.bucket:128|c"); | ||
} | ||
|
||
[Fact] | ||
public static void CounterNegative() | ||
{ | ||
var message = StatsDMessage.Counter(-128, "bucket"); | ||
Check(message, "prefix.bucket:-128|c"); | ||
} | ||
|
||
[Fact] | ||
public static void Timing() | ||
{ | ||
var message = StatsDMessage.Timing(128, "bucket"); | ||
Check(message, "prefix.bucket:128|ms"); | ||
} | ||
|
||
[Fact] | ||
public static void TimingSampled() | ||
{ | ||
var message = StatsDMessage.Timing(128, "bucket"); | ||
Check(message, 0.5, "prefix.bucket:128|ms|@0.5"); | ||
} | ||
|
||
[Fact] | ||
public static void GaugeIntegral() | ||
{ | ||
var message = StatsDMessage.Gauge(128, "bucket"); | ||
Check(message, "prefix.bucket:128|g"); | ||
} | ||
|
||
[Fact] | ||
public static void GaugeFloat() | ||
{ | ||
var message = StatsDMessage.Gauge(128.5, "bucket"); | ||
Check(message, "prefix.bucket:128.5|g"); | ||
} | ||
|
||
[Fact] | ||
public static void MessagesLargerThenAvailableBufferShouldNotBeFormatted() | ||
{ | ||
var buffer = new byte[128]; | ||
var hugeBucket = new string('x', 256); | ||
var message = StatsDMessage.Gauge(128.5, hugeBucket); | ||
Formatter.TryFormat(message, 1.0, buffer, out int written).ShouldBe(false); | ||
written.ShouldBe(0); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, 'z')] | ||
[InlineData(2, 'z')] | ||
[InlineData(4, 'z')] | ||
[InlineData(8, 'z')] | ||
[InlineData(16, 'z')] | ||
[InlineData(32, 'z')] | ||
[InlineData(64, 'z')] | ||
[InlineData(128, 'z')] | ||
[InlineData(256, 'z')] | ||
[InlineData(512, 'z')] | ||
[InlineData(1024, 'z')] | ||
[InlineData(2048, 'z')] | ||
[InlineData(1, 'Ж')] | ||
[InlineData(2, 'Ж')] | ||
[InlineData(4, 'Ж')] | ||
[InlineData(8, 'Ж')] | ||
[InlineData(16, 'Ж')] | ||
[InlineData(32, 'Ж')] | ||
[InlineData(64, 'Ж')] | ||
[InlineData(128, 'Ж')] | ||
[InlineData(256, 'Ж')] | ||
[InlineData(512, 'Ж')] | ||
[InlineData(1024, 'Ж')] | ||
[InlineData(2048, 'Ж')] | ||
public static void GetMaxBufferSizeCalculatesValidBufferSizes(int bucketSize, char ch) | ||
{ | ||
var hugeBucket = new string(ch, bucketSize); | ||
var message = StatsDMessage.Gauge(128.5, hugeBucket); | ||
var expected = $"prefix.{hugeBucket}:128.5|g"; | ||
|
||
var buffer = new byte[Formatter.GetMaxBufferSize(message)]; | ||
|
||
Formatter.TryFormat(message, 1.0, buffer, out int written).ShouldBe(true); | ||
var actual = Encoding.UTF8.GetString(buffer.AsSpan(0, written)); | ||
actual.ShouldBe(expected); | ||
} | ||
|
||
private static void Check(StatsDMessage message, string expected) | ||
{ | ||
Check(message, 1, expected); | ||
} | ||
|
||
private static void Check(StatsDMessage message, double sampleRate, string expected) | ||
{ | ||
Formatter.TryFormat(message, sampleRate, Buffer, out int written).ShouldBe(true); | ||
var result = Encoding.UTF8.GetString(Buffer.AsSpan(0, written)); | ||
result.ShouldBe(expected); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.