This commit is contained in:
2248356998 qq.com
2025-09-28 17:01:19 +08:00
parent 976323a716
commit 2416226eb0
9 changed files with 51 additions and 46 deletions

View File

@@ -1,9 +1,9 @@
<Project>
<PropertyGroup>
<PluginVersion>10.11.77</PluginVersion>
<ProPluginVersion>10.11.77</ProPluginVersion>
<DefaultVersion>10.11.77</DefaultVersion>
<PluginVersion>10.11.78</PluginVersion>
<ProPluginVersion>10.11.78</ProPluginVersion>
<DefaultVersion>10.11.78</DefaultVersion>
<AuthenticationVersion>10.11.6</AuthenticationVersion>
<SourceGeneratorVersion>10.11.6</SourceGeneratorVersion>
<NET8Version>8.0.20</NET8Version>

View File

@@ -73,7 +73,7 @@ public partial class ThingsGatewayBitConverter : IThingsGatewayBitConverter
get => endianType; set
{
endianType = value;
TouchSocketBitConverter = new TouchSocketBitConverter(endianType);
TouchSocketBitConverter = TouchSocketBitConverter.GetBitConverter(endianType);
}
}
@@ -188,13 +188,13 @@ public partial class ThingsGatewayBitConverter : IThingsGatewayBitConverter
// 更新设备地址为去除附加信息后的地址
registerAddress = sb.ToString();
var converter = (IThingsGatewayBitConverter)FastMapper.Mapper(this, type);
// 如果没有解析出任何附加信息,则直接返回默认的数据转换器
if (bcdFormat == null && stringlength == null && encoding == null && dataFormat == null && wstring == null)
{
//MemoryCache.Set(cacheKey, this!, 3600);
return converter;
return this;
}
var converter = (IThingsGatewayBitConverter)FastMapper.Mapper(this, type);
// 根据默认的数据转换器创建新的数据转换器实例

View File

@@ -84,7 +84,7 @@ public class ChannelRuntime : Channel
[Newtonsoft.Json.JsonIgnore]
[MapperIgnore]
[AutoGenerateColumn(Ignore = true)]
internal ConcurrentDictionary<long, DeviceRuntime>? DeviceRuntimes { get; } = new(Environment.ProcessorCount, 1000);
internal ConcurrentDictionary<long, DeviceRuntime>? DeviceRuntimes { get; } = new();
/// <summary>
/// 设备数量

View File

@@ -251,7 +251,7 @@ public class DeviceRuntime : Device
[Newtonsoft.Json.JsonIgnore]
[MapperIgnore]
[AutoGenerateColumn(Ignore = true)]
internal ConcurrentDictionary<string, VariableRuntime>? VariableRuntimes { get; } = new(Environment.ProcessorCount, 1000);
internal ConcurrentDictionary<string, VariableRuntime>? VariableRuntimes { get; } = new();
/// <summary>
/// 特殊方法变量

View File

