Created
November 4, 2013 21:37
-
-
Save alextercete/7309588 to your computer and use it in GitHub Desktop.
HttpClient benchmark comparison between disposing and not disposing the instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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