2021-09-28 15:47:26 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
|
2021-09-28 18:02:12 +05:00
|
|
|
|
namespace AsbCloudDevOperations
|
2021-09-28 15:47:26 +05:00
|
|
|
|
{
|
|
|
|
|
public class ControllerLoadTester
|
|
|
|
|
{
|
2021-10-01 15:27:54 +05:00
|
|
|
|
private readonly Random rand = new();
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-09-30 16:41:00 +05:00
|
|
|
|
//Пример роута: "api/telemetryDataSaub/123123_";
|
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
|
|
|
|
|
public void TestControllerRoute(CancellationToken token = default)
|
2021-09-28 15:47:26 +05:00
|
|
|
|
{
|
2021-10-01 15:27:54 +05:00
|
|
|
|
var tasks = new List<Task>();
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 30; i++)
|
|
|
|
|
{
|
|
|
|
|
tasks.Add(SendRequestsAsync(i, token));
|
|
|
|
|
};
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
Task.WaitAll(tasks.ToArray(), token);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("End");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SendRequestsAsync(int i, CancellationToken token)
|
|
|
|
|
{
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
var uid = $"123123_1";
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-04 15:41:47 +05:00
|
|
|
|
using var httpClient = new HttpClient();
|
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
Console.WriteLine($"Sending telemetry info with uid: {uid}");
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
var tInfoUri = new Uri($"http://localhost:5000/api/telemetry/{uid}/info");
|
2021-10-04 18:07:42 +05:00
|
|
|
|
var infoResponse = await httpClient.PostAsync(tInfoUri, MakeTelemetryInfoContent(), token);
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
Console.WriteLine($"Передана telemetry info с: {infoResponse.StatusCode}");
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
for (var j = 0; j <= 10; j++)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(rand.Next(1000, 5000), token);
|
|
|
|
|
var dataSaubUri = new Uri($"http://localhost:5000/api/telemetryDataSaub/{uid}");
|
2021-10-04 18:07:42 +05:00
|
|
|
|
var saubResponse = await httpClient.PostAsync(dataSaubUri, MakeDataSaubContent(), token);
|
2021-09-28 15:47:26 +05:00
|
|
|
|
|
2021-10-01 15:27:54 +05:00
|
|
|
|
Console.WriteLine(saubResponse.StatusCode);
|
|
|
|
|
}
|
2021-09-28 15:47:26 +05:00
|
|
|
|
}
|
2021-10-04 15:54:01 +05:00
|
|
|
|
|
|
|
|
|
private static StringContent MakeTelemetryInfoContent()
|
|
|
|
|
{
|
|
|
|
|
var json = JsonSerializer.Serialize(new TelemetryInfoDto{ Cluster = "Cluster" });
|
|
|
|
|
|
|
|
|
|
var stringContent = new StringContent(json, Encoding.UTF8,
|
|
|
|
|
"application/json");
|
|
|
|
|
return stringContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static StringContent MakeDataSaubContent()
|
|
|
|
|
{
|
|
|
|
|
var json = JsonSerializer.Serialize(new List<TelemetryDataSaubDto>()
|
|
|
|
|
{
|
|
|
|
|
new TelemetryDataSaubDto()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var stringContent = new StringContent(json, Encoding.UTF8,
|
|
|
|
|
"application/json");
|
|
|
|
|
return stringContent;
|
|
|
|
|
}
|
2021-09-28 15:47:26 +05:00
|
|
|
|
}
|
|
|
|
|
}
|