Skip to content

Commit

Permalink
refactor: all test ok
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Nov 11, 2023
1 parent aa6d4ab commit 195da92
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 186 deletions.
5 changes: 0 additions & 5 deletions src/DotFastLZ.Benchmark/Benchmark.cs

This file was deleted.

40 changes: 40 additions & 0 deletions src/DotFastLZ.Benchmark/BenchmarkDotFastLZ.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
54 changes: 54 additions & 0 deletions src/DotFastLZ.Benchmark/BenchmarkK4osLZ4.cs
Original file line number Diff line number Diff line change
@@ -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<byte>(dstBytes);
LZ4Pickler.Pickle(srcBytes, writer, level);
return writer.WrittenCount;
}

private static long DecompressK4osLZ4(byte[] srcBytes, long size, byte[] dstBytes)
{
var writer = new FixedArrayBufferWriter<byte>(dstBytes);
LZ4Pickler.Unpickle(srcBytes.AsSpan(0, (int)size), writer);
return writer.WrittenCount;
}
}
50 changes: 50 additions & 0 deletions src/DotFastLZ.Benchmark/BenchmarkSystemZip.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
81 changes: 75 additions & 6 deletions src/DotFastLZ.Benchmark/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace DotFastLZ.Benchmark;

Expand All @@ -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();
Expand All @@ -28,17 +30,84 @@ 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();

//Console.WriteLine(result.ToString());
return result;
}
}

public static void Print(string headline, List<BenchmarkResult> 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]) + "|"
);
}
}
}
Loading

0 comments on commit 195da92

Please sign in to comment.