|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using BenchmarkDotNet.Attributes; |
| 8 | +using ICSharpCode.SharpZipLib.Tar; |
| 9 | + |
| 10 | +namespace ICSharpCode.SharpZipLib.Benchmark.Tar |
| 11 | +{ |
| 12 | + [MemoryDiagnoser] |
| 13 | + [Config(typeof(MultipleRuntimes))] |
| 14 | + public class TarInputStream |
| 15 | + { |
| 16 | + private readonly byte[] archivedData; |
| 17 | + private readonly byte[] readBuffer = new byte[1024]; |
| 18 | + |
| 19 | + public TarInputStream() |
| 20 | + { |
| 21 | + using (var outputMemoryStream = new MemoryStream()) |
| 22 | + { |
| 23 | + using (var zipOutputStream = |
| 24 | + new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) |
| 25 | + { |
| 26 | + var tarEntry = TarEntry.CreateTarEntry("some file"); |
| 27 | + tarEntry.Size = 1024 * 1024; |
| 28 | + zipOutputStream.PutNextEntry(tarEntry); |
| 29 | + |
| 30 | + var rng = RandomNumberGenerator.Create(); |
| 31 | + var inputBuffer = new byte[1024]; |
| 32 | + rng.GetBytes(inputBuffer); |
| 33 | + |
| 34 | + for (int i = 0; i < 1024; i++) |
| 35 | + { |
| 36 | + zipOutputStream.Write(inputBuffer, 0, inputBuffer.Length); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + archivedData = outputMemoryStream.ToArray(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [Benchmark] |
| 45 | + public long ReadTarInputStream() |
| 46 | + { |
| 47 | + using (var memoryStream = new MemoryStream(archivedData)) |
| 48 | + using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8)) |
| 49 | + { |
| 50 | + var entry = zipInputStream.GetNextEntry(); |
| 51 | + |
| 52 | + while (zipInputStream.Read(readBuffer, 0, readBuffer.Length) > 0) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + return entry.Size; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Benchmark] |
| 61 | + public async Task<long> ReadTarInputStreamAsync() |
| 62 | + { |
| 63 | + using (var memoryStream = new MemoryStream(archivedData)) |
| 64 | + using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8)) |
| 65 | + { |
| 66 | + var entry = await zipInputStream.GetNextEntryAsync(CancellationToken.None); |
| 67 | + |
| 68 | +#if NETCOREAPP2_1_OR_GREATER |
| 69 | + while (await zipInputStream.ReadAsync(readBuffer.AsMemory()) > 0) |
| 70 | + { |
| 71 | + } |
| 72 | +#else |
| 73 | + while (await zipInputStream.ReadAsync(readBuffer, 0, readBuffer.Length) > 0) |
| 74 | + { |
| 75 | + } |
| 76 | +#endif |
| 77 | + |
| 78 | + return entry.Size; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments