[英]Parallel Chunked vs Sequential Chunked file upload
I am trying to find out the optimal way to upload a large file to the server.我试图找出将大文件上传到服务器的最佳方式。 On my local, I tried both methods:
在本地,我尝试了两种方法:
But here the merging time is way more as compared to upload time, atleast on my local.但是这里的合并时间比上传时间要长得多,至少在我的本地是这样。 Hence, the parallel upload is always a lot slower than the sequential upload.
因此,并行上传总是比顺序上传慢很多。 How can I improve my upload time?
如何缩短上传时间?
Here is the Sequential Upload Code :这是顺序上传代码:
[HttpPost]
[RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
[RequestSizeLimit(209715200)]
public async Task<IActionResult> UploadSeq()
{
string filename = "./Uploads/seqfile";
using(Stream file = Request.Body)
using (FileStream stream = new FileStream(filename, FileMode.Append))
{
await file.CopyToAsync(stream);
await stream.FlushAsync();
}
return Ok();
}
And here is the parallel code :这是并行代码:
[HttpPost("/ParUpload/{id}/{size}")]
[RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
[RequestSizeLimit(209715200)]
public async Task<IActionResult> UploadParallel(int id, int size)
{
string filename = "./Uploads/parfilecomplete.mkv";
using (Stream file = Request.Body)
using (FileStream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
stream.Seek(id * size, SeekOrigin.Begin);
await file.CopyToAsync(stream);
await stream.FlushAsync();
}
return Ok();
}
For large files, the parallel upload is preferred.对于大文件,首选并行上传。 But there are few things to consider
但有几件事需要考虑
Parallel for the win, but you have to do extra work if you want to develop such system.并行取胜,但如果你想开发这样的系统,你必须做额外的工作。
You should not create more than 6 connections per domain (FQDN) since there are connection limits in place on client side.您不应为每个域 (FQDN) 创建超过 6 个连接,因为客户端存在连接限制。 You can increase the limit by using same ip but different domain names like
upload1.domain.com
, upload2.domain.com
, etc.. but then you will probably fill the bandwidth.您可以通过使用相同的 ip 但不同的域名(如
upload1.domain.com
、 upload2.domain.com
等)来增加限制。但是你可能会填满带宽。
You should not open and close file every time a part is received, it is a very expensive operation.您不应在每次收到零件时打开和关闭文件,这是一项非常昂贵的操作。 Instead create a writer class that open the file and you queue received part and the location to write, and writer will write when it can, and closes the file when it writes all parts.
而是创建一个打开文件的编写器类,并将接收到的部分和要写入的位置排队,编写器将在可能时写入,并在写入所有部分时关闭文件。 You would trade disk latency with memory.
你会用内存来交换磁盘延迟。
If you can enable TCP fast start.如果可以启用TCP快速启动。 This will let you send data to your server faster
这将使您可以更快地将数据发送到您的服务器
Choose your buffer size wisely.明智地选择缓冲区大小。 Try with different sizes where it requires minimal disk IO and even less network IO.
在需要最少磁盘 IO 甚至更少网络 IO 的情况下尝试使用不同的大小。 Buffer size must a multiple of disk sector size.
缓冲区大小必须是磁盘扇区大小的倍数。
These are what comes to my mind from my previous experience.这些是我从以前的经历中想到的。 I do not remember exact details but that items should help.
我不记得确切的细节,但这些项目应该会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.