From 0adbdb926b5f984dcee2b05d893edd6adfedcb91 Mon Sep 17 00:00:00 2001 From: "2248356998 qq.com" <2248356998@qq.com> Date: Fri, 12 Sep 2025 23:49:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9F=BA=E5=87=86=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Benchmark/ModbusBenchmark.cs | 74 +++++++++++++++---- .../ThingsGateway.Foundation.Benchmark.csproj | 1 + 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/Plugin/ThingsGateway.Foundation.Benchmark/Benchmark/ModbusBenchmark.cs b/src/Plugin/ThingsGateway.Foundation.Benchmark/Benchmark/ModbusBenchmark.cs index 6a7c49385..349c753ff 100644 --- a/src/Plugin/ThingsGateway.Foundation.Benchmark/Benchmark/ModbusBenchmark.cs +++ b/src/Plugin/ThingsGateway.Foundation.Benchmark/Benchmark/ModbusBenchmark.cs @@ -13,7 +13,11 @@ using BenchmarkConsoleApp; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Diagnosers; -using HslCommunication.ModBus; + +using Longbow.Modbus; +using Longbow.TcpSocket; + +using Microsoft.Extensions.DependencyInjection; using System.IO.Pipelines; using System.Net.Sockets; @@ -31,9 +35,10 @@ namespace ThingsGateway.Foundation; [MemoryDiagnoser] public class ModbusBenchmark : IDisposable { + private readonly List _lgbModbusClients = []; private List thingsgatewaymodbuss = new(); private List nmodbuss = new(); - private List modbusTcpNets = new(); + //private List modbusTcpNets = new(); private List modbusTcpMasters = new(); private PipeOptions GetNoDelayPipeOptions() { @@ -77,15 +82,15 @@ public class ModbusBenchmark : IDisposable nmodbus.ReadHoldingRegistersAsync(1, 0, 100).GetFalseAwaitResult(); nmodbuss.Add(nmodbus); } - for (int i = 0; i < Program.ClientCount; i++) - { - ModbusTcpNet modbusTcpNet = new(); - modbusTcpNet.IpAddress = "127.0.0.1"; - modbusTcpNet.Port = 502; - modbusTcpNet.ConnectServer(); - modbusTcpNet.ReadAsync("0", 100).GetFalseAwaitResult(); - modbusTcpNets.Add(modbusTcpNet); - } + //for (int i = 0; i < Program.ClientCount; i++) + //{ + // ModbusTcpNet modbusTcpNet = new(); + // modbusTcpNet.IpAddress = "127.0.0.1"; + // modbusTcpNet.Port = 502; + // modbusTcpNet.ConnectServer(); + // modbusTcpNet.ReadAsync("0", 100).GetFalseAwaitResult(); + // modbusTcpNets.Add(modbusTcpNet); + //} for (int i = 0; i < Program.ClientCount; i++) { @@ -96,8 +101,46 @@ public class ModbusBenchmark : IDisposable client.ReadHoldingRegistersAsync(0, 100).GetFalseAwaitResult(); modbusTcpMasters.Add(client); } - } + + { + var sc = new ServiceCollection(); + sc.AddTcpSocketFactory(); + sc.AddModbusFactory(); + + var provider = sc.BuildServiceProvider(); + var factory = provider.GetRequiredService(); + + for (int i = 0; i < Program.ClientCount; i++) + { + var client = factory.GetOrCreateTcpMaster(); + client.ConnectAsync("127.0.0.1", 502).GetAwaiter().GetResult(); + client.ReadHoldingRegistersAsync(0x01, 0x00, 10).GetAwaiter().GetResult(); + + _lgbModbusClients.Add(client); + } + } + } + [Benchmark] + public async Task LongbowModbus() + { + List tasks = new List(); + foreach (var _lgbModbusClient in _lgbModbusClients) + { + + for (int i = 0; i < Program.TaskNumberOfItems; i++) + { + tasks.Add(Task.Run(async () => + { + for (int i = 0; i < Program.NumberOfItems; i++) + { + var task = await _lgbModbusClient.ReadHoldingRegistersAsync(1, 0, 10); + } + })); + } + } + await Task.WhenAll(tasks); + } [Benchmark] public async Task ThingsGateway() { @@ -117,6 +160,7 @@ public class ModbusBenchmark : IDisposable { throw new Exception(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + result.ToString()); } + var data = TouchSocketBitConverter.ConvertValues(result.Content.Span, EndianType.Little); } })); } @@ -139,6 +183,7 @@ public class ModbusBenchmark : IDisposable for (int i = 0; i < Program.NumberOfItems; i++) { var result = await modbusTcpMaster.ReadHoldingRegistersAsync(0, 100); + var data = TouchSocketBitConverter.ConvertValues(result.Data.Span, EndianType.Little); if (!result.IsSuccess) { throw new Exception(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + result.ToString()); @@ -199,9 +244,12 @@ public class ModbusBenchmark : IDisposable public void Dispose() { + thingsgatewaymodbuss?.ForEach(a => a.Channel.SafeDispose()); thingsgatewaymodbuss?.ForEach(a => a.SafeDispose()); nmodbuss?.ForEach(a => a.SafeDispose()); - modbusTcpNets?.ForEach(a => a.SafeDispose()); + //modbusTcpNets?.ForEach(a => a.SafeDispose()); + _lgbModbusClients?.ForEach(a => a.DisposeAsync().GetAwaiter().GetResult()); } + } \ No newline at end of file diff --git a/src/Plugin/ThingsGateway.Foundation.Benchmark/ThingsGateway.Foundation.Benchmark.csproj b/src/Plugin/ThingsGateway.Foundation.Benchmark/ThingsGateway.Foundation.Benchmark.csproj index d117bd34c..acbab3228 100644 --- a/src/Plugin/ThingsGateway.Foundation.Benchmark/ThingsGateway.Foundation.Benchmark.csproj +++ b/src/Plugin/ThingsGateway.Foundation.Benchmark/ThingsGateway.Foundation.Benchmark.csproj @@ -43,6 +43,7 @@ +