@@ -36,6 +36,10 @@ namespace ThingsGateway.Foundation;
[MemoryDiagnoser]
public class ModbusBenchmark : IDisposable
{
public static int ClientCount = 2;
public static int TaskNumberOfItems = 4;
public static int NumberOfItems = 4;
private readonly List<IModbusClient> _lgbModbusClients = [];
private List<ModbusMaster> thingsgatewaymodbuss = new();
private List<IModbusMaster> nmodbuss = new();
@@ -45,7 +49,7 @@ public class ModbusBenchmark : IDisposable
[GlobalSetup]
public async Task Init()
{
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var clientConfig = new TouchSocket.Core.TouchSocketConfig();
@@ -65,7 +69,7 @@ public class ModbusBenchmark : IDisposable
}
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var factory = new NModbus.ModbusFactory();
@@ -73,7 +77,7 @@ public class ModbusBenchmark : IDisposable
await nmodbus.ReadHoldingRegistersAsync(1, 0, 100);
nmodbuss.Add(nmodbus);
}
//for (int i = 0; i < Program.ClientCount; i++)
//for (int i = 0; i < ClientCount; i++)
//{
// ModbusTcpNet modbusTcpNet = new();
// modbusTcpNet.IpAddress = "127.0.0.1";
@@ -83,7 +87,7 @@ public class ModbusBenchmark : IDisposable
// modbusTcpNets.Add(modbusTcpNet);
//}
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var client = new ModbusTcpMaster();
await client.SetupAsync(new TouchSocketConfig()
@@ -101,7 +105,7 @@ public class ModbusBenchmark : IDisposable
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<IModbusFactory>();
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var client = factory.GetOrCreateTcpMaster();
await client.ConnectAsync("127.0.0.1", 502);
@@ -120,11 +124,11 @@ public class ModbusBenchmark : IDisposable
foreach (var thingsgatewaymodbus in thingsgatewaymodbuss)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
var result = await thingsgatewaymodbus.ModbusReadAsync(addr).ConfigureAwait(false);
if (!result.IsSuccess)
@@ -146,11 +150,11 @@ public class ModbusBenchmark : IDisposable
foreach (var _lgbModbusClient in _lgbModbusClients)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
using var cts = new CancellationTokenSource(3000);
var task = await _lgbModbusClient.ReadHoldingRegistersAsync(1, 0, 100, cts.Token).ConfigureAwait(false);
@@ -167,11 +171,11 @@ public class ModbusBenchmark : IDisposable
List<Task> tasks = new List<Task>();
foreach (var modbusTcpMaster in modbusTcpMasters)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
var result = await modbusTcpMaster.ReadHoldingRegistersAsync(0, 100).ConfigureAwait(false);
var data = TouchSocketBitConverter.ConvertValues<byte, ushort>(result.Data.Span, EndianType.Little);
@@ -193,11 +197,11 @@ public class ModbusBenchmark : IDisposable
List<Task> tasks = new List<Task>();
foreach (var nmodbus in nmodbuss)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
var result = await nmodbus.ReadHoldingRegistersAsync(1, 0, 100).ConfigureAwait(false);
}
@@ -215,11 +219,11 @@ public class ModbusBenchmark : IDisposable
// List<Task> tasks = new List<Task>();
// foreach (var modbusTcpNet in modbusTcpNets)
// {
// for (int i = 0; i < Program.TaskNumberOfItems; i++)
// for (int i = 0; i < TaskNumberOfItems; i++)
// {
// tasks.Add(Task.Run(async () =>
// {
// for (int i = 0; i < Program.NumberOfItems; i++)
// for (int i = 0; i < NumberOfItems; i++)
// {
// var result = await modbusTcpNet.ReadAsync("0", 100);
// if (!result.IsSuccess)

View File

@@ -29,6 +29,10 @@ namespace ThingsGateway.Foundation;
[MemoryDiagnoser]
public class S7Benchmark : IDisposable
{
public static int ClientCount = 5;
public static int TaskNumberOfItems = 1;
public static int NumberOfItems = 5;
private List<SiemensS7Master> siemensS7s = new();
private List<Plc> plcs = new();
@@ -39,7 +43,7 @@ public class S7Benchmark : IDisposable
{
{
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var clientConfig = new TouchSocket.Core.TouchSocketConfig();
@@ -56,14 +60,14 @@ public class S7Benchmark : IDisposable
await siemensS7.ReadAsync("M1", 100);
siemensS7s.Add(siemensS7);
}
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, "127.0.0.1");
await siemensS7Net.ConnectServerAsync();
await siemensS7Net.ReadAsync("M0", 100);
siemensS7Nets.Add(siemensS7Net);
}
for (int i = 0; i < Program.ClientCount; i++)
for (int i = 0; i < ClientCount; i++)
{
var plc = new Plc(CpuType.S71500, "127.0.0.1", 102, 0, 0);
await plc.OpenAsync();//打开plc连接
@@ -79,11 +83,11 @@ public class S7Benchmark : IDisposable
List<Task> tasks = new List<Task>();
foreach (var plc in plcs)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
var result = await plc.ReadAsync(DataType.Memory, 1, 0, VarType.Byte, 100);
}
@@ -99,11 +103,11 @@ public class S7Benchmark : IDisposable
List<Task> tasks = new List<Task>();
foreach (var siemensS7Net in siemensS7Nets)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
var result = await siemensS7Net.ReadAsync("M0", 100);
if (!result.IsSuccess)
@@ -129,11 +133,11 @@ public class S7Benchmark : IDisposable
List<Task> tasks = new List<Task>();
foreach (var siemensS7 in siemensS7s)
{
for (int i = 0; i < Program.TaskNumberOfItems; i++)
for (int i = 0; i < TaskNumberOfItems; i++)
{
tasks.Add(Task.Run(async () =>
{
for (int i = 0; i < Program.NumberOfItems; i++)
for (int i = 0; i < NumberOfItems; i++)
{
var result = await siemensS7.S7ReadAsync(siemensS7Address);
if (!result.IsSuccess)

View File

@@ -20,14 +20,11 @@ namespace BenchmarkConsoleApp
{
internal class Program
{
public static int ClientCount = 50;
public static int TaskNumberOfItems = 6;
public static int NumberOfItems = 50;
private static async Task Main(string[] args)
{
Console.WriteLine("开始测试前请先启动ModbusSlave建议使用本项目自带的ThingsGateway.Debug.Photino软件开启S7可以用KEPSERVER的S7模拟服务");
Console.WriteLine($"多客户端({ClientCount}),多线程({TaskNumberOfItems})并发读取({NumberOfItems})测试,共{ClientCount * TaskNumberOfItems * NumberOfItems}次");
await Task.CompletedTask;
//ModbusBenchmark modbusBenchmark = new ModbusBenchmark();
//System.Diagnostics.Stopwatch stopwatch = new();
@@ -46,15 +43,15 @@ namespace BenchmarkConsoleApp
//.WithOptions(ConfigOptions.DisableOptimizationsValidator)
//);
BenchmarkRunner.Run<MapperBench>(
ManualConfig.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
);
// BenchmarkRunner.Run<ModbusBenchmark>(
// ManualConfig.Create(DefaultConfig.Instance)
// .WithOptions(ConfigOptions.DisableOptimizationsValidator)
// BenchmarkRunner.Run<MapperBench>(
//ManualConfig.Create(DefaultConfig.Instance)
//.WithOptions(ConfigOptions.DisableOptimizationsValidator)
//);
BenchmarkRunner.Run<ModbusBenchmark>(
ManualConfig.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
);
// BenchmarkRunner.Run<S7Benchmark>(
//ManualConfig.Create(DefaultConfig.Instance)
//.WithOptions(ConfigOptions.DisableOptimizationsValidator)

View File

@@ -45,7 +45,7 @@ public class SQLHistoryValue : IPrimaryIdEntity
///<summary>
///实时值
///</summary>
[SugarColumn(ColumnDescription = "实时值")]
[SugarColumn(ColumnDescription = "实时值", ColumnDataType = StaticConfig.CodeFirst_BigString, IsNullable = true)]
[AutoGenerateColumn(Order = 23, Visible = true, Sortable = true, Filterable = false)]
public string Value { get; set; }

View File

@@ -44,7 +44,7 @@ public class SQLRealValue : IPrimaryIdEntity
///<summary>
///实时值
///</summary>
[SugarColumn(ColumnDescription = "实时值")]
[SugarColumn(ColumnDescription = "实时值", ColumnDataType = StaticConfig.CodeFirst_BigString, IsNullable = true)]
[AutoGenerateColumn(Order = 21, Visible = true, Sortable = true, Filterable = false)]
public string Value { get; set; }