Skip to content

Commit 28fac6a

Browse files
authored
[akazawayun.pro] add db tests (#10070)
* add new test : CSharp/akazawayun.pro * modify config and code * add content-type * update url * update package version * add 3 new tests: db、queries、updates 1. add 3 new tests: db、queries、updates; 2. split code to multi files; * - * use generated sql instead const string. * update version * fix bug of db query
1 parent b35988f commit 28fac6a

File tree

7 files changed

+185
-51
lines changed

7 files changed

+185
-51
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# 此阶段用于生成项目
1+
# 生成
22
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS publish
33
WORKDIR /app
44
COPY src .
55
RUN dotnet publish -c Release -o /app/publish
66

7-
# 此阶段用于运行
7+
# 运行
88
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS runtime
99
WORKDIR /app
1010
COPY --from=publish /app/publish .
1111

1212
EXPOSE 2022
13-
ENTRYPOINT ["dotnet", "AkazawaYun.FrameworkBenchmarks.dll"]
13+
ENTRYPOINT ["dotnet", "AkazawaYun.FrameworkBenchmarks.dll"]
Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
{
2-
"framework": "akazawayun.pro",
3-
"tests": [
4-
{
5-
"default": {
6-
"display_name": "akazawayun.pro",
7-
"framework": "akazawayun.pro",
8-
"webserver": "akazawayun.pro",
9-
"plaintext_url": "/post/plaintext",
10-
"json_url": "/post/json",
11-
"port": 2022,
12-
"classification": "Micro",
13-
"approach": "Realistic",
14-
"platform": ".NET",
15-
"language": "C#",
16-
"flavor": "CoreCLR",
17-
"os": "Linux",
18-
"database_os": "Linux",
19-
"database": "MySQL",
20-
"orm": "Micro",
21-
"notes": ""
22-
}
23-
}
24-
]
2+
"framework": "akazawayun.pro",
3+
"tests": [
4+
{
5+
"default": {
6+
"display_name": "akazawayun.pro",
7+
"framework": "akazawayun.pro",
8+
"webserver": "akazawayun.pro",
9+
"plaintext_url": "/post/plaintext",
10+
"json_url": "/post/json",
11+
"db_url": "/post/db",
12+
"query_url": "/post/queries?queries=",
13+
"update_url": "/post/updates?queries=",
14+
"port": 2022,
15+
"classification": "Micro",
16+
"approach": "Realistic",
17+
"platform": ".NET",
18+
"language": "C#",
19+
"flavor": "CoreCLR",
20+
"os": "Linux",
21+
"database_os": "Linux",
22+
"database": "MySQL",
23+
"orm": "Micro",
24+
"notes": ""
25+
}
26+
}
27+
]
2528
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<EnableSourceGenerator>true</EnableSourceGenerator>
9+
<PublishAot>false</PublishAot>
810
</PropertyGroup>
9-
11+
1012
<PropertyGroup>
1113
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
1214
<ServerGarbageCollection>true</ServerGarbageCollection>
1315
</PropertyGroup>
14-
16+
1517
<ItemGroup>
16-
<PackageReference Include="AkazawaYun.PRO" Version="1.13.25.727" />
18+
<PackageReference Include="AkazawaYun.PRO" Version="1.13.25.816" />
19+
<PackageReference Include="MySql.Data" Version="9.4.0" />
1720
</ItemGroup>
1821

1922
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma warning disable IDE1006,CS8981
2+
using AkazawaYun.AOT;
3+
using System.Text.Json.Serialization;
4+
5+
namespace AkazawaYun.FrameworkBenchmarks;
6+
7+
8+
[JsonSerializable(typeof(JsonModel))]
9+
[JsonSerializable(typeof(world[]))]
10+
public partial class AotJsonContext : JsonSerializerContext { }
11+
12+
13+
public class JsonModel
14+
{
15+
public string? message { get; set; }
16+
}
17+
public class world : IAotModel
18+
{
19+
public int id { get; set; }
20+
public int randomNumber { get; set; }
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using AkazawaYun.PRO7;
2+
using MySql.Data.MySqlClient;
3+
using System.Data.Common;
4+
5+
namespace AkazawaYun.FrameworkBenchmarks;
6+
7+
class Mysql : akzDbFactory
8+
{
9+
protected override DbConnection NewConnection() => new MySqlConnection(ConString);
10+
}
Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,93 @@
1-
using AkazawaYun.PRO7;
1+
#pragma warning disable IDE1006,IL2026
2+
3+
using AkazawaYun.AOT;
4+
using AkazawaYun.PRO7;
5+
using AkazawaYun.PRO7.AkazawaYunWebFunctionAOP;
26
using AkazawaYun.PRO7.AkazawaYunWebInterceptor;
3-
using System.Text.Json.Serialization;
7+
using System.Diagnostics.CodeAnalysis;
48

59
namespace AkazawaYun.FrameworkBenchmarks;
610

7-
public class Program : IPostFunction
11+
class Program : IPostFunctionWrapper
812
{
9-
static async Task Main()
13+
static readonly akzWebBuilder builder;
14+
static readonly akzDbFactory mysql;
15+
16+
17+
static Program()
1018
{
11-
akzJson.Config(null, Json.Default);
12-
var server = await akzWebBuilder.Shared.Load().SetDefault().Build()
19+
akzJson.Config(null, AotJsonContext.Default);
20+
builder = akzWebBuilder.Shared.SetDefault()
21+
.Build()
1322
.Config<IWebReceptor, akzWebInterceptor>(itc =>
1423
{
24+
itc.ClearInterceptor();
1525
itc.AddInterceptor(new akzWebInterceptorNotOnlyPost());
16-
}).Launch();
26+
});
27+
mysql = new akzDbBuilderII()
28+
.SetServer("tfb-database:3306")
29+
//.SetServer("localhost:3306")
30+
.SetUser("benchmarkdbuser")
31+
.SetPwd("benchmarkdbpass")
32+
.SetDatabase("hello_world")
33+
.SetCharset()
34+
.SetOtherset()
35+
.Build<Mysql>();
36+
}
37+
static async Task Main()
38+
{
39+
await builder.Launch();
1740
akzLog.Default = akzLog.Output.None;
1841
await Task.Delay(-1);
1942
}
2043

2144

22-
public static ValueTask<HttpRes> plaintext(HttpReq _)
45+
public static HttpRes plaintext() => HttpRes.HttpOK("Hello, World!", ".txt");
46+
public static JsonModel json() => new()
47+
{
48+
message = "Hello, World!"
49+
};
50+
51+
//[WebFunctionAopTry]
52+
public static async Task<world> db()
2353
{
24-
return HttpRes.HttpOK("Hello, World!", ".txt");
54+
await using IDb con = await mysql.Connect();
55+
world obj = await WorldService.GetRandomWorld(con);
56+
return obj;
2557
}
26-
public static ValueTask<HttpRes> json(HttpReq _)
58+
//[WebFunctionAopTry]
59+
public static async Task<world[]> queries(string queries)
2760
{
28-
return HttpRes.HttpJson(new JsonResponse
29-
{
30-
message = "Hello, World!"
31-
});
61+
int count = ParseCount(queries);
62+
63+
await using IDb con = await mysql.Connect();
64+
world[] lst = await WorldService.GetWorlds(con, count);
65+
return lst;
3266
}
33-
}
67+
//[WebFunctionAopTry]
68+
public static async Task<world[]> updates(string queries)
69+
{
70+
int count = ParseCount(queries);
3471

35-
public class JsonResponse
36-
{
37-
public string? message { get; set; }
38-
}
72+
await using IDb con = await mysql.Connect();
73+
world[] lst = await WorldService.GetWorlds(con, count);
74+
75+
foreach (world obj in lst)
76+
obj.randomNumber = Random.Shared.Next(1, 10001);
77+
78+
await WorldService.SaveWorlds(con, lst);
79+
80+
return lst;
81+
}
3982

40-
[JsonSerializable(typeof(JsonResponse))]
41-
public partial class Json : JsonSerializerContext { }
83+
84+
static int ParseCount(string queries)
85+
{
86+
if (!int.TryParse(queries, out int count))
87+
return 1;
88+
89+
count = Math.Clamp(count, 1, 500);
90+
return count;
91+
}
92+
93+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using AkazawaYun.PRO7;
2+
3+
namespace AkazawaYun.FrameworkBenchmarks;
4+
5+
class WorldService
6+
{
7+
static readonly int @id;
8+
// SELECT id, randomNumber FROM world WHERE id=@id ;
9+
public static readonly string SqlSelect = akzSqlinq<world>.Query().Select(m => new
10+
{
11+
m.id,
12+
m.randomNumber,
13+
}).Where(m => m.id == @id).Build();
14+
// UPDATE world SET randomNumber=@randomNumber WHERE id=@id ;
15+
public static readonly string SqlUpdate = akzSqlinq<world>.Update().Set(m => new()
16+
{
17+
randomNumber = m.randomNumber,
18+
}).Where(m => m.id == @id).Build();
19+
20+
21+
public static async Task<world> GetRandomWorld(IDb con, IDictionary<string, object?>? shared = null)
22+
{
23+
int id = Random.Shared.Next(1, 10001);
24+
shared ??= new DpSingleBuilder().Build();
25+
shared["id"] = id;
26+
27+
world? obj = await con.Find(SqlSelect, shared).Toworld();
28+
return obj!;
29+
}
30+
public static async Task<world[]> GetWorlds(IDb con, int count)
31+
{
32+
world[] lst = new world[count];
33+
var dp = new DpSingleBuilder().Build();
34+
for (int i = 0; i < count; i++)
35+
{
36+
world obj = await GetRandomWorld(con, dp);
37+
lst[i] = obj;
38+
}
39+
return lst;
40+
}
41+
public static async Task SaveWorlds(IDb con, world[] lst)
42+
{
43+
await con.Execute(SqlUpdate, lst.ToDp());
44+
}
45+
}

0 commit comments

Comments
 (0)