-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathS3Benchmark.cs
More file actions
223 lines (176 loc) · 4.89 KB
/
S3Benchmark.cs
File metadata and controls
223 lines (176 loc) · 4.89 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Amazon.S3.Util;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Minio;
using Minio.DataModel.Args;
using Storage.Benchmark.Utils;
namespace Storage.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[MeanColumn]
[MemoryDiagnoser]
public class S3Benchmark
{
private string _bucket = null!;
private CancellationToken _cancellation;
private InputStream _inputData = null!;
private string _fileId = null!;
private MemoryStream _outputData = null!;
private IAmazonS3 _amazonClient = null!;
private TransferUtility _amazonTransfer = null!;
private IMinioClient _minioClient = null!;
private S3BucketClient _s3Client = null!;
[GlobalSetup]
public void Config()
{
var config = BenchmarkHelper.ReadConfiguration();
var settings = BenchmarkHelper.ReadSettings(config);
_bucket = settings.Bucket;
_cancellation = default;
_fileId = $"привет-как-дела{Guid.NewGuid()}";
_inputData = BenchmarkHelper.ReadBigFile(config);
_outputData = new MemoryStream(new byte[_inputData.Length]);
_amazonClient = BenchmarkHelper.CreateAWSClient(settings);
_amazonTransfer = new TransferUtility(_amazonClient);
_minioClient = BenchmarkHelper.CreateMinioClient(settings);
_s3Client = BenchmarkHelper.CreateStoragesClient(settings);
BenchmarkHelper.EnsureBucketExists(_s3Client, _cancellation);
}
[GlobalCleanup]
public void Clear()
{
_s3Client.Dispose();
_inputData.Dispose();
_outputData.Dispose();
}
[Benchmark]
public async Task<int> Aws()
{
var result = 0;
await AmazonS3Util.DoesS3BucketExistV2Async(_amazonClient, _bucket);
try
{
await _amazonClient.GetObjectMetadataAsync(_bucket, _fileId, _cancellation);
}
catch (Exception)
{
result++; // it's OK - file not found
}
_inputData.Seek(0, SeekOrigin.Begin);
await _amazonTransfer.UploadAsync(_inputData, _bucket, _fileId, _cancellation);
result++;
await _amazonClient.GetObjectMetadataAsync(_bucket, _fileId, _cancellation);
result++;
_outputData.Seek(0, SeekOrigin.Begin);
var fileDownload = await _amazonClient.GetObjectAsync(_bucket, _fileId, _cancellation);
await fileDownload.ResponseStream.CopyToAsync(_outputData, _cancellation);
result++;
await _amazonClient.DeleteObjectAsync(
new DeleteObjectRequest { BucketName = _bucket, Key = _fileId },
_cancellation);
result++;
return result;
}
[Benchmark]
public async Task<int> Minio()
{
var result = 0;
if (!await _minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(_bucket), _cancellation))
{
ThrowException();
}
result++;
try
{
await _minioClient.StatObjectAsync(
new StatObjectArgs()
.WithBucket(_bucket)
.WithObject(_fileId),
_cancellation);
}
catch (Exception)
{
result++; // it's OK - file not found
}
_inputData.Seek(0, SeekOrigin.Begin);
await _minioClient.PutObjectAsync(
new PutObjectArgs()
.WithBucket(_bucket)
.WithObject(_fileId)
.WithObjectSize(_inputData.Length)
.WithStreamData(_inputData)
.WithContentType("application/pdf"),
_cancellation);
result++;
await _minioClient.StatObjectAsync(
new StatObjectArgs()
.WithBucket(_bucket)
.WithObject(_fileId),
_cancellation);
result++;
_outputData.Seek(0, SeekOrigin.Begin);
await _minioClient.GetObjectAsync(
new GetObjectArgs()
.WithBucket(_bucket)
.WithObject(_fileId)
.WithCallbackStream((file, ct) => file.CopyToAsync(_outputData, ct)),
_cancellation);
result++;
await _minioClient.RemoveObjectAsync(
new RemoveObjectArgs()
.WithBucket(_bucket)
.WithObject(_fileId),
_cancellation);
++result;
return result;
}
[Benchmark(Baseline = true)]
public async Task<int> Storage()
{
var result = 0;
var bucketExistsResult = await _s3Client.IsBucketExists(_cancellation);
if (!bucketExistsResult)
{
ThrowException();
}
result++;
var fileExistsResult = await _s3Client.IsFileExists(_fileId, _cancellation);
if (fileExistsResult)
{
ThrowException();
}
result++;
_inputData.Seek(0, SeekOrigin.Begin);
var fileUploadResult = await _s3Client.UploadFile(_fileId, "application/pdf", _inputData, _cancellation);
if (!fileUploadResult)
{
ThrowException();
}
result++;
fileExistsResult = await _s3Client.IsFileExists(_fileId, _cancellation);
if (!fileExistsResult)
{
ThrowException();
}
result++;
_outputData.Seek(0, SeekOrigin.Begin);
var storageFile = await _s3Client.GetFile(_fileId, _cancellation);
if (!storageFile)
{
ThrowException(storageFile.ToString());
}
var fileStream = await storageFile.GetStream(_cancellation);
await fileStream.CopyToAsync(_outputData, _cancellation);
storageFile.Dispose();
result++;
await _s3Client.DeleteFile(_fileId, _cancellation);
result++;
return result;
}
private static void ThrowException(string? message = null)
{
throw new Exception(message);
}
}