在少数情况下,我们在负载测试期间记录了“ 0”毫秒,但在开发测试期间无法创建此问题。任何人都有 遇到相同的问题,并愿意知道是否有最佳实践 使用秒表。
private Stopwatch stopWatch = new Stopwatch();
[HttpGet]
[Route("/Test")]
public string TestStopWatch(bool throwExcetion)
{
stopWatch.Start();
try
{
Thread.Sleep(2 * 1000);
if (throwExcetion == true) throw new Exception("exception thrown");
return "Method executed successfully";
}
catch (Exception ex)
{
return "Method is thrown exception";
}
finally
{
stopWatch.Stop();
Console.WriteLine($" Elapsed Time: {stopWatch.ElapsedMilliseconds.ToString()}");
stopWatch.Reset();
}
}