Browse Source

修改客户端的压测工具主要关心(收发数是否一致、连接数是否断开)

tags/v2.2.2
smallchi 5 years ago
parent
commit
4988e99f13
12 changed files with 107 additions and 128 deletions
  1. +1
    -1
      simples/JT808.DotNetty.SimpleClient/Services/UpService.cs
  2. +3
    -1
      src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj
  3. +0
    -1
      src/JT808.DotNetty.CleintBenchmark/Program.cs
  4. +1
    -1
      src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkHostedService.cs
  5. +0
    -55
      src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkReportHostedService.cs
  6. +22
    -2
      src/JT808.DotNetty.Client/JT808ClientDotnettyExtensions.cs
  7. +2
    -2
      src/JT808.DotNetty.Client/JT808DeviceConfig.cs
  8. +25
    -0
      src/JT808.DotNetty.Client/JT808ReportOptions.cs
  9. +2
    -2
      src/JT808.DotNetty.Client/JT808TcpClient.cs
  10. +2
    -2
      src/JT808.DotNetty.Client/JT808TcpClientFactory.cs
  11. +49
    -19
      src/JT808.DotNetty.Client/Services/JT808ReportHostedService.cs
  12. +0
    -42
      src/JT808.DotNetty.Client/Services/JT808ReportService.cs

+ 1
- 1
simples/JT808.DotNetty.SimpleClient/Services/UpService.cs View File

@@ -20,7 +20,7 @@ namespace JT808.DotNetty.SimpleClient.Services


public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
{ {
JT808TcpClient client1 = jT808TcpClientFactory.Create(new DeviceConfig("12345678910", "127.0.0.1", 808));
JT808TcpClient client1 = jT808TcpClientFactory.Create(new JT808DeviceConfig("12345678910", "127.0.0.1", 808));
//1.终端注册 //1.终端注册
client1.Send(new JT808_0x0100() client1.Send(new JT808_0x0100()
{ {


+ 3
- 1
src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj View File

@@ -2,7 +2,7 @@


<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>7.3</LangVersion> <LangVersion>7.3</LangVersion>
</PropertyGroup> </PropertyGroup>


@@ -34,4 +34,6 @@
</None> </None>
</ItemGroup> </ItemGroup>


<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>

</Project> </Project>

+ 0
- 1
src/JT808.DotNetty.CleintBenchmark/Program.cs View File

@@ -43,7 +43,6 @@ namespace JT808.DotNetty.CleintBenchmark
services.AddJT808Configure() services.AddJT808Configure()
.AddJT808Client(); .AddJT808Client();
services.AddHostedService<CleintBenchmarkHostedService>(); services.AddHostedService<CleintBenchmarkHostedService>();
services.AddHostedService<CleintBenchmarkReportHostedService>();
}); });
await serverHostBuilder.RunConsoleAsync(); await serverHostBuilder.RunConsoleAsync();
} }


+ 1
- 1
src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkHostedService.cs View File

