-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(tar): support for async streams #746
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
82 changes: 82 additions & 0 deletions
82
benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarInputStream.cs
This file contains hidden or 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,82 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using BenchmarkDotNet.Attributes; | ||
| using ICSharpCode.SharpZipLib.Tar; | ||
|
|
||
| namespace ICSharpCode.SharpZipLib.Benchmark.Tar | ||
| { | ||
| [MemoryDiagnoser] | ||
| [Config(typeof(MultipleRuntimes))] | ||
| public class TarInputStream | ||
| { | ||
| private readonly byte[] archivedData; | ||
| private readonly byte[] readBuffer = new byte[1024]; | ||
|
|
||
| public TarInputStream() | ||
| { | ||
| using (var outputMemoryStream = new MemoryStream()) | ||
| { | ||
| using (var zipOutputStream = | ||
| new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) | ||
| { | ||
| var tarEntry = TarEntry.CreateTarEntry("some file"); | ||
| tarEntry.Size = 1024 * 1024; | ||
| zipOutputStream.PutNextEntry(tarEntry); | ||
|
|
||
| var rng = RandomNumberGenerator.Create(); | ||
| var inputBuffer = new byte[1024]; | ||
| rng.GetBytes(inputBuffer); | ||
|
|
||
| for (int i = 0; i < 1024; i++) | ||
| { | ||
| zipOutputStream.Write(inputBuffer, 0, inputBuffer.Length); | ||
| } | ||
| } | ||
|
|
||
| archivedData = outputMemoryStream.ToArray(); | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public long ReadTarInputStream() | ||
| { | ||
| using (var memoryStream = new MemoryStream(archivedData)) | ||
| using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8)) | ||
| { | ||
| var entry = zipInputStream.GetNextEntry(); | ||
|
|
||
| while (zipInputStream.Read(readBuffer, 0, readBuffer.Length) > 0) | ||
| { | ||
| } | ||
|
|
||
| return entry.Size; | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task<long> ReadTarInputStreamAsync() | ||
| { | ||
| using (var memoryStream = new MemoryStream(archivedData)) | ||
| using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8)) | ||
| { | ||
| var entry = await zipInputStream.GetNextEntryAsync(CancellationToken.None); | ||
|
|
||
| #if NETCOREAPP2_1_OR_GREATER | ||
| while (await zipInputStream.ReadAsync(readBuffer.AsMemory()) > 0) | ||
| { | ||
| } | ||
| #else | ||
| while (await zipInputStream.ReadAsync(readBuffer, 0, readBuffer.Length) > 0) | ||
| { | ||
| } | ||
| #endif | ||
|
|
||
| return entry.Size; | ||
| } | ||
| } | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarOutputStream.cs
This file contains hidden or 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,64 @@ | ||
| using System.IO; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using BenchmarkDotNet.Attributes; | ||
| using ICSharpCode.SharpZipLib.Tar; | ||
|
|
||
| namespace ICSharpCode.SharpZipLib.Benchmark.Tar | ||
| { | ||
| [MemoryDiagnoser] | ||
| [Config(typeof(MultipleRuntimes))] | ||
| public class TarOutputStream | ||
| { | ||
| private readonly byte[] backingArray = new byte[1024 * 1024 + (6 * 1024)]; | ||
| private readonly byte[] inputBuffer = new byte[1024]; | ||
| private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); | ||
|
|
||
| [Benchmark] | ||
| public void WriteTarOutputStream() | ||
| { | ||
| using (var outputMemoryStream = new MemoryStream(backingArray)) | ||
| { | ||
| using (var tarOutputStream = | ||
| new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) | ||
| { | ||
| var tarEntry = TarEntry.CreateTarEntry("some file"); | ||
| tarEntry.Size = 1024 * 1024; | ||
| tarOutputStream.PutNextEntry(tarEntry); | ||
|
|
||
| _rng.GetBytes(inputBuffer); | ||
|
|
||
| for (int i = 0; i < 1024; i++) | ||
| { | ||
| tarOutputStream.Write(inputBuffer, 0, inputBuffer.Length); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task WriteTarOutputStreamAsync() | ||
| { | ||
| using (var outputMemoryStream = new MemoryStream(backingArray)) | ||
| { | ||
| using (var tarOutputStream = | ||
| new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) | ||
| { | ||
| var tarEntry = TarEntry.CreateTarEntry("some file"); | ||
| tarEntry.Size = 1024 * 1024; | ||
|
|
||
| await tarOutputStream.PutNextEntryAsync(tarEntry, CancellationToken.None); | ||
|
|
||
| _rng.GetBytes(inputBuffer); | ||
|
|
||
| for (int i = 0; i < 1024; i++) | ||
| { | ||
| await tarOutputStream.WriteAsync(inputBuffer, 0, inputBuffer.Length); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,22 @@ | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace ICSharpCode.SharpZipLib.Core | ||
| { | ||
| class StringBuilderPool | ||
| { | ||
| public static StringBuilderPool Instance { get; } = new StringBuilderPool(); | ||
| private readonly Queue<StringBuilder> pool = new Queue<StringBuilder>(); | ||
|
|
||
| public StringBuilder Rent() | ||
| { | ||
| return pool.Count > 0 ? pool.Dequeue() : new StringBuilder(); | ||
| } | ||
|
|
||
| public void Return(StringBuilder builder) | ||
| { | ||
| builder.Clear(); | ||
| pool.Enqueue(builder); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.