Эх сурвалжийг харах

[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
AkazawaYun 2 долоо хоног өмнө
parent
commit
28fac6a635

+ 3 - 3
frameworks/CSharp/akazawayun.pro/akazawayun.pro.dockerfile

@@ -1,13 +1,13 @@
-# 此阶段用于生成项目
+# 生成
 FROM mcr.microsoft.com/dotnet/sdk:9.0 AS publish
 WORKDIR /app
 COPY src .
 RUN dotnet publish -c Release -o /app/publish
 
-# 此阶段用于运行
+# 运行
 FROM mcr.microsoft.com/dotnet/sdk:9.0 AS runtime
 WORKDIR /app
 COPY --from=publish /app/publish .
 
 EXPOSE 2022
-ENTRYPOINT ["dotnet", "AkazawaYun.FrameworkBenchmarks.dll"]
+ENTRYPOINT ["dotnet", "AkazawaYun.FrameworkBenchmarks.dll"]

+ 26 - 23
frameworks/CSharp/akazawayun.pro/benchmark_config.json

@@ -1,25 +1,28 @@
 {
-    "framework": "akazawayun.pro",
-    "tests": [
-        {
-            "default": {
-                "display_name": "akazawayun.pro",
-                "framework": "akazawayun.pro",
-                "webserver": "akazawayun.pro",
-                "plaintext_url": "/post/plaintext",
-                "json_url": "/post/json",
-                "port": 2022,
-                "classification": "Micro",
-                "approach": "Realistic",
-                "platform": ".NET",
-                "language": "C#",
-                "flavor": "CoreCLR",
-                "os": "Linux",
-                "database_os": "Linux",
-                "database": "MySQL",
-                "orm": "Micro",
-                "notes": ""
-            }
-        }
-    ]
+  "framework": "akazawayun.pro",
+  "tests": [
+    {
+      "default": {
+        "display_name": "akazawayun.pro",
+        "framework": "akazawayun.pro",
+        "webserver": "akazawayun.pro",
+        "plaintext_url": "/post/plaintext",
+        "json_url": "/post/json",
+        "db_url": "/post/db",
+        "query_url": "/post/queries?queries=",
+        "update_url": "/post/updates?queries=",
+        "port": 2022,
+        "classification": "Micro",
+        "approach": "Realistic",
+        "platform": ".NET",
+        "language": "C#",
+        "flavor": "CoreCLR",
+        "os": "Linux",
+        "database_os": "Linux",
+        "database": "MySQL",
+        "orm": "Micro",
+        "notes": ""
+      }
+    }
+  ]
 }

+ 7 - 4
frameworks/CSharp/akazawayun.pro/src/AkazawaYun.FrameworkBenchmarks.csproj

@@ -1,19 +1,22 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net9.0</TargetFramework>
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
+    <EnableSourceGenerator>true</EnableSourceGenerator>
+    <PublishAot>false</PublishAot>
   </PropertyGroup>
-  
+
   <PropertyGroup>
     <ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
     <ServerGarbageCollection>true</ServerGarbageCollection>
   </PropertyGroup>
-  
+
   <ItemGroup>
-    <PackageReference Include="AkazawaYun.PRO" Version="1.13.25.727" />
+    <PackageReference Include="AkazawaYun.PRO" Version="1.13.25.816" />
+    <PackageReference Include="MySql.Data" Version="9.4.0" />
   </ItemGroup>
 
 </Project>

+ 21 - 0
frameworks/CSharp/akazawayun.pro/src/Model.cs

@@ -0,0 +1,21 @@
+#pragma warning disable IDE1006,CS8981
+using AkazawaYun.AOT;
+using System.Text.Json.Serialization;
+
+namespace AkazawaYun.FrameworkBenchmarks;
+
+
+[JsonSerializable(typeof(JsonModel))]
+[JsonSerializable(typeof(world[]))]
+public partial class AotJsonContext : JsonSerializerContext { }
+
+
+public class JsonModel
+{
+    public string? message { get; set; }
+}
+public class world : IAotModel
+{
+    public int id { get; set; }
+    public int randomNumber { get; set; }
+}

+ 10 - 0
frameworks/CSharp/akazawayun.pro/src/Mysql.cs

@@ -0,0 +1,10 @@
+using AkazawaYun.PRO7;
+using MySql.Data.MySqlClient;
+using System.Data.Common;
+
+namespace AkazawaYun.FrameworkBenchmarks;
+
+class Mysql : akzDbFactory
+{
+    protected override DbConnection NewConnection() => new MySqlConnection(ConString);
+}

+ 73 - 21
frameworks/CSharp/akazawayun.pro/src/Program.cs

@@ -1,41 +1,93 @@
-using AkazawaYun.PRO7;
+#pragma warning disable IDE1006,IL2026
+
+using AkazawaYun.AOT;
+using AkazawaYun.PRO7;
+using AkazawaYun.PRO7.AkazawaYunWebFunctionAOP;
 using AkazawaYun.PRO7.AkazawaYunWebInterceptor;
-using System.Text.Json.Serialization;
+using System.Diagnostics.CodeAnalysis;
 
 namespace AkazawaYun.FrameworkBenchmarks;
 
-public class Program : IPostFunction
+class Program : IPostFunctionWrapper
 {
-    static async Task Main()
+    static readonly akzWebBuilder builder;
+    static readonly akzDbFactory mysql;
+
+
+    static Program()
     {
-        akzJson.Config(null, Json.Default);
-        var server = await akzWebBuilder.Shared.Load().SetDefault().Build()
+        akzJson.Config(null, AotJsonContext.Default);
+        builder = akzWebBuilder.Shared.SetDefault()
+            .Build()
             .Config<IWebReceptor, akzWebInterceptor>(itc =>
             {
+                itc.ClearInterceptor();
                 itc.AddInterceptor(new akzWebInterceptorNotOnlyPost());
-            }).Launch();
+            });
+        mysql = new akzDbBuilderII()
+            .SetServer("tfb-database:3306")
+            //.SetServer("localhost:3306")
+            .SetUser("benchmarkdbuser")
+            .SetPwd("benchmarkdbpass")
+            .SetDatabase("hello_world")
+            .SetCharset()
+            .SetOtherset()
+            .Build<Mysql>();
+    }
+    static async Task Main()
+    {
+        await builder.Launch();
         akzLog.Default = akzLog.Output.None;
         await Task.Delay(-1);
     }
 
 
