EncodingBenchmarks.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Text;
  2. using Apq.Cfg.EncodingSupport;
  3. using Apq.Cfg.Sources.File;
  4. using BenchmarkDotNet.Attributes;
  5. namespace Apq.Cfg.Benchmarks;
  6. /// <summary>
  7. /// 编码检测性能测试
  8. /// </summary>
  9. [MemoryDiagnoser]
  10. [RankColumn]
  11. public class EncodingBenchmarks
  12. {
  13. private string _testDir = null!;
  14. private string _utf8NoBomFile = null!;
  15. private string _utf8BomFile = null!;
  16. private string _utf16LeFile = null!;
  17. private string _gb2312File = null!;
  18. private string _largeUtf8File = null!;
  19. [GlobalSetup]
  20. public void Setup()
  21. {
  22. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  23. _testDir = Path.Combine(Path.GetTempPath(), $"ApqCfgEncodingBench_{Guid.NewGuid():N}");
  24. Directory.CreateDirectory(_testDir);
  25. // UTF-8 无 BOM
  26. _utf8NoBomFile = Path.Combine(_testDir, "utf8_nobom.json");
  27. File.WriteAllText(_utf8NoBomFile, """{"key": "value", "中文": "测试"}""", new UTF8Encoding(false));
  28. // UTF-8 有 BOM
  29. _utf8BomFile = Path.Combine(_testDir, "utf8_bom.json");
  30. File.WriteAllText(_utf8BomFile, """{"key": "value", "中文": "测试"}""", new UTF8Encoding(true));
  31. // UTF-16 LE
  32. _utf16LeFile = Path.Combine(_testDir, "utf16le.json");
  33. File.WriteAllText(_utf16LeFile, """{"key": "value", "中文": "测试"}""", Encoding.Unicode);
  34. // GB2312
  35. _gb2312File = Path.Combine(_testDir, "gb2312.json");
  36. File.WriteAllText(_gb2312File, """{"key": "value", "中文": "测试"}""", Encoding.GetEncoding("GB2312"));
  37. // 大文件 UTF-8
  38. _largeUtf8File = Path.Combine(_testDir, "large_utf8.json");
  39. var sb = new StringBuilder();
  40. sb.Append("{");
  41. for (int i = 0; i < 1000; i++)
  42. {
  43. if (i > 0) sb.Append(",");
  44. sb.Append($"\"key{i}\": \"value{i}中文测试\"");
  45. }
  46. sb.Append("}");
  47. File.WriteAllText(_largeUtf8File, sb.ToString(), new UTF8Encoding(true));
  48. }
  49. [GlobalCleanup]
  50. public void Cleanup()
  51. {
  52. if (Directory.Exists(_testDir))
  53. {
  54. try { Directory.Delete(_testDir, true); }
  55. catch { }
  56. }
  57. }
  58. // ========== BOM 检测测试 ==========
  59. [Benchmark(Description = "Detect_UTF8_WithBOM")]
  60. public EncodingDetectionResult Detect_UTF8_WithBOM()
  61. {
  62. return FileCfgSourceBase.EncodingDetector.Detect(_utf8BomFile);
  63. }
  64. [Benchmark(Description = "Detect_UTF16LE_WithBOM")]
  65. public EncodingDetectionResult Detect_UTF16LE_WithBOM()
  66. {
  67. return FileCfgSourceBase.EncodingDetector.Detect(_utf16LeFile);
  68. }
  69. // ========== 无 BOM 检测测试 ==========
  70. [Benchmark(Description = "Detect_UTF8_NoBOM")]
  71. public EncodingDetectionResult Detect_UTF8_NoBOM()
  72. {
  73. // 清除缓存以测试实际检测性能
  74. FileCfgSourceBase.EncodingDetector.ClearCache();
  75. return FileCfgSourceBase.EncodingDetector.Detect(_utf8NoBomFile);
  76. }
  77. [Benchmark(Description = "Detect_GB2312")]
  78. public EncodingDetectionResult Detect_GB2312()
  79. {
  80. FileCfgSourceBase.EncodingDetector.ClearCache();
  81. return FileCfgSourceBase.EncodingDetector.Detect(_gb2312File);
  82. }
  83. // ========== 缓存效果测试 ==========
  84. [Benchmark(Description = "Detect_Cached_1000")]
  85. public EncodingDetectionResult Detect_Cached_1000()
  86. {
  87. EncodingDetectionResult result = default!;
  88. for (int i = 0; i < 1000; i++)
  89. {
  90. result = FileCfgSourceBase.EncodingDetector.Detect(_utf8BomFile);
  91. }
  92. return result;
  93. }
  94. [Benchmark(Description = "Detect_Uncached_100")]
  95. public EncodingDetectionResult Detect_Uncached_100()
  96. {
  97. EncodingDetectionResult result = default!;
  98. for (int i = 0; i < 100; i++)
  99. {
  100. FileCfgSourceBase.EncodingDetector.ClearCache();
  101. result = FileCfgSourceBase.EncodingDetector.Detect(_utf8NoBomFile);
  102. }
  103. return result;
  104. }
  105. // ========== 大文件检测测试 ==========
  106. [Benchmark(Description = "Detect_LargeFile")]
  107. public EncodingDetectionResult Detect_LargeFile()
  108. {
  109. FileCfgSourceBase.EncodingDetector.ClearCache();
  110. return FileCfgSourceBase.EncodingDetector.Detect(_largeUtf8File);
  111. }
  112. // ========== 编码映射测试 ==========
  113. [Benchmark(Description = "Mapping_ExactPath_Lookup")]
  114. public Encoding? Mapping_ExactPath_Lookup()
  115. {
  116. return FileCfgSourceBase.EncodingDetector.MappingConfig.GetWriteEncoding(_utf8BomFile);
  117. }
  118. [Benchmark(Description = "Mapping_Wildcard_Lookup")]
  119. public Encoding? Mapping_Wildcard_Lookup()
  120. {
  121. // 假设已配置 *.json 的通配符映射
  122. return FileCfgSourceBase.EncodingDetector.MappingConfig.GetWriteEncoding(
  123. Path.Combine(_testDir, "test.json"));
  124. }
  125. // ========== 混合场景测试 ==========
  126. [Benchmark(Description = "Detect_MixedEncodings_10")]
  127. public void Detect_MixedEncodings_10()
  128. {
  129. FileCfgSourceBase.EncodingDetector.ClearCache();
  130. FileCfgSourceBase.EncodingDetector.Detect(_utf8NoBomFile);
  131. FileCfgSourceBase.EncodingDetector.Detect(_utf8BomFile);
  132. FileCfgSourceBase.EncodingDetector.Detect(_utf16LeFile);
  133. FileCfgSourceBase.EncodingDetector.Detect(_gb2312File);
  134. FileCfgSourceBase.EncodingDetector.Detect(_utf8NoBomFile);
  135. FileCfgSourceBase.EncodingDetector.Detect(_utf8BomFile);
  136. FileCfgSourceBase.EncodingDetector.Detect(_utf16LeFile);
  137. FileCfgSourceBase.EncodingDetector.Detect(_gb2312File);
  138. FileCfgSourceBase.EncodingDetector.Detect(_utf8NoBomFile);
  139. FileCfgSourceBase.EncodingDetector.Detect(_utf8BomFile);
  140. }
  141. }