77 lines
No EOL
2.1 KiB
C#
77 lines
No EOL
2.1 KiB
C#
using System.Net.Http.Json;
|
|
using BenchmarkDotNet.Attributes;
|
|
using StitchATon2.Domain;
|
|
|
|
namespace StitchATon2.Benchmark;
|
|
|
|
public class SingleTileBenchmark
|
|
{
|
|
private const string Url = "http://localhost:5088/api/image/generate";
|
|
private readonly TileManager _tileManager = new(Configuration.Default);
|
|
private readonly Random _random = new();
|
|
private readonly HttpClient _client = new();
|
|
|
|
private string GetRandomCoordinatePair()
|
|
{
|
|
var maxId = _tileManager.Configuration.Rows
|
|
* _tileManager.Configuration.Columns;
|
|
|
|
var id = _random.Next(maxId);
|
|
var tile = _tileManager.GetTile(id);
|
|
return $"{tile.Coordinate}:{tile.Coordinate}";
|
|
}
|
|
|
|
private JsonContent GetRandomDto(float scale, float offsetX = 0, float offsetY = 0)
|
|
{
|
|
return JsonContent.Create(new
|
|
{
|
|
canvas_rect = GetRandomCoordinatePair(),
|
|
crop_offset = (float[]) [offsetX, offsetY],
|
|
crop_size = (float[]) [1f - offsetX, 1f - offsetY],
|
|
output_scale = scale,
|
|
});
|
|
}
|
|
|
|
private JsonContent GetRandomDtoWithOffset(float scale)
|
|
{
|
|
var offsetX = _random.NextSingle();
|
|
var offsetY = _random.NextSingle();
|
|
return GetRandomDto(scale, offsetX, offsetY);
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task NoScaling()
|
|
{
|
|
await _client.PostAsync(Url, GetRandomDto(1f));
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task ScaleHalf()
|
|
{
|
|
await _client.PostAsync(Url, GetRandomDto(.5f));
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task ScaleQuarter()
|
|
{
|
|
await _client.PostAsync(Url, GetRandomDto(.25f));
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task NoScalingWithOffset()
|
|
{
|
|
await _client.PostAsync(Url, GetRandomDtoWithOffset(1f));
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task ScaleHalfWithOffset()
|
|
{
|
|
await _client.PostAsync(Url, GetRandomDtoWithOffset(.5f));
|
|
}
|
|
|
|
[Benchmark]
|
|
public async Task ScaleQuarterWithOffset()
|
|
{
|
|
await _client.PostAsync(Url, GetRandomDtoWithOffset(.25f));
|
|
}
|
|
} |