-    public static ValueTask<HttpRes> plaintext(HttpReq _)
+    public static HttpRes plaintext() => HttpRes.HttpOK("Hello, World!", ".txt");
+    public static JsonModel json() => new()
+    {
+        message = "Hello, World!"
+    };
+
+    //[WebFunctionAopTry]
+    public static async Task<world> db()
     {
-        return HttpRes.HttpOK("Hello, World!", ".txt");
+        await using IDb con = await mysql.Connect();
+        world obj = await WorldService.GetRandomWorld(con);
+        return obj;
     }
-    public static ValueTask<HttpRes> json(HttpReq _)
+    //[WebFunctionAopTry]
+    public static async Task<world[]> queries(string queries)
     {
-        return HttpRes.HttpJson(new JsonResponse
-        {
-            message = "Hello, World!"
-        });
+        int count = ParseCount(queries);
+
+        await using IDb con = await mysql.Connect();
+        world[] lst = await WorldService.GetWorlds(con, count);
+        return lst;
     }
-}
+    //[WebFunctionAopTry]
+    public static async Task<world[]> updates(string queries)
+    {
+        int count = ParseCount(queries);
 
-public class JsonResponse
-{
-    public string? message { get; set; }
-}
+        await using IDb con = await mysql.Connect();
+        world[] lst = await WorldService.GetWorlds(con, count);
+
+        foreach (world obj in lst)
+            obj.randomNumber = Random.Shared.Next(1, 10001);
+
+        await WorldService.SaveWorlds(con, lst);
+
+        return lst;
+    }
 
-[JsonSerializable(typeof(JsonResponse))]
-public partial class Json : JsonSerializerContext { }
+
+    static int ParseCount(string queries)
+    {
+        if (!int.TryParse(queries, out int count))
+            return 1;
+
+        count = Math.Clamp(count, 1, 500);
+        return count;
+    }
+
+}

+ 45 - 0
frameworks/CSharp/akazawayun.pro/src/WorldService.cs

@@ -0,0 +1,45 @@
+using AkazawaYun.PRO7;
+
+namespace AkazawaYun.FrameworkBenchmarks;
+
+class WorldService
+{
+    static readonly int @id;
+    // SELECT id, randomNumber FROM world WHERE id=@id ;
+    public static readonly string SqlSelect = akzSqlinq<world>.Query().Select(m => new
+    {
+        m.id,
+        m.randomNumber,
+    }).Where(m => m.id == @id).Build();
+    // UPDATE world SET randomNumber=@randomNumber WHERE id=@id ;
+    public static readonly string SqlUpdate = akzSqlinq<world>.Update().Set(m => new()
+    {
+        randomNumber = m.randomNumber,
+    }).Where(m => m.id == @id).Build();
+
+
+    public static async Task<world> GetRandomWorld(IDb con, IDictionary<string, object?>? shared = null)
+    {
+        int id = Random.Shared.Next(1, 10001);
+        shared ??= new DpSingleBuilder().Build();
+        shared["id"] = id;
+
+        world? obj = await con.Find(SqlSelect, shared).Toworld();
+        return obj!;
+    }
+    public static async Task<world[]> GetWorlds(IDb con, int count)
+    {
+        world[] lst = new world[count];
+        var dp = new DpSingleBuilder().Build();
+        for (int i = 0; i < count; i++)
+        {
+            world obj = await GetRandomWorld(con, dp);
+            lst[i] = obj;
+        }
+        return lst;
+    }
+    public static async Task SaveWorlds(IDb con, world[] lst)
+    {
+        await con.Execute(SqlUpdate, lst.ToDp());
+    }
+}