Created
March 27, 2012 19:57
-
-
Save sanjayuttam/2219725 to your computer and use it in GitHub Desktop.
Parallel HTTP Requestrrrrrrrrrrr...very simple and hacked together quickly. don't use in production systems.
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Globalization; | |
using System.Linq; | |
using System.Net; | |
using System.Threading.Tasks; | |
namespace SocketRequestor40 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<string> theUrls = new List<string>() | |
{ | |
"http://www.someUrlThatRedirectsYou.com", //should return a 301 or 302 | |
"http://www.someNonRedirect.com" //should return a 200 | |
}; | |
new UrlRequester().HitUrls(theUrls); | |
} | |
private class UrlRequester | |
{ | |
public int maxRequests { get; set; } | |
public UrlRequester() | |
{ | |
maxRequests = 10; | |
} | |
public UrlRequester(int maxNumRequests) | |
{ | |
maxRequests = maxNumRequests; | |
} | |
public void HitUrls(List<string> urls) | |
{ | |
int n = urls.Count(); | |
var totalTimes = new long[n]; | |
Parallel.For(0, n, i => | |
{ | |
for (int request = 0; request < maxRequests; request++) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
try | |
{ | |
sw.Start(); | |
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urls[i]); | |
webRequest.AllowAutoRedirect = false; | |
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); | |
sw.Stop(); | |
totalTimes[i] += sw.ElapsedMilliseconds; | |
int responseCode = (int)response.StatusCode; | |
bool isRedirect = responseCode == (int)HttpStatusCode.Redirect || responseCode == (int)HttpStatusCode.TemporaryRedirect; | |
Console.WriteLine(urls[i] + " returned a: " + responseCode + " " + response.StatusCode.ToString() + | |
((isRedirect == true) ? " redirected to: " + response.Headers["Location"].ToString(CultureInfo.InvariantCulture) : string.Empty) | |
); | |
} | |
catch (WebException we) | |
{ | |
sw.Stop(); | |
totalTimes[i] += sw.ElapsedMilliseconds; | |
Console.WriteLine("Error Response: " + (int)((HttpWebResponse)we.Response).StatusCode + " " + ((HttpWebResponse)we.Response).StatusCode.ToString()); | |
} | |
} | |
}); | |
var grandTotalTime = totalTimes.Sum(); | |
var reqsPerSec = (double)(n * maxRequests * 1000) / (double)grandTotalTime; | |
Console.WriteLine("Requests per second: {0}", reqsPerSec); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment