Skip to content

Commit

Permalink
增加了Fantasy.Benchmark基准测试功能。具体请看Fantasy.Benchmark/README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
qq362946 committed Oct 1, 2024
1 parent 89641f2 commit fc799ad
Show file tree
Hide file tree
Showing 303 changed files with 488 additions and 71 deletions.
8 changes: 7 additions & 1 deletion Fanatsy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Tools.ExporterNetwo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Net", "Fantasy.Net\Fantasy.Net\Fantasy.Net.csproj", "{4782A7D1-2B43-48DD-AF50-EDC773436E20}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Console", "Fantays.Console\Fantasy.Console\Fantasy.Console.csproj", "{4E463740-00D2-4B4F-AC5B-A09B24487D35}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Console", "Fantays.Console\Fantasy.Console.csproj", "{4E463740-00D2-4B4F-AC5B-A09B24487D35}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{0CEB6A4D-8CE9-4EA7-A918-61FA62D6F6F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Tools.ConfigTable", "Tools\SourceCode\Fantasy.Tools.ConfigTable\Fantasy.Tools.ConfigTable.csproj", "{A93DFC08-E57F-4BBF-BAAA-18FDB458D515}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Tools.NetworkProtocol", "Tools\SourceCode\Fantasy.Tools.NetworkProtocol\Fantasy.Tools.NetworkProtocol.csproj", "{1951BC8A-0B3E-42F8-950B-DC142988B225}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fantasy.Benchmark", "Fantasy.Benchmark\Fantasy.Benchmark.csproj", "{C228C5CC-81B9-4323-B4BD-F3C1FB129676}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -73,5 +75,9 @@ Global
{1951BC8A-0B3E-42F8-950B-DC142988B225}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1951BC8A-0B3E-42F8-950B-DC142988B225}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1951BC8A-0B3E-42F8-950B-DC142988B225}.Release|Any CPU.Build.0 = Release|Any CPU
{C228C5CC-81B9-4323-B4BD-F3C1FB129676}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C228C5CC-81B9-4323-B4BD-F3C1FB129676}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C228C5CC-81B9-4323-B4BD-F3C1FB129676}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C228C5CC-81B9-4323-B4BD-F3C1FB129676}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions Fanatsy.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AKCPClientNetwork_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F345510aa2adc4dc692470d34421b7bf380400_003F47_003F946135ef_003FKCPClientNetwork_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
138 changes: 138 additions & 0 deletions Fantasy.Benchmark/ConsoleLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

namespace Fantasy
{
/// <summary>
/// 标准的控制台Log
/// </summary>
public sealed class ConsoleLog : ILog
{

/// <summary>
/// 记录跟踪级别的日志消息。
/// </summary>
/// <param name="message">日志消息。</param>
public void Trace(string message)
{
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.WriteLine(message);
}

/// <summary>
/// 记录警告级别的日志消息。
/// </summary>
/// <param name="message">日志消息。</param>
public void Warning(string message)
{
System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.WriteLine(message);
}

/// <summary>
/// 记录信息级别的日志消息。
/// </summary>
/// <param name="message">日志消息。</param>
public void Info(string message)
{
System.Console.ForegroundColor = ConsoleColor.Gray;
System.Console.WriteLine(message);
}

/// <summary>
/// 记录调试级别的日志消息。
/// </summary>
/// <param name="message">日志消息。</param>
public void Debug(string message)
{
System.Console.ForegroundColor = ConsoleColor.DarkGreen;
System.Console.WriteLine(message);
}

/// <summary>
/// 记录错误级别的日志消息。
/// </summary>
/// <param name="message">日志消息。</param>
public void Error(string message)
{
System.Console.ForegroundColor = ConsoleColor.DarkRed;
System.Console.WriteLine(message);
}

/// <summary>
/// 记录严重错误级别的日志消息。
/// </summary>
/// <param name="message">日志消息。</param>
public void Fatal(string message)
{
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.WriteLine(message);
}

/// <summary>
/// 记录跟踪级别的格式化日志消息。
/// </summary>
/// <param name="message">日志消息模板。</param>
/// <param name="args">格式化参数。</param>
public void Trace(string message, params object[] args)
{
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.WriteLine(message, args);
}

/// <summary>
/// 记录警告级别的格式化日志消息。
/// </summary>
/// <param name="message">日志消息模板。</param>
/// <param name="args">格式化参数。</param>
public void Warning(string message, params object[] args)
{
System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.WriteLine(message, args);
}

/// <summary>
/// 记录信息级别的格式化日志消息。
/// </summary>
/// <param name="message">日志消息模板。</param>
/// <param name="args">格式化参数。</param>
public void Info(string message, params object[] args)
{
System.Console.ForegroundColor = ConsoleColor.Gray;
System.Console.WriteLine(message, args);
}

/// <summary>
/// 记录调试级别的格式化日志消息。
/// </summary>
/// <param name="message">日志消息模板。</param>
/// <param name="args">格式化参数。</param>
public void Debug(string message, params object[] args)
{
System.Console.ForegroundColor = ConsoleColor.DarkGreen;
System.Console.WriteLine(message, args);
}

/// <summary>
/// 记录错误级别的格式化日志消息。
/// </summary>
/// <param name="message">日志消息模板。</param>
/// <param name="args">格式化参数。</param>
public void Error(string message, params object[] args)
{
System.Console.ForegroundColor = ConsoleColor.DarkRed;
System.Console.WriteLine(message, args);
}

/// <summary>
/// 记录严重错误级别的格式化日志消息。
/// </summary>
/// <param name="message">日志消息模板。</param>
/// <param name="args">格式化参数。</param>
public void Fatal(string message, params object[] args)
{
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.WriteLine(message, args);
}
}
}
32 changes: 32 additions & 0 deletions Fantasy.Benchmark/Fantasy.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE;FANTASY_CONSOLE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DefineConstants>TRACE;FANTASY_CONSOLE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="MongoDB.Bson" Version="2.29.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="protobuf-net" Version="3.2.30" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Fantays.Console\Fantasy.Console.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions Fantasy.Benchmark/NetworkBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Fantasy.Async;
using Fantasy.InnerMessage;
using Fantasy.Network;
using Fantasy.Platform.Console;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

