From d5001ff47d87f820ed8442be861106e8f66163da Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 29 Oct 2023 12:40:21 +0900 Subject: [PATCH] feat: Benchmark template --- DotFastLZ.sln | 7 ++++++ src/DotFastLZ.Benchmark/BenchmarkResult.cs | 8 ++++++ .../DotFastLZ.Benchmark.csproj | 12 +++++++++ src/DotFastLZ.Benchmark/Program.cs | 25 +++++++++++++++++-- .../DotFastLZ.Resource.csproj | 7 ++++++ .../DotFastLZ.Resource/R.cs | 15 +++++------ .../DotFastLZ.Resource}/SourceZip.cs | 6 ++--- .../DotFastLZ.Compression.Tests.csproj | 1 + .../Fixtures/TestHelper.cs | 9 ++++--- .../RoundTripTests.cs | 21 ++++++++-------- .../DotFastLZ.Packaging.Tests/SixPackTests.cs | 15 +++++------ 11 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 src/DotFastLZ.Benchmark/BenchmarkResult.cs create mode 100644 src/DotFastLZ.Resource/DotFastLZ.Resource.csproj rename test/DotFastLZ.Compression.Tests/Fixtures/ResourceHelper.cs => src/DotFastLZ.Resource/R.cs (95%) rename {test/DotFastLZ.Compression.Tests/Fixtures => src/DotFastLZ.Resource}/SourceZip.cs (91%) diff --git a/DotFastLZ.sln b/DotFastLZ.sln index d258e28..751d604 100644 --- a/DotFastLZ.sln +++ b/DotFastLZ.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotFastLZ.Packaging.Tests", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotFastLZ.Benchmark", "src\DotFastLZ.Benchmark\DotFastLZ.Benchmark.csproj", "{6B8B8BA7-9039-43BD-9837-EEF2957AF5AD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotFastLZ.Resource", "src\DotFastLZ.Resource\DotFastLZ.Resource.csproj", "{A64E2087-E40B-45C0-9084-F00A213D8FBC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -52,6 +54,10 @@ Global {6B8B8BA7-9039-43BD-9837-EEF2957AF5AD}.Debug|Any CPU.Build.0 = Debug|Any CPU {6B8B8BA7-9039-43BD-9837-EEF2957AF5AD}.Release|Any CPU.ActiveCfg = Release|Any CPU {6B8B8BA7-9039-43BD-9837-EEF2957AF5AD}.Release|Any CPU.Build.0 = Release|Any CPU + {A64E2087-E40B-45C0-9084-F00A213D8FBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A64E2087-E40B-45C0-9084-F00A213D8FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A64E2087-E40B-45C0-9084-F00A213D8FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A64E2087-E40B-45C0-9084-F00A213D8FBC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {C20491BC-E472-4F09-B29C-DD7BF853C1C4} = {642F4CE7-0431-49D4-9BDE-E663C13C7792} @@ -60,5 +66,6 @@ Global {95E2DEFC-180E-4EAC-84CD-583DE1AAD587} = {642F4CE7-0431-49D4-9BDE-E663C13C7792} {15F8F8E3-2D89-4487-AAFB-3B617470342E} = {87EE9A81-1074-4466-AF83-6A059185A8E9} {6B8B8BA7-9039-43BD-9837-EEF2957AF5AD} = {642F4CE7-0431-49D4-9BDE-E663C13C7792} + {A64E2087-E40B-45C0-9084-F00A213D8FBC} = {642F4CE7-0431-49D4-9BDE-E663C13C7792} EndGlobalSection EndGlobal diff --git a/src/DotFastLZ.Benchmark/BenchmarkResult.cs b/src/DotFastLZ.Benchmark/BenchmarkResult.cs new file mode 100644 index 0000000..9d94e22 --- /dev/null +++ b/src/DotFastLZ.Benchmark/BenchmarkResult.cs @@ -0,0 +1,8 @@ +namespace DotFastLZ.Benchmark; + +public class BenchmarkResult +{ + public int Times; + public long StartTicks; + public long EndTicks; +} \ No newline at end of file diff --git a/src/DotFastLZ.Benchmark/DotFastLZ.Benchmark.csproj b/src/DotFastLZ.Benchmark/DotFastLZ.Benchmark.csproj index 120e38c..6403c45 100644 --- a/src/DotFastLZ.Benchmark/DotFastLZ.Benchmark.csproj +++ b/src/DotFastLZ.Benchmark/DotFastLZ.Benchmark.csproj @@ -5,4 +5,16 @@ net7.0 + + + + + + + + + + + + diff --git a/src/DotFastLZ.Benchmark/Program.cs b/src/DotFastLZ.Benchmark/Program.cs index 849d980..7c124b1 100644 --- a/src/DotFastLZ.Benchmark/Program.cs +++ b/src/DotFastLZ.Benchmark/Program.cs @@ -1,9 +1,30 @@ using System; +using DotFastLZ.Resource; -public class Program +namespace DotFastLZ.Benchmark; + +public static class Program { public static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + try + { + R.ExtractAll(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + finally + { + R.DeleteAll(); + } + + } + + public static BenchmarkResult Benchmark(string file, int count) + { + return null; } } \ No newline at end of file diff --git a/src/DotFastLZ.Resource/DotFastLZ.Resource.csproj b/src/DotFastLZ.Resource/DotFastLZ.Resource.csproj new file mode 100644 index 0000000..8268829 --- /dev/null +++ b/src/DotFastLZ.Resource/DotFastLZ.Resource.csproj @@ -0,0 +1,7 @@ + + + + net7.0 + + + diff --git a/test/DotFastLZ.Compression.Tests/Fixtures/ResourceHelper.cs b/src/DotFastLZ.Resource/R.cs similarity index 95% rename from test/DotFastLZ.Compression.Tests/Fixtures/ResourceHelper.cs rename to src/DotFastLZ.Resource/R.cs index caf0b29..4bcc70e 100644 --- a/test/DotFastLZ.Compression.Tests/Fixtures/ResourceHelper.cs +++ b/src/DotFastLZ.Resource/R.cs @@ -28,9 +28,9 @@ THE SOFTWARE. using System.Security.Cryptography; using System.Text; -namespace DotFastLZ.Compression.Tests.Fixtures; +namespace DotFastLZ.Resource; -public static class ResourceHelper +public static class R { public const string Prefix = "compression-corpus"; @@ -94,7 +94,7 @@ public static string Find(string pathName) return Path.GetFullPath(pathName); } - + public static int ExtractZipFile(string zipFilePath, string destDir) { int count = 0; @@ -102,7 +102,7 @@ public static int ExtractZipFile(string zipFilePath, string destDir) { if (string.IsNullOrEmpty(destDir)) destDir = string.Empty; - + if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); @@ -112,9 +112,10 @@ public static int ExtractZipFile(string zipFilePath, string destDir) foreach (ZipArchiveEntry entry in archive.Entries) { // https://cwe.mitre.org/data/definitions/22.html - string destFileName = Path.Combine(destDir, entry.FullName); string fullDestDirPath = Path.GetFullPath(destDir + Path.DirectorySeparatorChar); - if (!destFileName.StartsWith(fullDestDirPath)) { + string destFileName = Path.GetFullPath(Path.Combine(destDir, entry.FullName)); + if (!destFileName.StartsWith(fullDestDirPath)) + { throw new InvalidOperationException($"Entry is outside the target dir: {destFileName}"); } @@ -203,7 +204,7 @@ public static void ExtractAll() } } - public static void RemoveAll() + public static void DeleteAll() { var path = Find(Prefix); if (!string.IsNullOrEmpty(path)) diff --git a/test/DotFastLZ.Compression.Tests/Fixtures/SourceZip.cs b/src/DotFastLZ.Resource/SourceZip.cs similarity index 91% rename from test/DotFastLZ.Compression.Tests/Fixtures/SourceZip.cs rename to src/DotFastLZ.Resource/SourceZip.cs index 1f06311..9f7a74a 100644 --- a/test/DotFastLZ.Compression.Tests/Fixtures/SourceZip.cs +++ b/src/DotFastLZ.Resource/SourceZip.cs @@ -23,7 +23,7 @@ THE SOFTWARE. using System.IO; -namespace DotFastLZ.Compression.Tests.Fixtures; +namespace DotFastLZ.Resource; public class SourceZip { @@ -38,7 +38,7 @@ public SourceZip(string fileName, string extractPath) public void Extract(string extractRootPath) { - var zipFilePath = ResourceHelper.Find(_fileName); + var zipFilePath = R.Find(_fileName); var directoryName = Path.GetDirectoryName(zipFilePath); if (null == directoryName) { @@ -46,6 +46,6 @@ public void Extract(string extractRootPath) } var extractPath = Path.Combine(directoryName, extractRootPath, _extractPath); - ResourceHelper.ExtractZipFile(zipFilePath, extractPath); + R.ExtractZipFile(zipFilePath, extractPath); } } \ No newline at end of file diff --git a/test/DotFastLZ.Compression.Tests/DotFastLZ.Compression.Tests.csproj b/test/DotFastLZ.Compression.Tests/DotFastLZ.Compression.Tests.csproj index 5275248..0eebccd 100644 --- a/test/DotFastLZ.Compression.Tests/DotFastLZ.Compression.Tests.csproj +++ b/test/DotFastLZ.Compression.Tests/DotFastLZ.Compression.Tests.csproj @@ -19,6 +19,7 @@ + diff --git a/test/DotFastLZ.Compression.Tests/Fixtures/TestHelper.cs b/test/DotFastLZ.Compression.Tests/Fixtures/TestHelper.cs index a9f41ba..8d62725 100644 --- a/test/DotFastLZ.Compression.Tests/Fixtures/TestHelper.cs +++ b/test/DotFastLZ.Compression.Tests/Fixtures/TestHelper.cs @@ -24,6 +24,7 @@ THE SOFTWARE. using System; using System.IO; +using DotFastLZ.Resource; namespace DotFastLZ.Compression.Tests.Fixtures; @@ -86,7 +87,7 @@ public static bool test_ref_decompressor_level1(string name, string file_name) RefImpl.REF_Level1_decompress(compressed_buffer, compressed_size, uncompressed_buffer); Console.WriteLine("Comparing. Please wait..."); - long result = ResourceHelper.Compare(file_name, file_buffer, uncompressed_buffer, file_size); + long result = R.Compare(file_name, file_buffer, uncompressed_buffer, file_size); if (0 <= result) { Console.WriteLine($"failed to compare - {name} index({result})"); @@ -153,7 +154,7 @@ public static bool test_ref_decompressor_level2(string name, string file_name) RefImpl.REF_Level2_decompress(compressed_buffer, compressed_size, uncompressed_buffer); Console.WriteLine("Comparing. Please wait..."); - long result = ResourceHelper.Compare(file_name, file_buffer, uncompressed_buffer, file_size); + long result = R.Compare(file_name, file_buffer, uncompressed_buffer, file_size); if (0 <= result) { Console.WriteLine($"failed to compare - {name} index({result})"); @@ -220,7 +221,7 @@ public static bool test_roundtrip_level1(string name, string file_name) FastLZ.DecompressLevel1(compressed_buffer, 0, compressed_size, uncompressed_buffer, 0, uncompressed_buffer.Length); Console.WriteLine("Comparing. Please wait..."); - long result = ResourceHelper.Compare(file_name, file_buffer, uncompressed_buffer, file_size); + long result = R.Compare(file_name, file_buffer, uncompressed_buffer, file_size); if (0 <= result) { Console.WriteLine($"failed to compare - {name} index({result})"); @@ -288,7 +289,7 @@ public static bool test_roundtrip_level2(string name, string file_name) FastLZ.DecompressLevel2(compressed_buffer, 0, compressed_size, uncompressed_buffer, 0, uncompressed_buffer.Length); Console.WriteLine("Comparing. Please wait..."); - long result = ResourceHelper.Compare(file_name, file_buffer, uncompressed_buffer, file_size); + long result = R.Compare(file_name, file_buffer, uncompressed_buffer, file_size); if (0 <= result) { Console.WriteLine($"failed to compare - {name} index({result})"); diff --git a/test/DotFastLZ.Compression.Tests/RoundTripTests.cs b/test/DotFastLZ.Compression.Tests/RoundTripTests.cs index 52ce76b..397a8db 100644 --- a/test/DotFastLZ.Compression.Tests/RoundTripTests.cs +++ b/test/DotFastLZ.Compression.Tests/RoundTripTests.cs @@ -25,6 +25,7 @@ THE SOFTWARE. using System; using System.IO; using DotFastLZ.Compression.Tests.Fixtures; +using DotFastLZ.Resource; using NUnit.Framework; namespace DotFastLZ.Compression.Tests; @@ -36,23 +37,23 @@ public class RoundTripTests [OneTimeSetUp] public void OnSetUp() { - ResourceHelper.ExtractAll(); + R.ExtractAll(); } [OneTimeTearDown] public void OnTearDown() { // remove - ResourceHelper.RemoveAll(); + R.DeleteAll(); } [Test] public void TestRefDecompressorLevel1() { Console.WriteLine("Test reference decompressor for Level 1"); - foreach (var name in ResourceHelper.TestFiles) + foreach (var name in R.TestFiles) { - var filename = ResourceHelper.Find(Path.Combine(ResourceHelper.Prefix, name)); + var filename = R.Find(Path.Combine(R.Prefix, name)); bool result = TestHelper.test_ref_decompressor_level1(name, filename); Assert.That(result, Is.EqualTo(true), $"test_ref_decompressor_level1({name}, {filename})"); } @@ -62,9 +63,9 @@ public void TestRefDecompressorLevel1() public void TestRefDecompressorLevel2() { Console.WriteLine("Test reference decompressor for Level 2"); - foreach (var name in ResourceHelper.TestFiles) + foreach (var name in R.TestFiles) { - var filename = ResourceHelper.Find(Path.Combine(ResourceHelper.Prefix, name)); + var filename = R.Find(Path.Combine(R.Prefix, name)); bool result = TestHelper.test_ref_decompressor_level2(name, filename); Assert.That(result, Is.EqualTo(true), $"test_ref_decompressor_level2({name}, {filename})"); } @@ -75,9 +76,9 @@ public void TestRefDecompressorLevel2() public void TestRoundtripLevel1() { Console.WriteLine("Test round-trip for Level 1"); - foreach (var name in ResourceHelper.TestFiles) + foreach (var name in R.TestFiles) { - var filename = ResourceHelper.Find(Path.Combine(ResourceHelper.Prefix, name)); + var filename = R.Find(Path.Combine(R.Prefix, name)); bool result = TestHelper.test_roundtrip_level1(name, filename); Assert.That(result, Is.EqualTo(true), $"test_roundtrip_level1({name}, {filename})"); } @@ -87,9 +88,9 @@ public void TestRoundtripLevel1() public void TestRoundtripLevel2() { Console.WriteLine("Test round-trip for Level 2"); - foreach (var name in ResourceHelper.TestFiles) + foreach (var name in R.TestFiles) { - var filename = ResourceHelper.Find(Path.Combine(ResourceHelper.Prefix, name)); + var filename = R.Find(Path.Combine(R.Prefix, name)); var result = TestHelper.test_roundtrip_level2(name, filename); Assert.That(result, Is.EqualTo(true), $"test_roundtrip_level2({name}, {filename})"); } diff --git a/test/DotFastLZ.Packaging.Tests/SixPackTests.cs b/test/DotFastLZ.Packaging.Tests/SixPackTests.cs index 33af4c7..162eea0 100644 --- a/test/DotFastLZ.Packaging.Tests/SixPackTests.cs +++ b/test/DotFastLZ.Packaging.Tests/SixPackTests.cs @@ -5,6 +5,7 @@ using System.Security.Cryptography; using System.Text; using DotFastLZ.Compression.Tests.Fixtures; +using DotFastLZ.Resource; using NUnit.Framework; namespace DotFastLZ.Packaging.Tests; @@ -53,25 +54,25 @@ public void TestPackAndUnpack() File.Delete(fastlz1); File.Delete(fastlz2); - ResourceHelper.GenerateFile(filename, 1024 * 1024); + R.GenerateFile(filename, 1024 * 1024); // pack SixPack.PackFile(1, filename, fastlz1, Console.Write); SixPack.PackFile(2, filename, fastlz2, Console.Write); - var sourceMd5 = ResourceHelper.ComputeMD5(filename); + var sourceMd5 = R.ComputeMD5(filename); File.Delete(filename); Assert.That(sourceMd5, Is.EqualTo("90e4a4b78ebf7f88b02b0054ab0d6daa")); Assert.That(File.Exists(filename), Is.EqualTo(false)); // checksum - Assert.That(ResourceHelper.ComputeMD5(fastlz1), Is.EqualTo("6ca821bdf187f12bf23552133dfa99a1")); - Assert.That(ResourceHelper.ComputeMD5(fastlz2), Is.EqualTo("c70d787ea842eba36b7d1479b94c6740")); + Assert.That(R.ComputeMD5(fastlz1), Is.EqualTo("6ca821bdf187f12bf23552133dfa99a1")); + Assert.That(R.ComputeMD5(fastlz2), Is.EqualTo("c70d787ea842eba36b7d1479b94c6740")); // unpack level1 { int status1 = SixPack.UnpackFile(fastlz1, Console.Write); - var decompress1Md5 = ResourceHelper.ComputeMD5(filename); + var decompress1Md5 = R.ComputeMD5(filename); File.Delete(filename); File.Delete(fastlz1); @@ -83,7 +84,7 @@ public void TestPackAndUnpack() // unpack level2 { int status2 = SixPack.UnpackFile(fastlz2, Console.Write); - var decompress2Md5 = ResourceHelper.ComputeMD5(filename); + var decompress2Md5 = R.ComputeMD5(filename); File.Delete(filename); File.Delete(fastlz2); @@ -100,7 +101,7 @@ public void TestBenchmarkSpeed() const string benchmarkFileName = "benchmark.txt"; File.Delete(benchmarkFileName); - ResourceHelper.GenerateFile(benchmarkFileName, 1024 * 1024 * 8); + R.GenerateFile(benchmarkFileName, 1024 * 1024 * 8); int status1 = SixPack.BenchmarkSpeed(1, benchmarkFileName, Console.Write); int status2 = SixPack.BenchmarkSpeed(2, benchmarkFileName, Console.Write);