Skip to content

Commit 5060bd2

Browse files
authored
Add touchsocket platform test (TechEmpower#10278)
* update(touchsocket):version to 4.0.0-rc.2 * update(touchsocket):version to 4.0.0-rc.5 * optimize(touchsocket):scheduler * Performance (TouchSocketHttp, TouchSocketWebApi): Enable server garbage collection * update(touchsocket):version to 4.0.0-rc.15 * update(touchsocket):version to 4.0.0-rc42 * 重构(Program): 显式指定变量类型并优化配置 显式指定变量类型以提高代码可读性和类型安全性 修改命名空间为 `TouchSocketHttp` 将 `options.BufferOnDemand` 默认值从 `false` 修改为 `true` 调整 `SetTransportOption` 和 `ApiServer` 中的相关代码 * 优化(csproj): 升级TouchSocket包版本 在TouchSocketHttp.csproj和TouchSocketWebApi.csproj中,将TouchSocket.Hosting和TouchSocket.WebApi的包引用版本从4.0.0-rc.42升级到4.0.0-rc.44,以修复错误、提高性能或增加新功能 * Add(TouchSocket):platform test * Add:Platform config * 优化(Program.cs): 替换 Console.ReadKey 为 Console.ReadLine 替换 Program.cs 文件中 Program 类的 Console.ReadKey 为 Console.ReadLine 更改目的是更方便地处理用户输入或与其他代码逻辑保持一致 * 优化: 移除不必要的 ServerGarbageCollection 属性并统一命名空间为 TouchSocketHttpPlatform * 优化(Program): 调整HTTP服务器启动逻辑 重构服务器启动流程以支持更灵活的配置: - 替换server.StartAsync(8080)为server.SetupAsync配置流程 - 配置监听端口、最大连接数及控制台日志记录器 - 删除server.SafeDispose,改为通过while(true)保持运行 - 删除直接调用Console.ReadLine的逻辑 * 重构(project): 更新项目依赖和SDK配置 更新 `TouchSocketHttp.csproj` 的项目 SDK 为 `Microsoft.NET.Sdk`,移除 `<UserSecretsId>` 配置 移除 `Microsoft.Extensions.Hosting` 和 `TouchSocket.Hosting` 旧版本依赖,更新 `TouchSocket.WebApi` 至 `4.0.0-rc.46` 更新 `TouchSocketHttpPlatform.csproj` 中 `TouchSocket` 包版本至 `4.0.0-rc.46` 更新 `TouchSocketWebApi.csproj` 中 `TouchSocket.Hosting` 和 `TouchSocket.WebApi` 包版本至 `4.0.0-rc.46` * 新增(config): 添加 NuGet.Config 配置文件 添加新的 NuGet.Config 文件,包含以下更改: - 引入 XML 声明,版本为 1.0,编码为 UTF-8 - 在 `<configuration>` 节点中定义 `<packageSources>` 节点 - 使用 `<clear />` 清除所有现有的包源配置 - 添加新的包源,键为 "NuGet",值为 "https://api.nuget.org/v3/index.json"
1 parent 40ce2db commit 5060bd2

15 files changed

Lines changed: 546 additions & 16 deletions

frameworks/CSharp/touchsocket/Benchmarks.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 18
4-
VisualStudioVersion = 18.0.11109.219 d18.0-oob
4+
VisualStudioVersion = 18.0.11109.219
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TouchSocketWebApi", "src\TouchSocketWebApi\TouchSocketWebApi.csproj", "{6BD9363A-D77F-5D90-8444-2BC37495C920}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TouchSocketHttp31", "src\To
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TouchSocketWebApi31", "src\TouchSocketWebApi31\TouchSocketWebApi31.csproj", "{6B6A57CE-1F69-FA6E-7458-0A4B6AB60683}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TouchSocketHttpPlatform", "src\TouchSocketHttpPlatform\TouchSocketHttpPlatform.csproj", "{BC320C24-941D-2109-FBC8-92780569BBA4}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{6B6A57CE-1F69-FA6E-7458-0A4B6AB60683}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{6B6A57CE-1F69-FA6E-7458-0A4B6AB60683}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{6B6A57CE-1F69-FA6E-7458-0A4B6AB60683}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{BC320C24-941D-2109-FBC8-92780569BBA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{BC320C24-941D-2109-FBC8-92780569BBA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{BC320C24-941D-2109-FBC8-92780569BBA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{BC320C24-941D-2109-FBC8-92780569BBA4}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

frameworks/CSharp/touchsocket/README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,40 @@
22

33
See [touchsocket](https://touchsocket.net/) for more information.
44

5+
## Variants Included
6+
7+
* `touchsocket.webapi` (`TouchSocketWebApi`) – WebApi style.
8+
* `touchsocket.webapi31` (`TouchSocketWebApi31`) – WebApi targeting .NET 8.
9+
* `touchsocket.http` (`TouchSocketHttp`) – Minimal HTTP implementation.
10+
* `touchsocket.http31` (`TouchSocketHttp31`) – Minimal HTTP targeting .NET 8.
11+
* `touchsocket.httpplatform` (`TouchSocketHttpPlatform`) – High-performance custom pipeline-based HTTP server focusing on low-level parsing and zero-allocation response writing.
12+
513
## Infrastructure Software Versions
614

7-
**Language**
15+
**Language / Runtime**
16+
17+
* C# / .NET (8.0 & 9.0 depending on variant)
18+
19+
## Endpoints
20+
21+
All variants implement:
22+
23+
* `/plaintext` – Returns a plain text "Hello, World!" response.
24+
* `/json` – Returns a JSON object `{"message": "Hello, World!"}`.
25+
26+
The `httpplatform` variant manually parses request lines and headers via `System.IO.Pipelines` for maximum throughput.
27+
28+
## Dockerfiles
29+
30+
Each variant has a dedicated Dockerfile named:
31+
32+
* `touchsocket.dockerfile` (webapi)
33+
* `touchsocket-webapi31.dockerfile`
34+
* `touchsocket-http.dockerfile`
35+
* `touchsocket-http31.dockerfile`
36+
* `touchsocket-httpplatform.dockerfile`
37+
38+
## Notes
839

9-
* C# 8.0
40+
The `httpplatform` variant is intended for benchmarking raw server performance. It omits higher-level abstractions to reduce overhead.
1041

frameworks/CSharp/touchsocket/benchmark_config.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@
7777
"display_name": "touchsocket.webapi31",
7878
"notes": "",
7979
"versus": "aspnetcore-mvc"
80+
},
81+
"httpplatform": {
82+
"plaintext_url": "/plaintext",
83+
"json_url": "/json",
84+
"port": 8080,
85+
"approach": "Realistic",
86+
"classification": "Platform",
87+
"database": "Postgres",
88+
"framework": "touchsocket.httpplatform",
89+
"language": "C#",
90+
"orm": "Micro",
91+
"platform": ".NET",
92+
"flavor": "CoreCLR",
93+
"webserver": "touchsocket",
94+
"os": "Linux",
95+
"database_os": "Linux",
96+
"display_name": "touchsocket.httpplatform",
97+
"notes": "High-performance custom pipeline-based implementation variant.",
98+
"versus": "aspnetcore"
8099
}
81100
}
82101
]

frameworks/CSharp/touchsocket/config.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ orm = "Raw"
5252
platform = ".NET"
5353
webserver = "touchsocket.webapi31"
5454
versus = "aspcore-mvc"
55+
56+
[httpplatform]
57+
urls.plaintext = "/plaintext"
58+
urls.json = "/json"
59+
approach = "Realistic"
60+
classification = "Micro"
61+
database = "Postgres"
62+
database_os = "Linux"
63+
os = "Linux"
64+
orm = "Raw"
65+
platform = ".NET"
66+
webserver = "touchsocket.httpplatform"
67+
versus = "aspcore"

frameworks/CSharp/touchsocket/src/TouchSocketHttp/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Text;
22
using TouchSocket.Core;
33
using TouchSocket.Http;
4-
using TouchSocket.Sockets;
54
using HttpContent = TouchSocket.Http.HttpContent;
65

76
namespace TouchSocketHttp;
@@ -18,7 +17,7 @@ await service.SetupAsync(new TouchSocketConfig()
1817
.SetMaxCount(1000000)
1918
.SetTransportOption(options =>
2019
{
21-
options.BufferOnDemand = true;
20+
options.BufferOnDemand = false;
2221
})
2322
.ConfigureContainer(a =>
2423
{
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Worker">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<OutputType>Exe</OutputType>
77
<ImplicitUsings>enable</ImplicitUsings>
8-
<ServerGarbageCollection>true</ServerGarbageCollection>
9-
<UserSecretsId>dotnet-TouchSocketWebApi-987c185f-10b1-452b-beb6-47d798a5a131</UserSecretsId>
108
</PropertyGroup>
119

1210
<ItemGroup>
13-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
14-
<PackageReference Include="TouchSocket.Hosting" Version="4.0.0-rc.44" />
15-
<PackageReference Include="TouchSocket.WebApi" Version="4.0.0-rc.44" />
11+
<PackageReference Include="TouchSocket.WebApi" Version="4.0.0-rc.46" />
1612
</ItemGroup>
1713
</Project>
1814

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// ------------------------------------------------------------------------------
2+
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
3+
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
4+
// CSDN博客:https://blog.csdn.net/qq_40374647
5+
// 哔哩哔哩视频:https://space.bilibili.com/94253567
6+
// Gitee源代码仓库:https://gitee.com/RRQM_Home
7+
// Github源代码仓库:https://github.com/RRQM
8+
// API首页:https://touchsocket.net/
9+
// 交流QQ群:234762506
10+
// 感谢您的下载和使用
11+
// ------------------------------------------------------------------------------
12+
13+
using System.Buffers.Text;
14+
using System.Diagnostics;
15+
using TouchSocket.Core;
16+
using TouchSocket.Sockets;
17+
18+
namespace TouchSocketHttpPlatform;
19+
20+
internal static class DateHeader
21+
{
22+
private const int dateTimeRLength = 29;
23+
private const int prefixLength = 6; // "Date: ".Length is 6 in bytes for ASCII
24+
private const int suffixIndex = dateTimeRLength + prefixLength;
25+
26+
// Wed, 14 Mar 2018 14:20:00 GMT
27+
private const int suffixLength = 2; // crlf
28+
private static readonly Timer s_timer = new((s) =>
29+
{
30+
SetDateValues(DateTimeOffset.UtcNow);
31+
}, null, 1000, 1000);
32+
33+
private static byte[] s_headerBytesMaster = new byte[prefixLength + dateTimeRLength + 2 * suffixLength];
34+
private static byte[] s_headerBytesScratch = new byte[prefixLength + dateTimeRLength + 2 * suffixLength];
35+
36+
static DateHeader()
37+
{
38+
var utf8 = "Date: "u8;
39+
40+
utf8.CopyTo(s_headerBytesMaster);
41+
utf8.CopyTo(s_headerBytesScratch);
42+
s_headerBytesMaster[suffixIndex] = (byte)'\r';
43+
s_headerBytesMaster[suffixIndex + 1] = (byte)'\n';
44+
s_headerBytesMaster[suffixIndex + 2] = (byte)'\r';
45+
s_headerBytesMaster[suffixIndex + 3] = (byte)'\n';
46+
s_headerBytesScratch[suffixIndex] = (byte)'\r';
47+
s_headerBytesScratch[suffixIndex + 1] = (byte)'\n';
48+
s_headerBytesScratch[suffixIndex + 2] = (byte)'\r';
49+
s_headerBytesScratch[suffixIndex + 3] = (byte)'\n';
50+
51+
SetDateValues(DateTimeOffset.UtcNow);
52+
SyncDateTimer();
53+
}
54+
55+
public static ReadOnlySpan<byte> HeaderBytes => s_headerBytesMaster;
56+
57+
public static void SyncDateTimer()
58+
{
59+
s_timer.Change(1000, 1000);
60+
}
61+
private static void SetDateValues(DateTimeOffset value)
62+
{
63+
lock (s_headerBytesScratch)
64+
{
65+
if (!Utf8Formatter.TryFormat(value, s_headerBytesScratch.AsSpan(prefixLength), out var written, 'R'))
66+
{
67+
throw new Exception("date time format failed");
68+
}
69+
Debug.Assert(written == dateTimeRLength);
70+
(s_headerBytesScratch, s_headerBytesMaster) = (s_headerBytesMaster, s_headerBytesScratch);
71+
}
72+
}
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ------------------------------------------------------------------------------
2+
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
3+
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
4+
// CSDN博客:https://blog.csdn.net/qq_40374647
5+
// 哔哩哔哩视频:https://space.bilibili.com/94253567
6+
// Gitee源代码仓库:https://gitee.com/RRQM_Home
7+
// Github源代码仓库:https://github.com/RRQM
8+
// API首页:https://touchsocket.net/
9+
// 交流QQ群:234762506
10+
// 感谢您的下载和使用
11+
// ------------------------------------------------------------------------------
12+
13+
using TouchSocket.Sockets;
14+
15+
namespace TouchSocketHttpPlatform;
16+
17+
internal sealed class MyServer : TcpService<MyTcpSessionClientBase>
18+
{
19+
/// <inheritdoc/>
20+
protected override MyTcpSessionClientBase NewClient()
21+
{
22+
return new MyTcpSessionClientBase();
23+
}
24+
}

0 commit comments

Comments
 (0)