|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using ICSharpCode.SharpZipLib.Zip; |
| 7 | + |
| 8 | +namespace ICSharpCode.SharpZipLib.Benchmark.Zip |
| 9 | +{ |
| 10 | + [MemoryDiagnoser] |
| 11 | + [Config(typeof(MultipleRuntimes))] |
| 12 | + public class ZipFile |
| 13 | + { |
| 14 | + private readonly byte[] readBuffer = new byte[4096]; |
| 15 | + private string zipFileWithLargeAmountOfEntriesPath; |
| 16 | + |
| 17 | + [GlobalSetup] |
| 18 | + public async Task GlobalSetup() |
| 19 | + { |
| 20 | + SharpZipLibOptions.InflaterPoolSize = 4; |
| 21 | + |
| 22 | + // large real-world test file from test262 repository |
| 23 | + string commitSha = "2e4e0e6b8ebe3348a207144204cb6d7a5571c863"; |
| 24 | + zipFileWithLargeAmountOfEntriesPath = Path.Combine(Path.GetTempPath(), $"{commitSha}.zip"); |
| 25 | + if (!File.Exists(zipFileWithLargeAmountOfEntriesPath)) |
| 26 | + { |
| 27 | + var uri = $"https://github.com/tc39/test262/archive/{commitSha}.zip"; |
| 28 | + |
| 29 | + Console.WriteLine("Loading test262 repository archive from {0}", uri); |
| 30 | + |
| 31 | + using (var client = new HttpClient()) |
| 32 | + { |
| 33 | + using (var downloadStream = await client.GetStreamAsync(uri)) |
| 34 | + { |
| 35 | + using (var writeStream = File.OpenWrite(zipFileWithLargeAmountOfEntriesPath)) |
| 36 | + { |
| 37 | + await downloadStream.CopyToAsync(writeStream); |
| 38 | + Console.WriteLine("File downloaded and saved to {0}", zipFileWithLargeAmountOfEntriesPath); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + [Benchmark] |
| 47 | + public void ReadLargeZipFile() |
| 48 | + { |
| 49 | + using (var file = new SharpZipLib.Zip.ZipFile(zipFileWithLargeAmountOfEntriesPath)) |
| 50 | + { |
| 51 | + foreach (ZipEntry entry in file) |
| 52 | + { |
| 53 | + using (var stream = file.GetInputStream(entry)) |
| 54 | + { |
| 55 | + while (stream.Read(readBuffer, 0, readBuffer.Length) > 0) |
| 56 | + { |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments