Skip to content

Commit

Permalink
feat: Benchmark template
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Oct 29, 2023
1 parent b3ae38b commit d5001ff
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 33 deletions.
7 changes: 7 additions & 0 deletions DotFastLZ.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
8 changes: 8 additions & 0 deletions src/DotFastLZ.Benchmark/BenchmarkResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DotFastLZ.Benchmark;

public class BenchmarkResult
{
public int Times;
public long StartTicks;
public long EndTicks;
}
12 changes: 12 additions & 0 deletions src/DotFastLZ.Benchmark/DotFastLZ.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.6" />
</ItemGroup>

<ItemGroup>
<Folder Include="DotFastLZ.Resource\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DotFastLZ.Resource\DotFastLZ.Resource.csproj" />
</ItemGroup>

</Project>
25 changes: 23 additions & 2 deletions src/DotFastLZ.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions src/DotFastLZ.Resource/DotFastLZ.Resource.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -94,15 +94,15 @@ public static string Find(string pathName)

return Path.GetFullPath(pathName);
}

public static int ExtractZipFile(string zipFilePath, string destDir)
{
int count = 0;
try
{
if (string.IsNullOrEmpty(destDir))
destDir = string.Empty;

if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
Expand All @@ -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}");
}

Expand Down Expand Up @@ -203,7 +204,7 @@ public static void ExtractAll()
}
}

public static void RemoveAll()
public static void DeleteAll()
{
var path = Find(Prefix);
if (!string.IsNullOrEmpty(path))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ THE SOFTWARE.

using System.IO;

namespace DotFastLZ.Compression.Tests.Fixtures;
namespace DotFastLZ.Resource;

public class SourceZip
{
Expand All @@ -38,14 +38,14 @@ 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)
{
throw new DirectoryNotFoundException($"not found directoryName - {zipFilePath}");
}

var extractPath = Path.Combine(directoryName, extractRootPath, _extractPath);
ResourceHelper.ExtractZipFile(zipFilePath, extractPath);
R.ExtractZipFile(zipFilePath, extractPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\DotFastLZ.Compression\DotFastLZ.Compression.csproj" />
<ProjectReference Include="..\..\src\DotFastLZ.Resource\DotFastLZ.Resource.csproj" />
</ItemGroup>

</Project>
9 changes: 5 additions & 4 deletions test/DotFastLZ.Compression.Tests/Fixtures/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ THE SOFTWARE.

using System;
using System.IO;
using DotFastLZ.Resource;

namespace DotFastLZ.Compression.Tests.Fixtures;

Expand Down Expand Up @@ -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})");
Expand Down Expand Up @@ -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})");
Expand Down Expand Up @@ -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})");
Expand Down Expand Up @@ -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})");
Expand Down
21 changes: 11 additions & 10 deletions test/DotFastLZ.Compression.Tests/RoundTripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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})");
}
Expand All @@ -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})");
}
Expand All @@ -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})");
}
Expand All @@ -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})");
}
Expand Down
15 changes: 8 additions & 7 deletions test/DotFastLZ.Packaging.Tests/SixPackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit d5001ff

Please sign in to comment.