我有一个从 Unix FTP 服务器下载文件的应用程序。它工作正常,只是有这个性能问题:大小为 <= 1K 的文件平均需要 2084 到 2400 毫秒来下载,而像 Filezilla 这样的应用程序在不到 1 秒的时间内下载相同的文件(每个文件)。
也许这一次对于一些普通用户来说还可以,但对于我的应用程序来说是不可接受的,因为我需要下载数千个文件。
我尽可能地优化代码: - 用于读取内容的缓存和缓冲区在类的构造函数中创建了 1 次。- 我创建了 1 次网络凭据,并在每次文件下载时重复使用。我知道这是可行的,因为第一个文件需要 7 秒才能下载,所有后续下载都在 2 秒范围内。- 我将缓冲区的大小从 2K 更改为 32K。我不知道这是否有帮助,因为我下载的文件小于 1K,所以理论上缓冲区将在 1 轮中从网络中填充所有信息。
也许与网络无关,但与我编写和/或 Windows 处理文件写入的方式有关?
有人可以给我一些关于如何减少类似于 filezilla 的时间的提示吗?我需要减少时间,否则我的 ftp 将每天 24 小时运行 3 天以完成它的任务 :( 提前非常感谢。这里的代码:它不完整,它只是显示下载部分。
//Create this on the constructor of my class
downloadCache = new MemoryStream(2097152);
downloadBuffer = new byte[32768];
public bool downloadFile(string pRemote, string pLocal, out long donwloadTime)
{
FtpWebResponse response = null;
Stream responseStream = null;
try
{
Stopwatch fileDownloadTime = new Stopwatch();
donwloadTime = 0;
fileDownloadTime.Start();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pRemote);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = false;
request.AuthenticationLevel = AuthenticationLevel.None;
request.EnableSsl = false;
request.Proxy = null;
//I created the credentials 1 time and re-use for every file I need to download
request.Credentials = this.manager.ftpCredentials;
response = (FtpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
downloadCache.Seek(0, SeekOrigin.Begin);
int bytesSize = 0;
int cachedSize = 0;
//create always empty file. Need this because WriteCacheToFile just append the file
using (FileStream fileStream = new FileStream(pLocal, FileMode.Create)) { };
// Download the file until the download is completed.
while (true)
{
bytesSize = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length);
if (bytesSize == 0 || 2097152 < cachedSize + bytesSize)
{
WriteCacheToFile(pLocal, cachedSize);
if (bytesSize == 0)
{
break;
}
downloadCache.Seek(0, SeekOrigin.Begin);
cachedSize = 0;
}
downloadCache.Write(downloadBuffer, 0, bytesSize);
cachedSize += bytesSize;
}
fileDownloadTime.Stop();
donwloadTime = fileDownloadTime.ElapsedMilliseconds;
//file downloaded OK
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
if (response != null)
{
response.Close();
}
if (responseStream != null)
{
responseStream.Close();
}
}
}
private void WriteCacheToFile(string downloadPath, int cachedSize)
{
using (FileStream fileStream = new FileStream(downloadPath, FileMode.Append))
{
byte[] cacheContent = new byte[cachedSize];
downloadCache.Seek(0, SeekOrigin.Begin);
downloadCache.Read(cacheContent, 0, cachedSize);
fileStream.Write(cacheContent, 0, cachedSize);
}
}