public void When_compressing_bytes() { using (ICompressor compressor = new LZ4Compressor()) { var compressedBytesStd = compressor.Compress(_inputBytes); var compressedBytesAggr = compressor.Compress(_inputBytes, CompressionLevel.Aggressive); compressedBytesStd.ShouldBe(compressedBytesAggr); compressor.DeCompress(compressedBytesStd).ShouldBe(_inputBytes); compressor.DeCompress(compressedBytesAggr).ShouldBe(_inputBytes); } }
public void When_compressing_string_with_default_method() { using (ICompressor compressor = new LZ4Compressor()) { var compressedBytesStd = compressor.Compress(InputStr); var compressedBytesAggr = compressor.Compress(InputStr, CompressionLevel.Aggressive); compressedBytesStd.ShouldBe(compressedBytesAggr); compressor.DeCompressAsString(compressedBytesStd).ShouldBe(InputStr); compressor.DeCompressAsString(compressedBytesAggr).ShouldBe(InputStr); compressor.DeCompress(compressedBytesStd).ShouldBe(_inputBytes); compressor.DeCompress(compressedBytesAggr).ShouldBe(_inputBytes); } }
public void When_compressing_a_stream_aggresive() { var inputBytes = Encoding.UTF8.GetBytes(InputStr); using (var streamIn = new MemoryStream(inputBytes)) using (ICompressor compressor = new LZ4Compressor()) using (var compressedStream = new MemoryStream()) { compressedStream.Length.ShouldBe(0); compressor.Compress(streamIn, compressedStream, CompressionLevel.Aggressive); var compressedBytes = compressedStream.ToArray(); compressedBytes.Length.ShouldBe(84); compressedStream.Position = 0; using (var deCompressedStream = new MemoryStream()) { compressor.DeCompress(compressedStream, deCompressedStream); var deCompressedBytes = deCompressedStream.ToArray(); deCompressedBytes.ShouldBe(_inputBytes); } compressor.DeCompressAsString(compressedBytes, Encoding.UTF8).ShouldBe(InputStr); } }
static void Main(string[] args) { var lz4 = new LZ4Compressor(); var snappy = new SnappyCompressor(); for (var i = 0; i < 100; i++) { var model = new WorldState2() { Players = Enumerable.Range(0, 1000).Select(_ => 1).ToArray() }; byte[] serialized; using (var m = new MemoryStream()) { ProtoBuf.Serializer.NonGeneric.Serialize(m, model); serialized = m.ToArray(); } var lz4Length = lz4.Compress(serialized).Length; var snappLength = snappy.Compress(serialized).Length; Console.WriteLine($"{serialized.Length}\t{lz4Length}\t{snappLength}\t{lz4Length - snappLength}"); } Console.ReadLine(); }
public void When_compressing_string_with_encoding() { using (ICompressor compressor = new LZ4Compressor()) { var inputBytes = Encoding.UTF32.GetBytes(InputStr); var compressedBytesStd = compressor.Compress(inputBytes); var compressedBytesAggr = compressor.Compress(inputBytes, CompressionLevel.Aggressive); compressedBytesStd.ShouldNotBe(compressedBytesAggr); compressor.DeCompressAsString(compressedBytesStd, Encoding.UTF32).ShouldBe(InputStr); compressor.DeCompressAsString(compressedBytesAggr, Encoding.UTF32).ShouldBe(InputStr); compressor.DeCompress(compressedBytesStd).ShouldBe(inputBytes); compressor.DeCompress(compressedBytesAggr).ShouldBe(inputBytes); } }
private static void DeCompressStreamLZ4() { using (var compressor = new LZ4Compressor()) { for (var i = 0; i < IterationCount; i++) { compressor.Compress(_bytes); } } }
public async Task <byte[]> Compress(CompressionType type, byte[] uncompressed) { if (type != CompressionType.LZ4) { throw new ArgumentException("Only LZ4 Compression is supported at the moment."); } return(await Task.Run(() => { LZ4Compressor comp = new LZ4Compressor("", K4os.Compression.LZ4.LZ4Level.L12_MAX); return comp.Compress(uncompressed); })); }
internal void LZ4Compressor_CanCompressAndDecompress() { // Arrange var message = "hello world!"; var compressor = new LZ4Compressor(); // Act var messageBytes = Encoding.UTF8.GetBytes(message); var compressed = compressor.Compress(messageBytes); var decompressed = compressor.Decompress(compressed); // Assert Assert.Equal(message, Encoding.UTF8.GetString(decompressed)); this.Output.WriteLine(BitConverter.ToString(compressed)); }
public void LZ4_Read_Write_Faster_Than_GZip() { var gzipCompressor = new GZipCompressor(); var lz4Compressor = new LZ4Compressor(); double gzipSeconds = 0; double lz4Seconds = 0; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000; i++) { byte[] data = TestHelper.GetRandomByteArray(10); byte[] compressed = gzipCompressor.Compress(data); byte[] deflated = gzipCompressor.Decompress(compressed); Assert.IsTrue(data.SequenceEqual(deflated)); } watch.Stop(); gzipSeconds = watch.Elapsed.TotalSeconds; watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000; i++) { byte[] data = TestHelper.GetRandomByteArray(10); byte[] compressed = lz4Compressor.Compress(data); byte[] deflated = lz4Compressor.Decompress(compressed); Assert.IsTrue(data.SequenceEqual(deflated)); } watch.Stop(); lz4Seconds = watch.Elapsed.TotalSeconds; Assert.IsTrue(lz4Seconds < gzipSeconds); }