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

Feature/string builder optimise #68

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .idea/.idea.Splunk.Logging/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.Splunk.Logging/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.Splunk.Logging/.idea/projectSettingsUpdater.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.Splunk.Logging/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 201 additions & 0 deletions .idea/.idea.Splunk.Logging/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>examples</RootNamespace>
<AssemblyName>examples</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
Expand Down
13 changes: 13 additions & 0 deletions src/Splunk.Logging.Common/BatchBuffers/IBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Net.Http;

namespace Splunk.Logging.BatchBuffers
{
public interface IBuffer : IDisposable
{
void Append(HttpEventCollectorEventInfo serializedEventInfo);
long Length { get; }
HttpContent BuildHttpContent(string mediaType);
void SupportOriginalBehaviour();
}
}
72 changes: 72 additions & 0 deletions src/Splunk.Logging.Common/BatchBuffers/PushStreamInMemoryBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Splunk.Logging.BatchBuffers
{
public class PushStreamInMemoryBuffer : IBuffer
{
private readonly List<byte[]> serializedItems = new List<byte[]>();

public void Append(HttpEventCollectorEventInfo serializedEventInfo)
{
serializedItems.Add( Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(serializedEventInfo)));
}

public long Length => serializedItems.Sum(x => x.Length);

public HttpContent BuildHttpContent(string mediaType)
{
return new PushStreamContent(async s =>
{
foreach (var entry in serializedItems)
{
await s.WriteAsync(entry, 0, entry.Length);
}
}, mediaType, Length);
}

public void SupportOriginalBehaviour()
{
}

public void Dispose()
{
}

private class PushStreamContent : HttpContent
{
private readonly Func<Stream, Task> writeContent;
private readonly long knownLength;

public PushStreamContent(Func<Stream, Task> writeContent, string mediaType, long knownLength)
{
this.writeContent = writeContent;
this.knownLength = knownLength;
Headers.ContentType = new MediaTypeHeaderValue(mediaType)
{
CharSet = "utf-8"
};
}

protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
await writeContent(stream);
await stream.FlushAsync();
}

protected override bool TryComputeLength(out long length)
{
length = this.knownLength;
return true;
}
}
}
}
31 changes: 31 additions & 0 deletions src/Splunk.Logging.Common/BatchBuffers/StringBuilderBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

namespace Splunk.Logging.BatchBuffers
{
public class StringBuilderBuffer : IBuffer
{
private readonly StringBuilder builder = new StringBuilder();

public void Append(HttpEventCollectorEventInfo serializedEventInfo)
{
builder.Append(JsonConvert.SerializeObject(serializedEventInfo));
}

public long Length => builder.Length;

public HttpContent BuildHttpContent(string mediaType)
{
return new StringContent(builder.ToString(), Encoding.UTF8, mediaType);
}

public void SupportOriginalBehaviour()
{
}

public void Dispose()
{
}
}
}
Loading