简体   繁体   English

如何测量C#中每一行的执行时间

[英]How do I measure execution time for each row in C#

I have the following Selenium test: 我有以下硒测试:

[TestMethod]
public void Edit()
{
    Driver.GoToAdministrera();

    Driver.ClickLink(strings.ActorSettings);

    Driver.SendKeysToId(text, "Attributes");

    Driver.Navigate().Refresh();

    Driver.AssertTextInId(string.Empty, "Attributes");

    Driver.SendKeysToId(text, "Attributes");

    Driver.ClickClass("submitbutton");

    Thread.Sleep(3000);

    Driver.Navigate().Refresh();

    Driver.AssertTextInId(text, "Attributes");
}

It is slow and I want to analyze where the bottleneck is. 它很慢,我想分析瓶颈在哪里。 I know I could profile the test, but that will give me some long stack-trace and hotspots. 我知道我可以描述测试,但是那会给我一些漫长的堆栈跟踪和热点。 I just want to know how long the execution time is for each row. 我只想知道每一行的执行时间。

I know I can use the StopWatch class. 我知道我可以使用StopWatch类。 I can even do something like a help method for this: 我什至可以为此做一些帮助方法:

protected static void Time(Action action)
{
    var sw = new Stopwatch();
    sw.Reset();
    sw.Start();

    action();

    sw.Stop();
    Console.WriteLine(sw.Elapsed + " " + action.Method.Name);
}

But this requires me to modify my test. 但这需要我修改测试。

Does anyone know of a method to get the timings for each row in a piece of code executed? 有谁知道一种方法来获取一段代码中每一行的计时?

DateTime start = DateTime.Now;
// Do Processing
Driver.GoToAdministrera();
DateTime end = DateTime.Now;
Console.WriteLine("Time taken : " + (end - start));

If you use Visual Studio 2015 or later you can run the test under debugger and look for PerfTips values. 如果您使用Visual Studio 2015或更高版本,则可以在调试器下运行测试,并查找PerfTips值。 Stepping from line to line you will get estimated value of elapsed time for each line of the test which is probably what you need. 从一行到另一行,您将获得测试的每行经过时间的估计值,这可能是您所需要的。

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

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