forked from ddrilling/AsbCloudServer
nit refactor ControllerLoadTester
This commit is contained in:
parent
6925746da9
commit
dc636a1d8c
@ -1,80 +0,0 @@
|
||||
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;
|
||||
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
public static class ControllerLoadService
|
||||
{
|
||||
public class ControllerLoadTester
|
||||
{
|
||||
//Пример роута: "api/telemetryDataSaub/123123_";
|
||||
|
||||
|
||||
public static void TestControllerRoute(CancellationToken token = default)
|
||||
{
|
||||
var tasks = new List<Task>();
|
||||
|
||||
for (var i = 0; i < 30; i++)
|
||||
{
|
||||
tasks.Add(SendRequestsAsync(i, token));
|
||||
};
|
||||
|
||||
Task.WaitAll(tasks.ToArray(), token);
|
||||
|
||||
Console.WriteLine("End");
|
||||
}
|
||||
|
||||
private static async Task SendRequestsAsync(int i, CancellationToken token)
|
||||
{
|
||||
var rand = new Random();
|
||||
|
||||
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 <= 10; j++)
|
||||
{
|
||||
await Task.Delay(rand.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()
|
||||
{
|
||||
var json = JsonSerializer.Serialize(new List<TelemetryDataSaubDto>()
|
||||
{
|
||||
new TelemetryDataSaubDto()
|
||||
});
|
||||
|
||||
var stringContent = new StringContent(json, Encoding.UTF8,
|
||||
"application/json");
|
||||
return stringContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
131
ConsoleApp1/ControllerLoadTester.cs
Normal file
131
ConsoleApp1/ControllerLoadTester.cs
Normal file
@ -0,0 +1,131 @@
|
||||
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;
|
||||
|
||||
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<Task>();
|
||||
|
||||
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<TelemetryDataSaubDto>()
|
||||
{
|
||||
MakeTelemetryDataSaubDto(date)
|
||||
});
|
||||
|
||||
var stringContent = new StringContent(json, Encoding.UTF8,
|
||||
"application/json");
|
||||
return stringContent;
|
||||
}
|
||||
|
||||
private static TelemetryDataSaubDto MakeTelemetryDataSaubDto(DateTime date = default)
|
||||
{
|
||||
var dto = new TelemetryDataSaubDto
|
||||
{
|
||||
Date = 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ namespace ConsoleApp1
|
||||
{
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
DbDemoDataService.AddDemoData();
|
||||
ControllerLoadTester.TestControllerRoute();
|
||||
//.GetAwaiter().GetResult();
|
||||
|
||||
Console.WriteLine("End of Test");
|
||||
|
Loading…
Reference in New Issue
Block a user