@@ -49,7 +49,7 @@ namespace JT808.DotNetty.CleintBenchmark.Services
{ {
taskFactory.StartNew((item) => taskFactory.StartNew((item) =>
{ {
var client = jT808TcpClientFactory.Create(new DeviceConfig(((int)item).ToString(), clientBenchmarkOptions.IP, clientBenchmarkOptions.Port));
var client = jT808TcpClientFactory.Create(new JT808DeviceConfig(((int)item+1).ToString(), clientBenchmarkOptions.IP, clientBenchmarkOptions.Port));
int lat = new Random(1000).Next(100000, 180000); int lat = new Random(1000).Next(100000, 180000);
int Lng = new Random(1000).Next(100000, 180000); int Lng = new Random(1000).Next(100000, 180000);
while (!cts.IsCancellationRequested) while (!cts.IsCancellationRequested)


+ 0
- 55
src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkReportHostedService.cs View File

@@ -1,55 +0,0 @@
using JT808.DotNetty.CleintBenchmark.Configs;
using JT808.DotNetty.Client;
using JT808.DotNetty.Client.Services;
using JT808.Protocol.MessageBody;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace JT808.DotNetty.CleintBenchmark.Services
{
public class CleintBenchmarkReportHostedService : IHostedService
{
private readonly JT808ReportService jT808ReportService;

private CancellationTokenSource cts=new CancellationTokenSource();

private readonly ILogger logger;
public CleintBenchmarkReportHostedService(
ILoggerFactory loggerFactory,
JT808ReportService jT808ReportService)
{
this.jT808ReportService = jT808ReportService;
logger = loggerFactory.CreateLogger("CleintBenchmarkReportHostedService");
}
public Task StartAsync(CancellationToken cancellationToken)
{
logger.LogInformation("StartAsync...");
Task.Run(() => {
while (!cts.IsCancellationRequested)
{
logger.LogInformation(JsonConvert.SerializeObject(jT808ReportService.JT808Reports.LastOrDefault()));
Thread.Sleep(3000);
}
}, cts.Token);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
logger.LogInformation("StopAsync...");
cts.Cancel();
logger.LogInformation("正在生成报表...");
logger.LogInformation(JsonConvert.SerializeObject(jT808ReportService.JT808Reports,Formatting.Indented));
return Task.CompletedTask;
}
}
}

+ 22
- 2
src/JT808.DotNetty.Client/JT808ClientDotnettyExtensions.cs View File

@@ -4,17 +4,37 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using JT808.Protocol; using JT808.Protocol;
using Microsoft.Extensions.Configuration;


namespace JT808.DotNetty.Client namespace JT808.DotNetty.Client
{ {
public static class JT808ClientDotnettyExtensions public static class JT808ClientDotnettyExtensions
{ {
public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder)
public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder)
{ {
jT808Builder.Services.AddSingleton<JT808SendAtomicCounterService>(); jT808Builder.Services.AddSingleton<JT808SendAtomicCounterService>();
jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>(); jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>();
jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>(); jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>();
jT808Builder.Services.AddSingleton<JT808ReportService>();
jT808Builder.Services.Configure<JT808ReportOptions>((options)=> { });
jT808Builder.Services.AddHostedService<JT808ReportHostedService>();
return jT808Builder;
}
public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder, IConfiguration Configuration)
{
jT808Builder.Services.AddSingleton<JT808SendAtomicCounterService>();
jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>();
jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>();
jT808Builder.Services.Configure<JT808ReportOptions>(Configuration.GetSection("JT808ReportOptions"));
jT808Builder.Services.AddHostedService<JT808ReportHostedService>();
return jT808Builder;
}

public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder, Action<JT808ReportOptions> reportAction)
{
jT808Builder.Services.AddSingleton<JT808SendAtomicCounterService>();
jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>();
jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>();
jT808Builder.Services.Configure(reportAction);
jT808Builder.Services.AddHostedService<JT808ReportHostedService>(); jT808Builder.Services.AddHostedService<JT808ReportHostedService>();
return jT808Builder; return jT808Builder;
} }


src/JT808.DotNetty.Client/DeviceConfig.cs → src/JT808.DotNetty.Client/JT808DeviceConfig.cs View File

@@ -6,9 +6,9 @@ using System.Text;


namespace JT808.DotNetty.Client namespace JT808.DotNetty.Client
{ {
public class DeviceConfig
public class JT808DeviceConfig
{ {
public DeviceConfig(string terminalPhoneNo, string tcpHost,int tcpPort)
public JT808DeviceConfig(string terminalPhoneNo, string tcpHost,int tcpPort)
{ {
TerminalPhoneNo = terminalPhoneNo; TerminalPhoneNo = terminalPhoneNo;
TcpHost = tcpHost; TcpHost = tcpHost;

+ 25
- 0
src/JT808.DotNetty.Client/JT808ReportOptions.cs View File

@@ -0,0 +1,25 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace JT808.DotNetty.Client
{
public class JT808ReportOptions:IOptions<JT808ReportOptions>
{
public string FileName { get; set; } = $"JT808Report.{DateTime.Now.ToString("yyyyMMddHHssmm")}.txt";
public string FilePath { get; set; } = AppDomain.CurrentDomain.BaseDirectory;
public string FileFullPath { get { return Path.Combine(FilePath, FileName); } }
public int Interval { get; set; } = 3;
public JT808ReportOptions Value => this;

public void FileExistsAndCreate()
{
if(!File.Exists(FileFullPath))
{
File.Create(FileFullPath).Close();
}
}
}
}

+ 2
- 2
src/JT808.DotNetty.Client/JT808TcpClient.cs View File

@@ -25,11 +25,11 @@ namespace JT808.DotNetty.Client


private bool disposed = false; private bool disposed = false;


public DeviceConfig DeviceConfig { get; private set; }
public JT808DeviceConfig DeviceConfig { get; private set; }


public ILoggerFactory LoggerFactory { get; private set; } public ILoggerFactory LoggerFactory { get; private set; }


public JT808TcpClient(DeviceConfig deviceConfig, IServiceProvider serviceProvider)
public JT808TcpClient(JT808DeviceConfig deviceConfig, IServiceProvider serviceProvider)
{ {
DeviceConfig = deviceConfig; DeviceConfig = deviceConfig;
LoggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>(); LoggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();


+ 2
- 2
src/JT808.DotNetty.Client/JT808TcpClientFactory.cs View File

@@ -9,7 +9,7 @@ namespace JT808.DotNetty.Client
{ {
public interface IJT808TcpClientFactory : IDisposable public interface IJT808TcpClientFactory : IDisposable
{ {
JT808TcpClient Create(DeviceConfig deviceConfig);
JT808TcpClient Create(JT808DeviceConfig deviceConfig);


List<JT808TcpClient> GetAll(); List<JT808TcpClient> GetAll();
} }
@@ -26,7 +26,7 @@ namespace JT808.DotNetty.Client
this.serviceProvider = serviceProvider; this.serviceProvider = serviceProvider;
} }


public JT808TcpClient Create(DeviceConfig deviceConfig)
public JT808TcpClient Create(JT808DeviceConfig deviceConfig)
{ {
if(dict.TryGetValue(deviceConfig.TerminalPhoneNo,out var client)) if(dict.TryGetValue(deviceConfig.TerminalPhoneNo,out var client))
{ {


+ 49
- 19
src/JT808.DotNetty.Client/Services/JT808ReportHostedService.cs View File

@@ -1,37 +1,67 @@
using Microsoft.Extensions.Hosting;
using JT808.DotNetty.Client.Metadata;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;


namespace JT808.DotNetty.Client.Services namespace JT808.DotNetty.Client.Services
{ {
public class JT808ReportHostedService : IHostedService
public class JT808ReportHostedService : BackgroundService
{ {
private readonly JT808ReportService jT808ReportService;
private CancellationTokenSource cts = new CancellationTokenSource();
public JT808ReportHostedService(JT808ReportService jT808ReportService)
private readonly IOptionsMonitor<JT808ReportOptions> jT808ReportOptions;
private readonly JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService;
private readonly JT808SendAtomicCounterService jT808SendAtomicCounterService;
private readonly IJT808TcpClientFactory jT808TcpClientFactory;
private readonly ILogger logger;

public JT808ReportHostedService(
ILoggerFactory loggerFactory,
IOptionsMonitor<JT808ReportOptions> jT808ReportOptionsAccessor,
JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService,
JT808SendAtomicCounterService jT808SendAtomicCounterService,
IJT808TcpClientFactory jT808TcpClientFactory)
{ {
this.jT808ReportService = jT808ReportService;
logger = loggerFactory.CreateLogger("JT808ReportHostedService");
jT808ReportOptions = jT808ReportOptionsAccessor;
jT808ReportOptions.CurrentValue.FileExistsAndCreate();
this.jT808ReceiveAtomicCounterService = jT808ReceiveAtomicCounterService;
this.jT808SendAtomicCounterService = jT808SendAtomicCounterService;
this.jT808TcpClientFactory = jT808TcpClientFactory;
jT808ReportOptions.OnChange((options) => { options.FileExistsAndCreate(); });
} }
public Task StartAsync(CancellationToken cancellationToken)

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
Task.Run(() =>
while (!stoppingToken.IsCancellationRequested)
{ {
while (!cts.IsCancellationRequested)
var clients = jT808TcpClientFactory.GetAll();
JT808Report report = new JT808Report()
{ {
jT808ReportService.Create();
Thread.Sleep(1000);
//Task.Delay(TimeSpan.FromSeconds(1), cts.Token);
SendTotalCount = jT808SendAtomicCounterService.MsgSuccessCount,
ReceiveTotalCount = jT808ReceiveAtomicCounterService.MsgSuccessCount,
CurrentDate = DateTime.Now,
Connections = clients.Count,
OnlineConnections = clients.Where(w => w.IsOpen).Count(),
OfflineConnections = clients.Where(w => !w.IsOpen).Count(),
};
string json = JsonConvert.SerializeObject(report);
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug(json);
} }
}, cts.Token);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
cts.Cancel();
return Task.CompletedTask;
using (var sw=new StreamWriter(jT808ReportOptions.CurrentValue.FileFullPath,true))
{
sw.WriteLine(json);
}
await Task.Delay(TimeSpan.FromSeconds(jT808ReportOptions.CurrentValue.Interval), stoppingToken);
}
} }
} }
} }

+ 0
- 42
src/JT808.DotNetty.Client/Services/JT808ReportService.cs View File

@@ -1,42 +0,0 @@
using JT808.DotNetty.Client.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JT808.DotNetty.Client.Services
{
public class JT808ReportService
{
private readonly JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService;
private readonly JT808SendAtomicCounterService jT808SendAtomicCounterService;
private readonly IJT808TcpClientFactory jT808TcpClientFactory;

public List<JT808Report> JT808Reports { get; private set; }

public JT808ReportService(
JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService,
JT808SendAtomicCounterService jT808SendAtomicCounterService,
IJT808TcpClientFactory jT808TcpClientFactory)
{
this.jT808ReceiveAtomicCounterService = jT808ReceiveAtomicCounterService;
this.jT808SendAtomicCounterService = jT808SendAtomicCounterService;
this.jT808TcpClientFactory = jT808TcpClientFactory;
JT808Reports = new List<JT808Report>();
}

public void Create()
{
var clients = jT808TcpClientFactory.GetAll();
JT808Reports.Add(new JT808Report()
{
SendTotalCount= jT808SendAtomicCounterService.MsgSuccessCount,
ReceiveTotalCount= jT808ReceiveAtomicCounterService.MsgSuccessCount,
CurrentDate=DateTime.Now,
Connections= clients.Count,
OnlineConnections= clients.Where(w => w.IsOpen).Count(),
OfflineConnections= clients.Where(w => !w.IsOpen).Count(),
});
}
}
}

Loading…
Cancel
Save