Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ied206 committed Apr 5, 2021
2 parents 8abbd0c + 5f7a839 commit 8190ddd
Show file tree
Hide file tree
Showing 87 changed files with 556 additions and 1,460 deletions.
33 changes: 33 additions & 0 deletions Benchmark/BenchConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Filters;
using System.Collections.Generic;

namespace Benchmark
{
public class BenchConfig : ManualConfig
{
public const string ZLib = "ZLib";
public const string XZ = "XZ";
public const string LZ4 = "LZ4";

public BenchConfig()
{
switch (Program.Opts.Algorithms)
{
case AlgorithmFlags.None:
case AlgorithmFlags.All:
return;
}

List<string> categories = new List<string>();
if (Program.Opts.Algorithms.HasFlag(AlgorithmFlags.ZLib))
categories.Add(ZLib);
else if (Program.Opts.Algorithms.HasFlag(AlgorithmFlags.XZ))
categories.Add(XZ);
else if (Program.Opts.Algorithms.HasFlag(AlgorithmFlags.LZ4))
categories.Add(LZ4);

AddFilter(new AnyCategoriesFilter(categories.ToArray()));
}
}
}
97 changes: 68 additions & 29 deletions Benchmark/Program.cs → Benchmark/BenchProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@
namespace Benchmark
{
#region Parameter
[Flags]
public enum AlgorithmFlags
{
None = 0x0,
ZLib = 0x1,
XZ = 0x2,
LZ4 = 0x4,
All = ZLib | XZ | LZ4,
}

public abstract class ParamOptions
{
[Option("algo", Default = AlgorithmFlags.All, HelpText = "Choose algorithms to benchmark | zlib,xz,lz4,all")]
public AlgorithmFlags Algorithms { get; set; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Cast<T>() where T : ParamOptions
{
Expand Down Expand Up @@ -59,52 +72,75 @@ internal static void PrintErrorAndExit(IEnumerable<Error> errs)
#region Init and Cleanup
public static void NativeGlobalInit()
{
const string x64 = "x64";
const string x86 = "x86";
const string armhf = "armhf";
const string arm64 = "arm64";
const string runtimes = "runtimes";
const string native = "native";

string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string baseDir = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", ".."));

string zlibPath = null;
string xzPath = null;
string lz4Path = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string libDir;
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X86:
libDir = Path.Combine(baseDir, runtimes, "win-x86", native);
break;
case Architecture.X64:
zlibPath = Path.Combine(baseDir, x64, "zlibwapi.dll");
xzPath = Path.Combine(baseDir, x64, "liblzma.dll");
lz4Path = Path.Combine(baseDir, x64, "liblz4.dll");
libDir = Path.Combine(baseDir, runtimes, "win-x64", native);
break;
case Architecture.X86:
zlibPath = Path.Combine(baseDir, x86, "zlibwapi.dll");
xzPath = Path.Combine(baseDir, x86, "liblzma.dll");
lz4Path = Path.Combine(baseDir, x86, "liblz4.dll");
case Architecture.Arm64:
libDir = Path.Combine(baseDir, runtimes, "win-arm64", native);
break;
default:
throw new PlatformNotSupportedException();
}

zlibPath = Path.Combine(libDir, "zlibwapi.dll");
xzPath = Path.Combine(libDir, "liblzma.dll");
lz4Path = Path.Combine(libDir, "liblz4.dll");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
string libDir;
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X64:
zlibPath = Path.Combine(baseDir, x64, "libz.so");
xzPath = Path.Combine(baseDir, x64, "liblzma.so");
lz4Path = Path.Combine(baseDir, x64, "liblz4.so");
libDir = Path.Combine(baseDir, runtimes, "linux-x64", native);
break;
case Architecture.Arm:
zlibPath = Path.Combine(baseDir, armhf, "libz.so");
xzPath = Path.Combine(baseDir, armhf, "liblzma.so");
lz4Path = Path.Combine(baseDir, armhf, "liblz4.so");
libDir = Path.Combine(baseDir, runtimes, "linux-arm", native);
break;
case Architecture.Arm64:
zlibPath = Path.Combine(baseDir, arm64, "libz.so");
xzPath = Path.Combine(baseDir, arm64, "liblzma.so");
lz4Path = Path.Combine(baseDir, arm64, "liblz4.so");
libDir = Path.Combine(baseDir, runtimes, "linux-arm64", native);
break;
default:
throw new PlatformNotSupportedException();
}

zlibPath = Path.Combine(libDir, "libz.so");
xzPath = Path.Combine(libDir, "liblzma.so");
lz4Path = Path.Combine(libDir, "liblz4.so");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string libDir;
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X64:
libDir = Path.Combine(baseDir, runtimes, "osx-x64", native);
break;
case Architecture.Arm64:
throw new PlatformNotSupportedException("TODO");
default:
throw new PlatformNotSupportedException();
}

zlibPath = Path.Combine(libDir, "libz.dylib");
xzPath = Path.Combine(libDir, "liblzma.dylib");
lz4Path = Path.Combine(libDir, "liblz4.dylib");
}

if (zlibPath == null || xzPath == null || lz4Path == null)
Expand All @@ -123,10 +159,13 @@ public static void NativeGlobalCleanup()
}
#endregion

