-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathPerf.TarFile.cs
95 lines (82 loc) · 4.02 KB
/
Perf.TarFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Formats.Tar.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.NoWASM)]
public class Perf_TarFile
{
/*
Always exist:
root/
inputdir/
testdir/
file.txt
input.tar
Created on demand:
outputdir/
output.tar
*/
private static readonly string _rootDirPath = FileUtils.GetTestFilePath();
private static readonly string _inputDirPath = Path.Combine(_rootDirPath, "inputdir");
private static readonly string _outputDirPath = Path.Combine(_rootDirPath, "outputdir");
private static readonly string _testDirPath = Path.Combine(_inputDirPath, "testdir");
private static readonly string _testFilePath = Path.Combine(_inputDirPath, "file.txt");
private static readonly string _inputTarFilePath = Path.Combine(_rootDirPath, "input.tar");
private static readonly string _outputTarFilePath = Path.Combine(_rootDirPath, "output.tar");
[GlobalSetup]
public void Setup()
{
Directory.CreateDirectory(_testDirPath); // Creates all segments: root/inputdir/testdir
Directory.CreateDirectory(_outputDirPath);
File.Create(_testFilePath).Dispose();
TarFile.CreateFromDirectory(sourceDirectoryName: _inputDirPath, destinationFileName: _inputTarFilePath, includeBaseDirectory: false);
}
[GlobalCleanup]
public void Cleanup() => Directory.Delete(_rootDirPath, recursive: true);
[Benchmark]
public void CreateFromDirectory_Path()
{
TarFile.CreateFromDirectory(sourceDirectoryName: _inputDirPath, destinationFileName: _outputTarFilePath, includeBaseDirectory: false);
File.Delete(_outputTarFilePath);
}
[Benchmark]
public async Task CreateFromDirectory_Path_Async()
{
await TarFile.CreateFromDirectoryAsync(sourceDirectoryName: _inputDirPath, destinationFileName: _outputTarFilePath, includeBaseDirectory: false);
File.Delete(_outputTarFilePath);
}
[Benchmark]
public void ExtractToDirectory_Path() => TarFile.ExtractToDirectory(sourceFileName: _inputTarFilePath, destinationDirectoryName: _outputDirPath, overwriteFiles: true);
[Benchmark]
public Task ExtractToDirectory_Path_Async() => TarFile.ExtractToDirectoryAsync(sourceFileName: _inputTarFilePath, destinationDirectoryName: _outputDirPath, overwriteFiles: true);
[Benchmark]
public void CreateFromDirectory_Stream()
{
using MemoryStream ms = new MemoryStream();
TarFile.CreateFromDirectory(sourceDirectoryName: _inputDirPath, destination: ms, includeBaseDirectory: false);
}
[Benchmark]
public async Task CreateFromDirectory_Stream_Async()
{
await using MemoryStream ms = new MemoryStream();
await TarFile.CreateFromDirectoryAsync(sourceDirectoryName: _inputDirPath, destination: ms, includeBaseDirectory: false);
}
[Benchmark]
public void ExtractToDirectory_Stream()
{
using FileStream fs = File.OpenRead(_inputTarFilePath);
TarFile.ExtractToDirectory(source: fs, destinationDirectoryName: _outputDirPath, overwriteFiles: true);
}
[Benchmark]
public async Task ExtractToDirectory_Stream_Async()
{
await using FileStream fs = new FileStream(_inputTarFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true);
await TarFile.ExtractToDirectoryAsync(source: fs, destinationDirectoryName: _outputDirPath, overwriteFiles: true);
}
}
}