可以';我找不到时间花在哪里
本文关键字:时间 在哪里 找不到 可以 | 更新日期: 2023-09-27 18:23:57
我有一个函数,它可以获取FTP文件夹中的文件列表。
功能:
public static List<string> GetFileList(NetworkCredential credential, string FTPSite, string FTPFolder, string extension)
{
string LSOutput = "";
List<string> files = new List<string>();
bool isSuccess = false;
int retrier = 1;
while (isSuccess == false && retrier <= MaxRetries)
{
try
{
CommonHelper.PrepareLogAndEmail("--Attempt " + retrier + ":", LogMessageType.Simple);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPSite + FTPFolder);
request.Timeout = 120000;
request.Credentials = credential;
request.UseBinary = true;
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = null;
response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
LSOutput = reader.ReadToEnd();
reader.Close();
response.Close();
CommonHelper.PrepareLogAndEmail("--Succeeded", LogMessageType.Simple);
isSuccess = true;
}
catch (Exception ex)
{
CommonHelper.PrepareLogAndEmail("--Failed. Details: " + ex.Message, LogMessageType.Error);
}
retrier++;
}
if (!isSuccess)
throw new Exception("All attempts failed");
else
{
//Parse the LS
string[] LSOutputLines = LSOutput.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (string LSOutputLine in LSOutputLines)
files.Add(LSOutputLine);
//Filter files
files = files.Where(f => f.ToLower().EndsWith(extension.ToLower())).ToList();
PrepareLogAndEmail("Total " + extension.ToLower() + " files found: " + files.Count, LogMessageType.Simple);
return files;
}
}
以下是它的名称:
//-------------------------------------------------------//
startTime = DateTime.Now;
try
{
CommonHelper.PrepareLogAndEmail("Step 1: FIND FILES ON THE FTP FOLDER", CommonHelper.LogMessageType.StepStart);
filesOnFolder = CommonHelper.GetFileList(credential, FTPSite, FTPFolder, ".pgp");
if (filesOnFolder.Count == 0)
{
CommonHelper.PrepareLogAndEmail("Atleast one file was expected but not found", CommonHelper.LogMessageType.Simple);
CommonHelper.PrepareLogAndEmail("Step 1: COMPLETED", CommonHelper.LogMessageType.StepComplete);
CommonHelper.PrepareLogAndEmail("(Took " + (DateTime.Now - startTime).TotalSeconds + " seconds to complete)", CommonHelper.LogMessageType.TimeTaken);
CommonHelper.PrepareLogAndEmail(HeaderMessage, CommonHelper.LogMessageType.Header);
CommonHelper.SendMail(CommonHelper.EmailSubjectType.Informative);
Environment.Exit(1);
}
else
{
CommonHelper.PrepareLogAndEmail("Step 1: COMPLETED", CommonHelper.LogMessageType.StepComplete);
CommonHelper.PrepareLogAndEmail("(Took " + (DateTime.Now - startTime).TotalSeconds + " seconds to complete)", CommonHelper.LogMessageType.TimeTaken);
}
}
catch (Exception ex)
{
CommonHelper.PrepareLogAndEmail("An exception occured. Details: " + ex.Message, CommonHelper.LogMessageType.Error);
CommonHelper.PrepareLogAndEmail("Step 1: COMPLETED WITH ERRORS", CommonHelper.LogMessageType.StepComplete);
CommonHelper.PrepareLogAndEmail("(Took " + (DateTime.Now - startTime).TotalSeconds + " seconds to complete)", CommonHelper.LogMessageType.TimeTaken);
CommonHelper.PrepareLogAndEmail(HeaderMessage, CommonHelper.LogMessageType.Header);
CommonHelper.SendMail(CommonHelper.EmailSubjectType.Bad);
Environment.Exit(1);
}
//-------------------------------------------------------//
在某些情况下,我得到的输出为:
Step 1: FIND FILES ON THE FTP FOLDER
--Attempt 1:
--Succeeded
Total .pgp files found: 2
Step 1: COMPLETED
(Took 4.2742904 seconds to complete)
其中一个输出是这样的:
Step 1: FIND FILES ON THE FTP FOLDER
--Attempt 1:
--Failed. Details: The operation has timed out
--Attempt 2:
--Succeeded
Total .pgp files found: 0
Atleast one file was expected but not found
Step 1: COMPLETED
(Took 370.6597195 seconds to complete)
第一次尝试:120秒
第二次尝试(最坏情况):119秒
总计:239秒
额外时间(370-239):131秒
我不确定这131秒是在哪里度过的
PrepareLogAndEmail=将字符串记录到文本文件的函数
最大重试次数=3
首先使用System.Diagnostics.Stopwatch来记录您的计时。
我看到在GetFileList
中的catch
语句之后有以下内容:
Console.WriteLine("First retry (GetFileList)");
Console.ReadKey();
retrier++;
在等待用户输入时,计时器将继续运行。是否可能第一次尝试失败,您花了131秒才按下命令窗口中的一个键?
使用System.Diagnostics.Stopwatch而不是DateTime;
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
// Do something
// Stop timing
stopwatch.Stop();
// Result
var elapsedTime = stopwatch.Elapsed;