#region Commnad Line Options
public static ParamOptions Opts { get; private set; }
#endregion

#region Main
public static void Main(string[] args)
{
ParamOptions opts = null;
Parser argParser = new Parser(conf =>
{
conf.HelpWriter = Console.Out;
Expand All @@ -136,15 +175,15 @@ public static void Main(string[] args)

argParser.ParseArguments<AllBenchOptions,
CompBenchOptions, DecompBenchOptions, HashBenchOptions, BufferSizeBenchOptions>(args)
.WithParsed<AllBenchOptions>(x => opts = x)
.WithParsed<CompBenchOptions>(x => opts = x)
.WithParsed<DecompBenchOptions>(x => opts = x)
.WithParsed<HashBenchOptions>(x => opts = x)
.WithParsed<BufferSizeBenchOptions>(x => opts = x)
.WithParsed<AllBenchOptions>(x => Opts = x)
.WithParsed<CompBenchOptions>(x => Opts = x)
.WithParsed<DecompBenchOptions>(x => Opts = x)
.WithParsed<HashBenchOptions>(x => Opts = x)
.WithParsed<BufferSizeBenchOptions>(x => Opts = x)
.WithNotParsed(PrintErrorAndExit);
Debug.Assert(opts != null, $"{nameof(opts)} != null");
Debug.Assert(Opts != null, $"{nameof(Opts)} != null");

switch (opts)
switch (Opts)
{
case AllBenchOptions _:
BenchmarkRunner.Run<CompBench>();
Expand Down
6 changes: 3 additions & 3 deletions Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="Crc32.NET" Version="1.2.0" />
<PackageReference Include="InvertedTomato.Crc" Version="1.1.3" />
<PackageReference Include="K4os.Compression.LZ4.Streams" Version="1.1.11" />
<PackageReference Include="K4os.Compression.LZ4.Streams" Version="1.2.6" />
<PackageReference Include="K4os.Hash.Crc" Version="1.1.4" />
<PackageReference Include="K4os.Hash.xxHash" Version="1.0.6" />
<PackageReference Include="SharpCompress" Version="0.25.1" />
<PackageReference Include="SharpCompress" Version="0.28.1" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion Benchmark/BufferSizeBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Benchmark
{
[Config(typeof(BenchConfig))]
public class BufferSizeBench
{
#region Fields and Properties
Expand All @@ -13,11 +14,14 @@ public class BufferSizeBench
public int BufferSize { get; set; }
public IReadOnlyList<int> BufferSizes { get; set; } = new int[]
{
4 * 1024,
16 * 1024,
32 * 1024,
64 * 1024,
128 * 1024,
256 * 1024,
512 * 1024,
1024 * 1024,
2 * 1024 * 1024,
4 * 1024 * 1024,
};

Expand Down Expand Up @@ -83,6 +87,7 @@ public void GlobalCleanup()

#region LZ4
[Benchmark]
[BenchmarkCategory(BenchConfig.LZ4)]
public void LZ4()
{
Joveler.Compression.LZ4.LZ4FrameCompressOptions compOpts = new Joveler.Compression.LZ4.LZ4FrameCompressOptions()
Expand Down Expand Up @@ -119,6 +124,7 @@ public void LZ4()

#region XZ
[Benchmark]
[BenchmarkCategory(BenchConfig.XZ)]
public void XZ()
{
Joveler.Compression.XZ.XZCompressOptions compOpts = new Joveler.Compression.XZ.XZCompressOptions()
Expand Down Expand Up @@ -155,6 +161,7 @@ public void XZ()

#region ZLib
[Benchmark]
[BenchmarkCategory(BenchConfig.ZLib)]
public void ZLib()
{
Joveler.Compression.ZLib.ZLibCompressOptions compOpts = new Joveler.Compression.ZLib.ZLibCompressOptions()
Expand Down
6 changes: 6 additions & 0 deletions Benchmark/CompBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Benchmark
{
#region CompBench
[Config(typeof(BenchConfig))]
public class CompBench
{
private string _sampleDir;
Expand Down Expand Up @@ -106,6 +107,7 @@ public void GlobalCleanup()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.LZ4)]
public double LZ4_Native()
{
Joveler.Compression.LZ4.LZ4FrameCompressOptions compOpts = new Joveler.Compression.LZ4.LZ4FrameCompressOptions()
Expand Down Expand Up @@ -134,6 +136,7 @@ public double LZ4_Native()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.LZ4)]
public double LZ4_Managed()
{
long compLen;
Expand All @@ -155,6 +158,7 @@ public double LZ4_Managed()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.ZLib)]
public double ZLib_Native()
{
long compLen;
Expand Down Expand Up @@ -182,6 +186,7 @@ public double ZLib_Native()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.ZLib)]
public double ZLib_Managed()
{
long compLen;
Expand All @@ -201,6 +206,7 @@ public double ZLib_Managed()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.XZ)]
public double XZ_Native()
{
long compLen;
Expand Down
7 changes: 7 additions & 0 deletions Benchmark/DecompBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Benchmark
{
#region DecompBench
[Config(typeof(BenchConfig))]
public class DecompBench
{
private string _sampleDir;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void GlobalCleanup()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.LZ4)]
public long LZ4_Native()
{
Joveler.Compression.LZ4.LZ4FrameDecompressOptions decompOpts = new Joveler.Compression.LZ4.LZ4FrameDecompressOptions();
Expand All @@ -88,6 +90,7 @@ public long LZ4_Native()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.LZ4)]
public long LZ4_Managed()
{
byte[] compData = SrcFiles[$"{Level}_{SrcFileName}.lz4"];
Expand All @@ -103,6 +106,7 @@ public long LZ4_Managed()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.ZLib)]
public long ZLib_Native()
{
Joveler.Compression.ZLib.ZLibDecompressOptions decompOpts = new Joveler.Compression.ZLib.ZLibDecompressOptions();
Expand All @@ -120,6 +124,7 @@ public long ZLib_Native()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.ZLib)]
public long ZLib_Managed()
{
byte[] compData = SrcFiles[$"{Level}_{SrcFileName}.zz"];
Expand All @@ -135,6 +140,7 @@ public long ZLib_Managed()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.XZ)]
public long XZ_Native()
{
byte[] compData = SrcFiles[$"{Level}_{SrcFileName}.xz"];
Expand All @@ -151,6 +157,7 @@ public long XZ_Native()
}

[Benchmark]
[BenchmarkCategory(BenchConfig.XZ)]
public long XZ_Managed()
{
byte[] compData = SrcFiles[$"{Level}_{SrcFileName}.xz"];
Expand Down
Loading

0 comments on commit 8190ddd

Please sign in to comment.