DD.WellWorkover.Cloud/ConsoleApp1/ControllerLoadTester.cs
2021-12-12 17:10:09 +05:00

131 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}