Last active
May 20, 2022 23:54
-
-
Save johnbabb/dece7950c91669e3b709 to your computer and use it in GitHub Desktop.
C# http performance test
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
string baseUrl = "http://localhost"; | |
void Main() | |
{ | |
var urls = new string[] { | |
"/url/endpoint", | |
}; | |
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); | |
}; | |
} |
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
string baseUrl = "http://localhost"; | |
string path = "/some/url"; | |
private HttpClient _client = new HttpClient(); | |
void Main() | |
{ | |
Console.WriteLine("Start {0}", DateTime.Now.TimeOfDay); | |
RunAsync().Wait(); | |
Console.WriteLine("Stop {0}", DateTime.Now.TimeOfDay); | |
} | |
private async Task RunAsync() | |
{ | |
var messageHandler = new HttpClientHandler { UseDefaultCredentials = true }; | |
using (HttpClient client = new HttpClient(messageHandler)) | |
{ | |
await RunTimedTest(client, baseUrl + path, "application/json"); | |
} | |
} | |
public async Task RunTimedTest(HttpClient _client, string Uri, string mediaHeader) | |
{ | |
int iterations = 500; | |
_client.DefaultRequestHeaders.Accept.Clear(); | |
_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(mediaHeader)); | |
var watch = Stopwatch.StartNew(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
var result = await _client.GetAsync(Uri); | |
var value = await result.Content.ReadAsStringAsync(); | |
} | |
Console.WriteLine("Format: {0,-20}\t Time (s):{1,10:ss\\.fff}", mediaHeader, watch.Elapsed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment