Been on SO for a very long time, new to this site. I am creating console app which tests API endpoint performance using json files from the specified directory as request bodies. Request json files themselves are not large ~5KB but large number of them. This is what relevant logic pieces look like. Need feedback if it's a viable way of loading all json files from a testing directory and ways to improve it if any.
- This is a class to hold file json data:
public readonly struct AppFileInfo
{
public AppFileInfo(string fullPath, long size)
{
FullPath = fullPath;
Name = Path.GetFileNameWithoutExtension(fullPath);
}
public AppFileInfo(FileInfo fileInfo)
{
FullPath = fileInfo.FullName;
Name = Path.GetFileNameWithoutExtension(fileInfo.Name);
using var buffer = new ArrayPoolBufferWriter<byte>(ArrayPool<byte>.Shared, 8192);
using var writer = new Utf8JsonWriter(buffer);
using var stream = File.OpenRead(fileInfo.FullName);
using var document = JsonDocument.Parse(stream);
document.WriteTo(writer);
Contents = buffer.GetMemory();
}
public string Name { get; init; }
public string FullPath { get; init; }
public ReadOnlyMemory<byte> Contents { get; init; }
public ReadOnlySpan<byte> ContentSpan => Contents.Span;
};
- This is how I would use that object to parse specified Directory:
public class RequestFileReader
{
private AppConfiguration _appConfig;
public RequestFileReader(IOptions<AppConfiguration> appConfig)
{
_appConfig = appConfig.Value;
}
public List<AppFileInfo> LoadRequestsFromDir(string dir)
{
List<AppFileInfo> requestFiles = [];
var fileInfos = new DirectoryInfo(dir).EnumerateFiles().Where(f => f.Extension == ".json");
foreach (var fileInfo in fileInfos)
{
var appFileInfo = new AppFileInfo(fileInfo);
requestFiles.Add(appFileInfo);
}
return requestFiles;
}
}
- This is how I would use that json data:
public async Task<List<TestResulta>> Run(string responsesPath)
{
var testRequests = _requestFileReader.LoadRequestsFromDir(appConfig.GetMismatchTestPath());
var requestChunks =
testRequests.Chunk(_appConfig.ChunkSize).ToList();
foreach (var chunk in requestChunks)
{
var tasks = chunk.Select(r => CreateResponseTask(r,
responsesPath));
await foreach (var task in Task.WhenEach(tasks))
{
testResults.Add(await task);
}
await Task.Delay(_appConfig.ChunkInterval);
return testResults;
}
public async Task<MismatchTestComparisonResult> CreateResponseTask(AppFileInfo request, string responsesPath)
{
HttpRequestMessage requestMessage = new HttpRequestMessage()
{
Method = HttpMethod.Post,
RequestUri = new Uri(uri),
Content = new ReadOnlyMemoryContent(request) { Headers = {
ContentType = new("application/json") } }
};
var client = _httpClientFactory.CreateClient();
try
{
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new
client.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new
AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.Timeout = TimeSpan.FromMinutes(appConfig.ClientTimeout);
}
var response = await client.SendAsync(requestMessage);
}
Any feedback/suggestions especially performance improvements and memory optimizations greatly appreciated.