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; using AsbCloudApp.Data.SAUB; namespace ConsoleApp1 { public static class ControllerLoadTester { static readonly Random random = new((int)DateTime.Now.Ticks); public static void TestControllerRoute(CancellationToken token = default) { var tasks = new List(); for (var i = 0; i < 1; i++) { tasks.Add(SendRequestsAsync(i, token)); }; Task.WaitAll(tasks.ToArray(), token); Console.WriteLine("End"); } private static async Task SendRequestsAsync(int i, CancellationToken token) { var uid = $"123123_1"; using var httpClient = new HttpClient(); Console.WriteLine($"Sending telemetry info with uid: {uid}"); var tInfoUri = new Uri($"http://localhost:5000/api/telemetry/{uid}/info"); var infoResponse = await httpClient.PostAsync(tInfoUri, MakeTelemetryInfoContent(), token); Console.WriteLine($"Передана telemetry info с: {infoResponse.StatusCode}"); for (var j = 0; j <= 100; j++) { await Task.Delay(random.Next(1000, 5000), token); var dataSaubUri = new Uri($"http://localhost:5000/api/telemetryDataSaub/{uid}"); var saubResponse = await httpClient.PostAsync(dataSaubUri, MakeDataSaubContent(), token); Console.WriteLine(saubResponse.StatusCode); } } 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(DateTime date = default) { var json = JsonSerializer.Serialize(new List() { MakeTelemetryDataSaubDto(date) }); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); return stringContent; } private static TelemetryDataSaubDto MakeTelemetryDataSaubDto(DateTime date = default) { var dto = new TelemetryDataSaubDto { DateTime = date == default ? DateTime.Now : date, User = "Вупсень", Mode = 1, //ротор IdFeedRegulator = 1, MseState = 1, WellDepth = MakeFloatRandom(100), BitDepth = MakeFloatRandom(100), BlockPosition = MakeFloatRandom(100), BlockPositionMin = MakeFloatRandom(100), BlockPositionMax = MakeFloatRandom(100), BlockSpeed = MakeFloatRandom(100), BlockSpeedSp = MakeFloatRandom(100), BlockSpeedSpRotor = MakeFloatRandom(100), BlockSpeedSpSlide = MakeFloatRandom(100), BlockSpeedSpDevelop = MakeFloatRandom(100), Pressure = MakeFloatRandom(100), PressureIdle = MakeFloatRandom(100), PressureSp = MakeFloatRandom(100), PressureSpRotor = MakeFloatRandom(100), PressureSpSlide = MakeFloatRandom(100), PressureSpDevelop = MakeFloatRandom(100), PressureDeltaLimitMax = MakeFloatRandom(100), AxialLoad = MakeFloatRandom(100), AxialLoadSp = MakeFloatRandom(100), AxialLoadLimitMax = MakeFloatRandom(100), HookWeight = MakeFloatRandom(100), HookWeightIdle = MakeFloatRandom(100), HookWeightLimitMin = MakeFloatRandom(100), HookWeightLimitMax = MakeFloatRandom(100), RotorTorque = MakeFloatRandom(100), RotorTorqueIdle = MakeFloatRandom(100), RotorTorqueSp = MakeFloatRandom(100), RotorTorqueLimitMax = MakeFloatRandom(100), RotorSpeed = MakeFloatRandom(100), Flow = MakeFloatRandom(100), FlowIdle = MakeFloatRandom(100), FlowDeltaLimitMax = MakeFloatRandom(100), }; return dto; } private static float MakeFloatRandom( float max, float min = 0) { var val = (float)( min + random.NextDouble() * (max - min)); return val; } private static float MakeFloatSin( float max, float min, float angle) { var val = (float)(min + Math.Sin(angle) * (max - min) / 2); return val; } } }