Skip to content

Instantly share code, notes, and snippets.

@alextercete
Created November 4, 2013 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alextercete/7309588 to your computer and use it in GitHub Desktop.
Save alextercete/7309588 to your computer and use it in GitHub Desktop.
HttpClient benchmark comparison between disposing and not disposing the instance
class Program
{
private const int CallsToMake = 100;
private const string UrlToCall = "http://www.pudim.com.br";
static void Main(string[] args)
{
Time(DisposingHttpClient, "Disposal of HttpClient instance");
Time(ReusingHttpClient, "Reuse of HttpClient instance");
}
private static void Time(Action action, string title)
{
var watch = Stopwatch.StartNew();
Console.WriteLine("Starting test for '{0}'", title);
action();
Console.WriteLine("Execution ended after {0} ms", watch.ElapsedMilliseconds);
Console.WriteLine();
}
private static void DisposingHttpClient()
{
Parallel.For(0, CallsToMake, i =>
{
using (var client = new HttpClient())
{
client.GetStringAsync(UrlToCall).Wait();
}
});
}
private static void ReusingHttpClient()
{
var client = new HttpClient();
Parallel.For(0, CallsToMake, i => client.GetStringAsync(UrlToCall).Wait());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment