I'm trying to understand a performance issue I'm having when creating new AppDomains and executing HttpWebRequests in these.
Have a look at my Host.exe:
private static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
AppDomain domain = AppDomain.CreateDomain("Foo");
try
{
Console.WriteLine(domain.ExecuteAssembly("Child.exe"));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
AppDomain.Unload(domain);
}
}
}
And my Child.exe:
public static int Main(string[] args)
{
Stopwatch sw = Stopwatch.StartNew();
var request = (HttpWebRequest) WebRequest.Create("http://127.0.0.1");
using (WebResponse response = request.GetResponse())
{
}
sw.Stop();
return (int) sw.ElapsedMilliseconds; // heh
}
Output is:
20
847
849
... and so on. As you can see, the first request in the first AppDomain is pretty fast. Then things start to go bad. There are two great delays when calling WebRequest.Create()
and request.GetResponse()
(kinda like 50/50 of 800 ms). They also keep my CPU spinning, steady at 12% (I'm on eight cores). Subsequent requests in one AppDomain are then perfectly fine.
What I'm looking for is the "plumbing" going on. I've seen some hits on Google and SO considering the performance of the initial WebRequest itself, proxy lookup (should not apply here) and hitting connection limits (I dispose of the response). What I really don't understand, is why the first request in the first AppDomain behaves so differently.