Created
November 21, 2024 02:19
-
-
Save Aaronontheweb/10a58996318455ccdb8068dead0ab19b to your computer and use it in GitHub Desktop.
LINQPad HTTP Load Testing
This file contains hidden or 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
async Task Main() | |
{ | |
// Configuration | |
const string url = "Your URL here"; // Replace with your URL | |
const int numberOfRequests = 2000; // Adjust the number of requests as needed | |
// Data storage | |
var tasks = new List<Task<RequestResult>>(); | |
// HTTP Client setup | |
using var httpClient = new HttpClient(); | |
// Fire requests in parallel | |
for (int i = 0; i < numberOfRequests; i++) | |
{ | |
tasks.Add(SendRequestAsync(httpClient, url)); | |
} | |
// Wait for all requests to complete and collect results | |
var results = await Task.WhenAll(tasks); | |
// Visualizations | |
results.Dump("Raw Data"); | |
// | |
// // Pie chart: Breakdown by response code | |
// results | |
// .GroupBy(r => r.StatusCode) | |
// .Select(g => new { StatusCode = g.Key, Count = g.Count() }) | |
// .Chart(c => c.StatusCode, c => c.Count, LINQPad.Util.SeriesType.Pie) | |
// .Dump(); | |
// | |
// // Line chart: Average latency over time | |
// results | |
// .GroupBy(r => r.Timestamp.Second) | |
// .Select(g => new | |
// { | |
// Second = g.Key, | |
// AverageLatency = g.Average(r => r.Latency.TotalMilliseconds) | |
// }) | |
// .OrderBy(x => x.Second) | |
// .Chart(c =>c.Second,c =>c.AverageLatency, LINQPad.Util.SeriesType.Line) | |
// .Dump(); | |
} | |
// Helper method for sending an HTTP GET request | |
private async Task<RequestResult> SendRequestAsync(HttpClient httpClient, string url) | |
{ | |
var startTime = DateTime.UtcNow; | |
try | |
{ | |
var response = await httpClient.GetAsync(url); | |
var endTime = DateTime.UtcNow; | |
return new RequestResult | |
{ | |
Timestamp = endTime, | |
Latency = endTime - startTime, | |
StatusCode = response.StatusCode, | |
Route = url, | |
ErrorMessage = null | |
}; | |
} | |
catch (HttpRequestException ex) | |
{ | |
var endTime = DateTime.UtcNow; // Record the time even for errors | |
return new RequestResult | |
{ | |
Timestamp = endTime, | |
Latency = endTime - startTime, | |
StatusCode = HttpStatusCode.InternalServerError, // Example fallback for errors | |
Route = url, | |
ErrorMessage = ex.Message | |
}; | |
} | |
catch (Exception ex) | |
{ | |
var endTime = DateTime.UtcNow; // Record the time even for errors | |
return new RequestResult | |
{ | |
Timestamp = endTime, | |
Latency = endTime - startTime, | |
StatusCode = HttpStatusCode.ServiceUnavailable, // Example fallback for unexpected exceptions | |
Route = url, | |
ErrorMessage = ex.Message | |
}; | |
} | |
} | |
// Data model | |
public class RequestResult | |
{ | |
public DateTime Timestamp { get; set; } | |
public TimeSpan Latency { get; set; } | |
public HttpStatusCode StatusCode { get; set; } | |
public string Route { get; set; } | |
public string ErrorMessage { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Commented out the charts because they were less useful than just looking at the raw data table