Measure cpu intensive time with async code

204 views Asked by At

we want to outsource some part of our application to an external company. These components make async requests to external websites and handle the content with html parsers or regular expressions.

async Task Do()
{
   var webContent = await Get("http://...");
   var match = Regex.Match(webContent);

   if (IsXXX(match))
   {
       webContent = await Get("http://other...");
   }
}

Each component is different, some make only one request, others do a lot more. We want to ensure that the CPU intensive part (Regex, Parsing) does not take a longer than 100ms and want to test it automatically as first step in the quality ensurance pipeline. Is there a way to measure the performance without the waiting time for the web requests?

I see only two solutions at the moment, but would like to know if there is better approach:

  1. Provide an wrapper for the web requests and measure the waiting time. Then use simple stopwatch to measure the time and subtract the waiting time. Difficult for soap calls when using generated client classes.
  2. Implement a custom synchronization context and measure the time here.

Are there some builtin solutions?

1

There are 1 answers

0
Bas On

Mock the IO-bound web requests to immediately return an HTML string that will take similar CPU to process. E.g. use return Task.FromResult(XXX) to get a task that is already completed. Then measure the method as a whole.

Note that you can use Microsoft Fakes to replace any internal call with your own method.