I wrote a script to test how long it takes to make requests against a url. It takes a list of urls, an array of concurrent requests, and a number of times to attempt the test. How could I improve on this?
//https://www.nuget.org/packages/RestSharp
string baseUrl = "http://localhost";
void Main()
{
var urls = new string[] {
"/some/end/point",
};
var concurrentRequests = new int[]{ 1, 5, 25, 50, 100, 500, 1000};
var numberOfTurns = 10;
RunTests(urls, concurrentRequests, numberOfTurns);
}
private void RunTests(string[] urls, int[] concurrentRequests, int numberOfTimes = 1)
{
for(var i = 0; i < numberOfTimes; i++)
{
RunFor(urls, concurrentRequests);
}
}
private void RunFor(string[] urls, int[] concurrentRequests)
{
concurrentRequests.ToList().ForEach( e =>
{
RunTest(urls, e);
});
}
private void RunTest(string[] sites, int iterations, string description = "")
{
var action = GetAction();
var watch = new Stopwatch();
// Construct started tasks
Task<bool>[] tasks = new Task<bool>[sites.Count()];
watch.Start();
for(int j = 0; j < iterations; j++)
{
for (int i = 0; i < sites.Count(); i++)
{
tasks[i] = Task<bool>.Factory.StartNew(action, sites[i]);
}
}
try
{
Task.WaitAll(tasks);
}
catch (AggregateException e)
{
Console.WriteLine("\nThe following exceptions have been thrown by WaitAll()");
for (int j = 0; j < e.InnerExceptions.Count; j++)
{
Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
}
}
finally
{
watch.Stop();
Console.WriteLine("\"{0}|{1}|{2}\", ",sites.Count(), iterations, watch.Elapsed.TotalSeconds);
}
}
private Func<object, bool> GetAction()
{
baseUrl = baseUrl.Trim('/');
return (object obj) =>
{
var str = (string)obj;
var client = new RestClient(baseUrl);
client.Authenticator = new NtlmAuthenticator();
var request = new RestRequest(str, Method.GET);
request.AddHeader("Accept", "text/html");
var response = client.Execute(request);
return (response != null);
};
}