namespace Fantasy.Benchmark;
[SimpleJob(RuntimeMoniker.Net80, baseline: true)]
public class NetworkBenchmark
{
private static Scene _scene;
private static Session _session;
private readonly BenchmarkRequest _benchmarkRequest = new BenchmarkRequest();

public static async FTask Initialize()
{
// 注册日志实例到框架中
Log.Register(new ConsoleLog());
// 初始化框架
Entry.Initialize();
// 执行StartUpdate方法
Entry.StartUpdate();
_scene = await Entry.CreateScene();
// 创建远程连接
_session = _scene.Connect("127.0.0.1:20000", NetworkProtocolType.KCP,
() =>
{
Log.Debug("连接到目标服务器成功");
var summary = BenchmarkRunner.Run<NetworkBenchmark>();
Console.WriteLine(summary);
},
() => { Log.Debug("无法连接到目标服务器"); },
() => { Log.Debug("与服务器断开连接"); }, false);
}

[Benchmark]
public async FTask Call()
{
await _session.Call(_benchmarkRequest);
}
}
2 changes: 2 additions & 0 deletions Fantasy.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fantasy.Benchmark.NetworkBenchmark.Initialize().Coroutine();
Console.ReadKey();
6 changes: 6 additions & 0 deletions Fantasy.Benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Fantasy.Benchmark
使用 Fantasy.Benchmark 工具,我们能够快速评估框架网络的处理性能。目前,该工具提供的基准测试主要集中在 RPC(远程过程调用)消息 方面。这一项测试能够有效测量系统在处理远程调用时的响应时间、吞吐量和资源利用率,帮助开发者优化网络通信性能,确保在高负载情况下系统依然能够稳定运行
## 操作步骤
- 1.打开位于 Examples/Server/Server.sln 的解决方案文件。
- 2.在解决方案中选择并启动 Main 项目。
- 3.接着,启动 Fantasy.Benchmark 应用程序,并耐心等待其测试结果的生成。
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Fantasy.Async;
using Fantasy.InnerMessage;
using Fantasy.Network.Interface;

#if FANTASY_NET
namespace Fantasy.Network.Benchmark.Handler;

/// <summary>
/// BenchmarkRequestHandler
/// </summary>
public sealed class BenchmarkRequestHandler : MessageRPC<BenchmarkRequest, BenchmarkResponse>
{
/// <summary>
/// Run方法
/// </summary>
/// <param name="session"></param>
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="reply"></param>
protected override async FTask Run(Session session, BenchmarkRequest request, BenchmarkResponse response, Action reply)
{
await FTask.CompletedTask;
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@
namespace Fantasy.InnerMessage
{
[ProtoContract]
public sealed partial class BenchmarkMessage : AMessage, IMessage
{
public uint OpCode()
{
return Fantasy.Network.OpCode.BenchmarkMessage;
}
}
[ProtoContract]
public partial class BenchmarkRequest : AMessage, IRequest
{
public uint OpCode()
{
return Fantasy.Network.OpCode.BenchmarkRequest;
}
[ProtoIgnore]
public BenchmarkResponse ResponseType { get; set; }
[ProtoMember(1)]
public long RpcId { get; set; }
}

[ProtoContract]
public partial class BenchmarkResponse : AMessage, IResponse
{
public uint OpCode()
{
return Fantasy.Network.OpCode.BenchmarkResponse;
}
[ProtoMember(1)]
public long RpcId { get; set; }
[ProtoMember(2)]
public uint ErrorCode { get; set; }
}
public sealed partial class Response : AMessage, IResponse
{
public uint OpCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public static class OpCodeType

public static class OpCode
{
public static readonly uint BenchmarkMessage = Create(OpCodeProtocolType.ProtoBuf, OpCodeType.OuterMessage, 8388607);
public static readonly uint BenchmarkRequest = Create(OpCodeProtocolType.ProtoBuf, OpCodeType.OuterRequest, 8388607);
public static readonly uint BenchmarkResponse = Create(OpCodeProtocolType.ProtoBuf, OpCodeType.OuterResponse, 8388607);
public static readonly uint PingRequest = Create(OpCodeProtocolType.ProtoBuf, OpCodeType.OuterPingRequest, 1);
public static readonly uint PingResponse = Create(OpCodeProtocolType.ProtoBuf, OpCodeType.OuterPingResponse, 1);
public static readonly uint DefaultResponse = Create(OpCodeProtocolType.ProtoBuf, OpCodeType.InnerResponse, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void StartUpdate()
while (_isInit)
{
ThreadScheduler.Update();
Thread.Sleep(1);
}
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
using System.ComponentModel;
using System.Runtime.Serialization;
using Fantasy.Pool;
#if FANTASY_NET
#if FANTASY_NET || FANTASY_UNITY || FANTASY_CONSOLE
using MongoDB.Bson.Serialization.Attributes;
#endif
using Newtonsoft.Json;
using ProtoBuf;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

Expand Down
8 changes: 8 additions & 0 deletions Fantasy.Unity/Fantasy.Unity/Runtime/Core/Benchmark.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc799ad

Please sign in to comment.