From 195da921fc49aadc640464358b18b606794e3de2 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 11 Nov 2023 15:47:12 +0900 Subject: [PATCH] refactor: all test ok --- src/DotFastLZ.Benchmark/Benchmark.cs | 5 - src/DotFastLZ.Benchmark/BenchmarkDotFastLZ.cs | 40 ++++ src/DotFastLZ.Benchmark/BenchmarkK4osLZ4.cs | 54 +++++ src/DotFastLZ.Benchmark/BenchmarkSystemZip.cs | 50 +++++ src/DotFastLZ.Benchmark/Benchmarks.cs | 81 ++++++- src/DotFastLZ.Benchmark/Program.cs | 203 +++--------------- 6 files changed, 247 insertions(+), 186 deletions(-) delete mode 100644 src/DotFastLZ.Benchmark/Benchmark.cs create mode 100644 src/DotFastLZ.Benchmark/BenchmarkDotFastLZ.cs create mode 100644 src/DotFastLZ.Benchmark/BenchmarkK4osLZ4.cs create mode 100644 src/DotFastLZ.Benchmark/BenchmarkSystemZip.cs diff --git a/src/DotFastLZ.Benchmark/Benchmark.cs b/src/DotFastLZ.Benchmark/Benchmark.cs deleted file mode 100644 index 2a683c5..0000000 --- a/src/DotFastLZ.Benchmark/Benchmark.cs +++ /dev/null @@ -1,5 +0,0 @@ -using System; -using System.Diagnostics; - -namespace DotFastLZ.Benchmark; - diff --git a/src/DotFastLZ.Benchmark/BenchmarkDotFastLZ.cs b/src/DotFastLZ.Benchmark/BenchmarkDotFastLZ.cs new file mode 100644 index 0000000..25489cd --- /dev/null +++ b/src/DotFastLZ.Benchmark/BenchmarkDotFastLZ.cs @@ -0,0 +1,40 @@ +using DotFastLZ.Compression; + +namespace DotFastLZ.Benchmark; + +public class BenchmarkDotFastLZ : IBenchmark +{ + public string Name => _name; + private string _name; + private int _level; + + public BenchmarkDotFastLZ(int level) + { + _name = $"DotFastLZ L{level}"; + _level = level; + } + + public BenchmarkResult Start(string filename, byte[] srcBytes, byte[] dstBytes) + { + var compress = CompressDotFastLZL1; + if (_level == 2) + compress = CompressDotFastLZL2; + + return Benchmarks.Start(Name, filename, srcBytes, dstBytes, compress, DecompressDotFastLZ); + } + + private static long CompressDotFastLZL1(byte[] srcBytes, byte[] dstBytes) + { + return FastLZ.CompressLevel1(srcBytes, 0, srcBytes.Length, dstBytes); + } + + private static long CompressDotFastLZL2(byte[] srcBytes, byte[] dstBytes) + { + return FastLZ.CompressLevel2(srcBytes, 0, srcBytes.Length, dstBytes); + } + + private static long DecompressDotFastLZ(byte[] srcBytes, long size, byte[] dstBytes) + { + return FastLZ.Decompress(srcBytes, size, dstBytes, dstBytes.Length); + } +} \ No newline at end of file diff --git a/src/DotFastLZ.Benchmark/BenchmarkK4osLZ4.cs b/src/DotFastLZ.Benchmark/BenchmarkK4osLZ4.cs new file mode 100644 index 0000000..584107f --- /dev/null +++ b/src/DotFastLZ.Benchmark/BenchmarkK4osLZ4.cs @@ -0,0 +1,54 @@ +using System; +using K4os.Compression.LZ4; + +namespace DotFastLZ.Benchmark; + +public class BenchmarkK4osLZ4 : IBenchmark +{ + public string Name => _name; + + private string _name; + private LZ4Level _level; + + public BenchmarkK4osLZ4(LZ4Level level) + { + _level = level; + _name = $"K4os.LZ L{_level.ToString()}"; + + // // K4os LZ4 + // var k4osL01 = Benchmark($"K4os.Compression.LZ4 L00", filename, + // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L00_FAST), DecompressK4osLZ4); + // var k4osL12 = Benchmark($"K4os.Compression.LZ4 L12", filename, + // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L12_MAX), DecompressK4osLZ4); + // var k4osL03HC = Benchmark($"K4os.Compression.LZ4 L03_HC", filename, + // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L03_HC), DecompressK4osLZ4); + // var k4osL09HC = Benchmark($"K4os.Compression.LZ4 L09_HC", filename, + // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L09_HC), DecompressK4osLZ4); + // results.Add(k4osL01); + // results.Add(k4osL12); + // results.Add(k4osL03HC); + // results.Add(k4osL09HC); + // + } + + + public BenchmarkResult Start(string filename, byte[] srcBytes, byte[] dstBytes) + { + return Benchmarks.Start(Name, filename, srcBytes, dstBytes, + (s, d) => CompressK4osLZ4(s, d, _level), DecompressK4osLZ4); + } + + private static long CompressK4osLZ4(byte[] srcBytes, byte[] dstBytes, LZ4Level level) + { + var writer = new FixedArrayBufferWriter(dstBytes); + LZ4Pickler.Pickle(srcBytes, writer, level); + return writer.WrittenCount; + } + + private static long DecompressK4osLZ4(byte[] srcBytes, long size, byte[] dstBytes) + { + var writer = new FixedArrayBufferWriter(dstBytes); + LZ4Pickler.Unpickle(srcBytes.AsSpan(0, (int)size), writer); + return writer.WrittenCount; + } +} \ No newline at end of file diff --git a/src/DotFastLZ.Benchmark/BenchmarkSystemZip.cs b/src/DotFastLZ.Benchmark/BenchmarkSystemZip.cs new file mode 100644 index 0000000..bd9e09c --- /dev/null +++ b/src/DotFastLZ.Benchmark/BenchmarkSystemZip.cs @@ -0,0 +1,50 @@ +using System; +using System.IO; +using System.IO.Compression; + +namespace DotFastLZ.Benchmark; + +public class BenchmarkSystemZip : IBenchmark +{ + public string Name => _name; + + private string _name; + private CompressionLevel _level; + + public BenchmarkSystemZip(CompressionLevel level) + { + _level = level; + _name = $"System.Io.Zip {_level.ToString()}"; + } + + public BenchmarkResult Start(string filename, byte[] srcBytes, byte[] dstBytes) + { + return Benchmarks.Start(Name, filename, srcBytes, dstBytes, (s, d) => CompressZip(s, d, _level), DecompressZip); + } + + private static long CompressZip(byte[] srcBytes, byte[] dstBytes, CompressionLevel level) + { + using var ms = new MemoryStream(); + using (var zip = new ZipArchive(ms, ZipArchiveMode.Create)) + { + var entry = zip.CreateEntry("test", level); + using var s = entry.Open(); + s.Write(srcBytes, 0, srcBytes.Length); + } + + var ssss = ms.ToArray().AsSpan(); + ssss.CopyTo(dstBytes); + return ssss.Length; + } + + private static long DecompressZip(byte[] srcBytes, long size, byte[] dstBytes) + { + using var readStream = new MemoryStream(srcBytes, 0, (int)size); + using var zip = new ZipArchive(readStream, ZipArchiveMode.Read); + var entry = zip.Entries[0]; + using var entryStream = entry.Open(); + using MemoryStream writeStream = new MemoryStream(dstBytes); + entryStream.CopyTo(writeStream); + return writeStream.Position; + } +} \ No newline at end of file diff --git a/src/DotFastLZ.Benchmark/Benchmarks.cs b/src/DotFastLZ.Benchmark/Benchmarks.cs index 89ffc8c..0611c6c 100644 --- a/src/DotFastLZ.Benchmark/Benchmarks.cs +++ b/src/DotFastLZ.Benchmark/Benchmarks.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; namespace DotFastLZ.Benchmark; @@ -10,15 +12,15 @@ public static BenchmarkResult Start(string name, string filename, byte[] srcByte var result = new BenchmarkResult(); result.Name = name; result.FileName = filename; + result.Times = 5; result.SourceByteLength = srcBytes.Length; result.Compression.ElapsedWatch = new Stopwatch(); result.Compression.ElapsedWatch.Start(); - for (int i = 0; i < 5; ++i) + for (int i = 0; i < result.Times; ++i) { long size = compress.Invoke(srcBytes, dstBytes); - result.Compression.InputBytes += result.SourceByteLength; + result.Compression.InputBytes += srcBytes.Length; result.Compression.OutputBytes += size; - result.Times += 1; } result.Compression.ElapsedWatch.Stop(); @@ -28,12 +30,11 @@ public static BenchmarkResult Start(string name, string filename, byte[] srcByte result.Decompression.ElapsedWatch = new Stopwatch(); result.Decompression.ElapsedWatch.Start(); - for (int i = 0; i < 5; ++i) + for (int i = 0; i < result.Times; ++i) { long size = decompress.Invoke(dstBytes, decompInputLength, srcBytes); result.Decompression.InputBytes += decompInputLength; result.Decompression.OutputBytes += size; - result.Times += 1; } result.Decompression.ElapsedWatch.Stop(); @@ -41,4 +42,72 @@ public static BenchmarkResult Start(string name, string filename, byte[] srcByte //Console.WriteLine(result.ToString()); return result; } -} + + public static void Print(string headline, List results) + { + var rows = results + .OrderByDescending(x => x.ComputeTotalSpeed()) + .ToList(); + + + // 각 열의 최대 길이를 찾기 위한 작업 + int[] widths = new int[BenchmarkResult.CollSize]; + for (int i = 0; i < rows.Count; i++) + { + widths[0] = Math.Max(rows[i].Name.Length, widths[0]); + widths[1] = Math.Max(rows[i].FileName.Length, widths[1]); + widths[2] = Math.Max(rows[i].ToSourceKbString().Length, widths[2]); + widths[3] = Math.Max(rows[i].Compression.ToSpeedString().Length, widths[3]); + widths[4] = Math.Max(rows[i].Decompression.ToSpeedString().Length, widths[4]); + widths[5] = Math.Max(rows[i].Compression.ToRateString().Length, widths[5]); + } + + var headName = "Name"; + var headFilename = "Filename"; + var headFileSize = "File kB"; + var headCompMbs = "Comp. MB/s"; + var headDecompMbs = "Decomp. MB/s"; + var headRate = "Rate"; + + widths[0] = Math.Max(widths[0], headName.Length) + 1; + widths[1] = Math.Max(widths[1], headFilename.Length) + 1; + widths[2] = Math.Max(widths[2], headFileSize.Length) + 1; + widths[3] = Math.Max(widths[3], headCompMbs.Length) + 1; + widths[4] = Math.Max(widths[4], headDecompMbs.Length) + 1; + widths[5] = Math.Max(widths[5], headRate.Length) + 1; + + Console.WriteLine(); + Console.WriteLine($"### {headline} ###"); + Console.WriteLine(); + + + // 표 출력 + Console.WriteLine("| " + + headName + new string(' ', widths[0] - headName.Length) + "| " + + headFilename + new string(' ', widths[1] - headFilename.Length) + "| " + + headFileSize + new string(' ', widths[2] - headFileSize.Length) + "| " + + headCompMbs + new string(' ', widths[3] - headCompMbs.Length) + "| " + + headDecompMbs + new string(' ', widths[4] - headDecompMbs.Length) + "| " + + headRate + new string(' ', widths[5] - headRate.Length) + "|"); + Console.WriteLine("|-" + + new string('-', widths[0]) + "|-" + + new string('-', widths[1]) + "|-" + + new string('-', widths[2]) + "|-" + + new string('-', widths[3]) + "|-" + + new string('-', widths[4]) + "|-" + + new string('-', widths[5]) + "|"); + + foreach (var row in rows) + { + Console.WriteLine( + "| " + + row.Name.PadRight(widths[0]) + "| " + + row.FileName.PadRight(widths[1]) + "| " + + row.ToSourceKbString().PadRight(widths[2]) + "| " + + row.Compression.ToSpeedString().PadRight(widths[3]) + "| " + + row.Decompression.ToSpeedString().PadRight(widths[4]) + "| " + + row.Compression.ToRateString().PadRight(widths[5]) + "|" + ); + } + } +} \ No newline at end of file diff --git a/src/DotFastLZ.Benchmark/Program.cs b/src/DotFastLZ.Benchmark/Program.cs index 9daa961..162f5a8 100644 --- a/src/DotFastLZ.Benchmark/Program.cs +++ b/src/DotFastLZ.Benchmark/Program.cs @@ -1,11 +1,9 @@ using System; using System.IO; -using System.IO.Compression; using System.Linq; using System.Collections.Generic; -using DotFastLZ.Compression; +using System.IO.Compression; using DotFastLZ.Resource; - using K4os.Compression.LZ4; namespace DotFastLZ.Benchmark; @@ -18,16 +16,38 @@ public static void Main(string[] args) { R.ExtractAll(); - var benchmarks = new List(); + var benchmakrs = new List(); + + benchmakrs.Add(new BenchmarkMemCopy()); + + benchmakrs.Add(new BenchmarkDotFastLZ(1)); + benchmakrs.Add(new BenchmarkDotFastLZ(2)); + + benchmakrs.Add(new BenchmarkK4osLZ4(LZ4Level.L00_FAST)); + benchmakrs.Add(new BenchmarkK4osLZ4(LZ4Level.L03_HC)); + benchmakrs.Add(new BenchmarkK4osLZ4(LZ4Level.L09_HC)); + benchmakrs.Add(new BenchmarkK4osLZ4(LZ4Level.L12_MAX)); + + benchmakrs.Add(new BenchmarkSystemZip(CompressionLevel.Fastest)); + benchmakrs.Add(new BenchmarkSystemZip(CompressionLevel.Optimal)); - var totalResults = new List(); + var results = new List(); foreach (var file in R.SourceFiles) { - var results = BenchmarkFile(file); - totalResults.AddRange(results); + var filepath = R.Find(Path.Combine(R.Prefix, file)); + var srcBytes = R.ToBytes(filepath); + var dstBytes = new byte[srcBytes.Length * 2]; + + foreach (var benchmark in benchmakrs) + { + var result = benchmark.Start(file, srcBytes.ToArray(), dstBytes); + results.Add(result); + + Console.WriteLine(result.ToString()); + } } - Print($"Benchmark", totalResults); + Benchmarks.Print($"Benchmark", results); } catch (Exception e) { @@ -39,171 +59,4 @@ public static void Main(string[] args) R.DeleteAll(); } } - - private static List BenchmarkFile(string filename) - { - var results = new List(); - - var filepath = R.Find(Path.Combine(R.Prefix, filename)); - var srcBytes = R.ToBytes(filepath); - var dstBytes = new byte[srcBytes.Length * 2]; - - // memcpy - var benchmark = new BenchmarkMemCopy(); - var memoryCopy = benchmark.Start(filename, srcBytes.ToArray(), dstBytes); - results.Add(memoryCopy); - - // // DotFastLZ fastlz - // var fastlzLv1 = Benchmark($"DotFastLZ.Compression L1", filename, srcBytes.ToArray(), dstBytes, CompressDotFastLZL1, DecompressDotFastLZ); - // var fastlzLv2 = Benchmark($"DotFastLZ.Compression L2", filename, srcBytes.ToArray(), dstBytes, CompressDotFastLZL2, DecompressDotFastLZ); - // results.Add(fastlzLv1); - // results.Add(fastlzLv2); - // - // // K4os LZ4 - // var k4osL01 = Benchmark($"K4os.Compression.LZ4 L00", filename, - // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L00_FAST), DecompressK4osLZ4); - // var k4osL12 = Benchmark($"K4os.Compression.LZ4 L12", filename, - // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L12_MAX), DecompressK4osLZ4); - // var k4osL03HC = Benchmark($"K4os.Compression.LZ4 L03_HC", filename, - // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L03_HC), DecompressK4osLZ4); - // var k4osL09HC = Benchmark($"K4os.Compression.LZ4 L09_HC", filename, - // srcBytes.ToArray(), dstBytes, (s, d) => CompressK4osLZ4(s, d, LZ4Level.L09_HC), DecompressK4osLZ4); - // results.Add(k4osL01); - // results.Add(k4osL12); - // results.Add(k4osL03HC); - // results.Add(k4osL09HC); - // - // var zipFastest = Benchmark($"System.IO.Compression.ZipArchive Fastest", filename, - // srcBytes.ToArray(), dstBytes, (s, d) => CompressZip(s, d, CompressionLevel.Fastest), DecompressZip); - // var zipOptimal = Benchmark($"System.IO.Compression.ZipArchive Optimal", filename, - // srcBytes.ToArray(), dstBytes, (s, d) => CompressZip(s, d, CompressionLevel.Optimal), DecompressZip); - // results.Add(zipFastest); - // results.Add(zipOptimal); - - return results; - } - - - private static long CompressZip(byte[] srcBytes, byte[] dstBytes, CompressionLevel level) - { - using var ms = new MemoryStream(); - using (var zip = new ZipArchive(ms, ZipArchiveMode.Create)) - { - var entry = zip.CreateEntry("test", level); - using var s = entry.Open(); - s.Write(srcBytes, 0, srcBytes.Length); - } - - var ssss = ms.ToArray().AsSpan(); - ssss.CopyTo(dstBytes); - return ssss.Length; - } - - private static long DecompressZip(byte[] srcBytes, long size, byte[] dstBytes) - { - using var readStream = new MemoryStream(srcBytes, 0, (int)size); - using var zip = new ZipArchive(readStream, ZipArchiveMode.Read); - var entry = zip.Entries[0]; - using var entryStream = entry.Open(); - using MemoryStream writeStream = new MemoryStream(dstBytes); - entryStream.CopyTo(writeStream); - return writeStream.Position; - } - - private static long CompressDotFastLZL1(byte[] srcBytes, byte[] dstBytes) - { - return FastLZ.CompressLevel1(srcBytes, 0, srcBytes.Length, dstBytes); - } - - private static long CompressDotFastLZL2(byte[] srcBytes, byte[] dstBytes) - { - return FastLZ.CompressLevel2(srcBytes, 0, srcBytes.Length, dstBytes); - } - - private static long DecompressDotFastLZ(byte[] srcBytes, long size, byte[] dstBytes) - { - return FastLZ.Decompress(srcBytes, size, dstBytes, dstBytes.Length); - } - - private static long CompressK4osLZ4(byte[] srcBytes, byte[] dstBytes, LZ4Level level) - { - var writer = new FixedArrayBufferWriter(dstBytes); - LZ4Pickler.Pickle(srcBytes, writer, level); - return writer.WrittenCount; - } - - private static long DecompressK4osLZ4(byte[] srcBytes, long size, byte[] dstBytes) - { - var writer = new FixedArrayBufferWriter(dstBytes); - LZ4Pickler.Unpickle(srcBytes.AsSpan(0, (int)size), writer); - return writer.WrittenCount; - } - - private static void Print(string headline, List results) - { - var rows = results - .OrderByDescending(x => x.ComputeTotalSpeed()) - .ToList(); - - - // 각 열의 최대 길이를 찾기 위한 작업 - int[] widths = new int[BenchmarkResult.CollSize]; - for (int i = 0; i < rows.Count; i++) - { - widths[0] = Math.Max(rows[i].Name.Length, widths[0]); - widths[1] = Math.Max(rows[i].FileName.Length, widths[1]); - widths[2] = Math.Max(rows[i].ToSourceKbString().Length, widths[2]); - widths[3] = Math.Max(rows[i].Compression.ToSpeedString().Length, widths[3]); - widths[4] = Math.Max(rows[i].Decompression.ToSpeedString().Length, widths[4]); - widths[5] = Math.Max(rows[i].Compression.ToRateString().Length, widths[5]); - } - - var headName = "Name"; - var headFilename = "Filename"; - var headFileSize = "File kB"; - var headCompMbs = "Comp. MB/s"; - var headDecompMbs = "Decomp. MB/s"; - var headRate = "Rate"; - - widths[0] = Math.Max(widths[0], headName.Length) + 1; - widths[1] = Math.Max(widths[1], headFilename.Length) + 1; - widths[2] = Math.Max(widths[2], headFileSize.Length) + 1; - widths[3] = Math.Max(widths[3], headCompMbs.Length) + 1; - widths[4] = Math.Max(widths[4], headDecompMbs.Length) + 1; - widths[5] = Math.Max(widths[5], headRate.Length) + 1; - - Console.WriteLine(); - Console.WriteLine($"### {headline} ###"); - Console.WriteLine(); - - - // 표 출력 - Console.WriteLine("| " + - headName + new string(' ', widths[0] - headName.Length) + "| " + - headFilename + new string(' ', widths[1] - headFilename.Length) + "| " + - headFileSize + new string(' ', widths[2] - headFileSize.Length) + "| " + - headCompMbs + new string(' ', widths[3] - headCompMbs.Length) + "| " + - headDecompMbs + new string(' ', widths[4] - headDecompMbs.Length) + "| " + - headRate + new string(' ', widths[5] - headRate.Length) + "|"); - Console.WriteLine("|-" + - new string('-', widths[0]) + "|-" + - new string('-', widths[1]) + "|-" + - new string('-', widths[2]) + "|-" + - new string('-', widths[3]) + "|-" + - new string('-', widths[4]) + "|-" + - new string('-', widths[5]) + "|"); - - foreach (var row in rows) - { - Console.WriteLine( - "| " + - row.Name.PadRight(widths[0]) + "| " + - row.FileName.PadRight(widths[1]) + "| " + - row.ToSourceKbString().PadRight(widths[2]) + "| " + - row.Compression.ToSpeedString().PadRight(widths[3]) + "| " + - row.Decompression.ToSpeedString().PadRight(widths[4]) + "| " + - row.Compression.ToRateString().PadRight(widths[5]) + "|" - ); - } - } } \ No newline at end of file