Ste*_*tes 6 .net c# azure azure-storage azure-blob-storage
批量上传文件Azure Blob存储的最快方法是什么?我试过两种方法,sync并async上传,async显然是最快的,但我不知道是否有更好的方法?是否内置了对批量上传的支持?我在文档中找不到任何内容但可能错过了它.
这是我跑的测试:
static void Main(string[] args)
{
int totalFiles = 10; //10, 50, 100
byte[] randomData = new byte[2097152]; //2mb
for (int i = 0; i < randomData.Length; i++)
{
randomData[i] = 255;
}
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
var blobClient = cloudStorageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("something");
container.CreateIfNotExists();
TimeSpan tsSync = Test1(totalFiles, randomData, container);
TimeSpan tsAsync = Test2(totalFiles, randomData, container);
Console.WriteLine($"Sync: {tsSync}");
Console.WriteLine($"Async: {tsAsync}");
Console.ReadLine();
}
public static TimeSpan Test2(int total, byte[] data, CloudBlobContainer container)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Task[] tasks = new Task[total];
for (int i = 0; i < total; i++)
{
CloudBlockBlob blob = container.GetBlockBlobReference(Guid.NewGuid().ToString());
tasks[i] = blob.UploadFromByteArrayAsync(data, 0, data.Length);
}
Task.WaitAll(tasks);
sw.Stop();
return sw.Elapsed;
}
public static TimeSpan Test1(int total, byte[] data, CloudBlobContainer container)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < total; i++)
{
CloudBlockBlob blob = container.GetBlockBlobReference(Guid.NewGuid().ToString());
blob.UploadFromByteArray(data, 0, data.Length);
}
sw.Stop();
return sw.Elapsed;
}
Run Code Online (Sandbox Code Playgroud)
这个输出是:
Sync: 00:00:08.7251781
Async: 00:00:04.7553491
DMLib: 00:00:05.1961654
Sync: 00:00:08.1169861
Async: 00:00:05.2384105
DMLib: 00:00:05.4955403
Sync: 00:00:07.6122464
Async: 00:00:05.0495365
DMLib: 00:00:06.4714047
Run Code Online (Sandbox Code Playgroud)
Sync: 00:00:39.1595797
Async: 00:00:22.5757347
DMLib: 00:00:25.2897623
Sync: 00:00:40.4932800
Async: 00:00:22.3296490
DMLib: 00:00:26.0631829
Sync: 00:00:39.2879245
Async: 00:00:24.0746697
DMLib: 00:00:26.9243116
Run Code Online (Sandbox Code Playgroud)
我希望这是一个有效的问题.
谢谢
编辑:
我已经用"DMLib"测试更新了结果,以回应到目前为止给出的答案.DMLib是一个没有配置更改的测试(见上文)没有性能提升
我ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;按照文档的推荐运行了一些测试,这增加了相当多的上传速度,但它也提高了我的异步方法的上传速度.到目前为止,DMlib还没有给我任何值得的性能提升.我在下面的配置更改中添加了第二组测试结果.
我也设置ServicePointManager.Expect100Continue = false;了这个速度没有区别.
ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;Sync: 00:00:07.6199307
Async: 00:00:02.9615565
DMLib: 00:00:02.6629716
Sync: 00:00:08.7721797
Async: 00:00:02.8246599
DMLib: 00:00:02.7281091
Sync: 00:00:07.8437682
Async: 00:00:03.0171246
DMLib: 00:00:03.0190045
Run Code Online (Sandbox Code Playgroud)
Sync: 00:00:40.2395863
Async: 00:00:10.3157544
DMLib: 00:00:10.5107740
Sync: 00:00:40.2473358
Async: 00:00:10.8190161
DMLib: 00:00:10.2585441
Sync: 00:00:41.2646137
Async: 00:00:13.7188085
DMLib: 00:00:10.8686173
Run Code Online (Sandbox Code Playgroud)
我是否错误地使用了库,因为它似乎没有提供比我自己的方法更好的性能.
| 归档时间: |
|
| 查看次数: |
3529 次 |
| 最近记录: |