瀏覽代碼

性能测试中包含.env文件

黄中银 5 天之前
父節點
當前提交
5beba71fa9

+ 1 - 0
benchmarks/Apq.Cfg.Benchmarks/Apq.Cfg.Benchmarks.csproj

@@ -26,6 +26,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\..\Apq.Cfg\Apq.Cfg.csproj" />
+    <ProjectReference Include="..\..\Apq.Cfg.Env\Apq.Cfg.Env.csproj" />
     <ProjectReference Include="..\..\Apq.Cfg.Ini\Apq.Cfg.Ini.csproj" />
     <ProjectReference Include="..\..\Apq.Cfg.Xml\Apq.Cfg.Xml.csproj" />
     <ProjectReference Include="..\..\Apq.Cfg.Yaml\Apq.Cfg.Yaml.csproj" />

+ 34 - 0
benchmarks/Apq.Cfg.Benchmarks/ReadWriteBenchmarks.cs

@@ -1,4 +1,5 @@
 using BenchmarkDotNet.Attributes;
+using Apq.Cfg.Env;
 using Apq.Cfg.Ini;
 using Apq.Cfg.Xml;
 using Apq.Cfg.Yaml;
@@ -15,6 +16,7 @@ public class ReadWriteBenchmarks : IDisposable
 {
     private readonly string _testDir;
     private ICfgRoot _jsonCfg = null!;
+    private ICfgRoot _envCfg = null!;
     private ICfgRoot _iniCfg = null!;
     private ICfgRoot _xmlCfg = null!;
     private ICfgRoot _yamlCfg = null!;
@@ -50,6 +52,21 @@ public class ReadWriteBenchmarks : IDisposable
             .AddJson(jsonPath, level: 0, writeable: true, isPrimaryWriter: true)
             .Build();
 
+        // 创建 .env 配置文件
+        var envPath = Path.Combine(_testDir, ".env");
+        File.WriteAllText(envPath, """
+            DATABASE_HOST=localhost
+            DATABASE_PORT=5432
+            DATABASE_NAME=testdb
+            APP_NAME=BenchmarkApp
+            APP_VERSION=1.0.0
+            APP_MAXRETRIES=3
+            APP_ENABLED=true
+            """);
+        _envCfg = new CfgBuilder()
+            .AddEnv(envPath, level: 0, writeable: true, isPrimaryWriter: true)
+            .Build();
+
         // 创建 INI 配置文件
         var iniPath = Path.Combine(_testDir, "config.ini");
         File.WriteAllText(iniPath, """
@@ -135,6 +152,7 @@ public class ReadWriteBenchmarks : IDisposable
     public void Dispose()
     {
         _jsonCfg?.Dispose();
+        _envCfg?.Dispose();
         _iniCfg?.Dispose();
         _xmlCfg?.Dispose();
         _yamlCfg?.Dispose();
@@ -152,6 +170,10 @@ public class ReadWriteBenchmarks : IDisposable
     [BenchmarkCategory("Get")]
     public string? Json_Get() => _jsonCfg.Get("Database:Host");
 
+    [Benchmark]
+    [BenchmarkCategory("Get")]
+    public string? Env_Get() => _envCfg.Get("DATABASE_HOST");
+
     [Benchmark]
     [BenchmarkCategory("Get")]
     public string? Ini_Get() => _iniCfg.Get("Database:Host");
@@ -176,6 +198,10 @@ public class ReadWriteBenchmarks : IDisposable
     [BenchmarkCategory("GetTyped")]
     public int Json_GetInt() => _jsonCfg.Get<int>("Database:Port");
 
+    [Benchmark]
+    [BenchmarkCategory("GetTyped")]
+    public int Env_GetInt() => _envCfg.Get<int>("DATABASE_PORT");
+
     [Benchmark]
     [BenchmarkCategory("GetTyped")]
     public int Ini_GetInt() => _iniCfg.Get<int>("Database:Port");
@@ -200,6 +226,10 @@ public class ReadWriteBenchmarks : IDisposable
     [BenchmarkCategory("Exists")]
     public bool Json_Exists() => _jsonCfg.Exists("Database:Host");
 
+    [Benchmark]
+    [BenchmarkCategory("Exists")]
+    public bool Env_Exists() => _envCfg.Exists("DATABASE_HOST");
+
     [Benchmark]
     [BenchmarkCategory("Exists")]
     public bool Ini_Exists() => _iniCfg.Exists("Database:Host");
@@ -224,6 +254,10 @@ public class ReadWriteBenchmarks : IDisposable
     [BenchmarkCategory("Set")]
     public void Json_Set() => _jsonCfg.Set("App:TempKey", "TempValue");
 
+    [Benchmark]
+    [BenchmarkCategory("Set")]
+    public void Env_Set() => _envCfg.Set("APP_TEMPKEY", "TempValue");
+
     [Benchmark]
     [BenchmarkCategory("Set")]
     public void Ini_Set() => _iniCfg.Set("App:TempKey", "TempValue");

+ 5 - 5
benchmarks/README.md

@@ -85,14 +85,14 @@ dotnet run -c Release --project benchmarks/Apq.Cfg.Benchmarks -f net9.0 -- --lis
 
 ### 1. ReadWriteBenchmarks - 读写性能测试
 
-测试不同配置源(JSON/INI/XML/YAML/TOML)的基本操作性能:
+测试不同配置源(JSON/Env/INI/XML/YAML/TOML)的基本操作性能:
 
 | 测试方法 | 说明 |
 |----------|------|
-| Json/Ini/Xml/Yaml/Toml_Get | 读取字符串值 |
-| Json/Ini/Xml/Yaml/Toml_GetInt | 读取并转换类型(如 int) |
-| Json/Ini/Xml/Yaml/Toml_Exists | 检查键是否存在 |
-| Json/Ini/Xml/Yaml/Toml_Set | 写入配置值 |
+| Json/Env/Ini/Xml/Yaml/Toml_Get | 读取字符串值 |
+| Json/Env/Ini/Xml/Yaml/Toml_GetInt | 读取并转换类型(如 int) |
+| Json/Env/Ini/Xml/Yaml/Toml_Exists | 检查键是否存在 |
+| Json/Env/Ini/Xml/Yaml/Toml_Set | 写入配置值 |
 
 ### 2. LargeFileBenchmarks - 大文件加载测试