繁体   English   中英

将文件上载到Azure BLOB存储 - Parallel.Foreach比Foreach慢

[英]Upload files to Azure BLOB storage - Parallel.Foreach is slower than Foreach

我有以下代码用于将文件夹从本地存储上传到blob存储,包括文件夹名称本身在blob的名称中(代码基于这里找到的一些方法http://blog.smarx.com/posts/pivot-odata -and-windows-azure-visual-netflix-browsing ):

public static void UploadBlobDir(CloudBlobContainer container, string dirPath)
        {
            string dirName = new Uri(dirPath).Segments.Last();

            Parallel.ForEach(enumerateDirectoryRecursive(dirPath), file =>
                {
                    string blobName = Path.Combine(dirName, Path.GetFullPath(file)).Substring(dirPath.Length - dirName.Length);
                    container.GetBlobReference(blobName).UploadFile(file);
                });
        }

并且:

private static IEnumerable<string> enumerateDirectoryRecursive(string root)
        {
            foreach (var file in Directory.GetFiles(root))
                yield return file;
            foreach (var subdir in Directory.GetDirectories(root))
                foreach (var file in enumerateDirectoryRecursive(subdir))
                    yield return file;
        }

此代码按预期工作并上传文件夹,但需要花费大量时间才能完成 - 上传 25个文件需要20秒 ,每个文件40KB。 所以我厌倦了用这样的常规循环替换并行循环:

foreach (var file in enumerateDirectoryRecursive(i_DirPath))
            {
                string blobName = Path.Combine(dirName, Path.GetFullPath(file)).Substring(i_DirPath.Length - dirName.Length);
                container.GetBlobReference(blobName).UploadFile(file);
            }

现在上传立即完成( 3秒约)。

同样重要的是要注意我正在使用存储模拟器进行开发。
Parallel.Forech显然应该更快。 这种差异是否来自存储模拟器的限制(当上线时,Parallel会更快)或者是其他我可能做错的事情?

根据我的经验,存储模拟器严格告诉您实际Azure存储中应该(或不存在)的性能。 模拟器通常非常慢。

然后,如果您的传输恰好是延迟限制而不是I / O限制,则Parallel.Foreach将更快。 然后,请注意Parallel.Foreach将仅使用您的CPU数作为默认并行度。 对于延迟限制进程,通常应该有更多的线程,通常每个CPU有4到8个线程(YMMV)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM