Last active
June 14, 2021 08:43
-
-
Save sankarge/cf20d61e5baf357cdae8587338227d3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.Arrays; | |
import java.util.zip.GZIPInputStream; | |
import java.util.zip.GZIPOutputStream; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import org.springframework.util.ResourceUtils; | |
import com.github.luben.zstd.Zstd; | |
import net.jpountz.lz4.LZ4Compressor; | |
import net.jpountz.lz4.LZ4Factory; | |
import net.jpountz.lz4.LZ4FastDecompressor; | |
class CompareCompressionTest { | |
@Test | |
void testCompression() throws IOException { | |
System.out.println("small"); | |
String test2 = "small"; | |
test_gzip(test2); | |
test_lz4(test2); | |
test_zstd(test2); | |
System.out.println("medium"); | |
String test1 = "medium"; | |
test_gzip(test1); | |
test_lz4(test1); | |
test_zstd(test1); | |
System.out.println("large"); | |
String test3 = "large"; | |
test_gzip(test3); | |
test_lz4(test3); | |
test_zstd(test3); | |
} | |
void test_zstd(String fileName) throws IOException { | |
byte[] data = getRawBytes(fileName); | |
long t1 = System.currentTimeMillis(); | |
final int decompressedLength = data.length; | |
byte[] compressed = Zstd.compress(data); | |
long t2 = System.currentTimeMillis(); | |
byte[] decompressed = Zstd.decompress(compressed, decompressedLength); | |
long t3 = System.currentTimeMillis(); | |
Assertions.assertTrue(Arrays.equals(data, decompressed)); | |
stats("zstd", data, compressed, t1, t2, t3); | |
} | |
void test_lz4(String fileName) throws IOException { | |
byte[] data = getRawBytes(fileName); | |
long t1 = System.currentTimeMillis(); | |
LZ4Factory factory = LZ4Factory.fastestInstance(); | |
final int decompressedLength = data.length; | |
// compress data | |
LZ4Compressor compressor = factory.fastCompressor(); | |
int maxCompressedLength = compressor.maxCompressedLength(decompressedLength); | |
byte[] compressed = new byte[maxCompressedLength]; | |
int compressLen = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength); | |
byte[] finalCompressedArray = Arrays.copyOf(compressed, compressLen); | |
long t2 = System.currentTimeMillis(); | |
// decompress data | |
LZ4FastDecompressor decompressor = factory.fastDecompressor(); | |
byte[] decompressed = new byte[decompressedLength]; | |
decompressor.decompress(compressed, 0, decompressed, 0, decompressedLength); | |
long t3 = System.currentTimeMillis(); | |
Assertions.assertTrue(Arrays.equals(data, decompressed)); | |
stats("lz4", data, finalCompressedArray, t1, t2, t3); | |
} | |
void test_gzip(String fileName) throws IOException { | |
byte[] data = getRawBytes(fileName); | |
long t1 = System.currentTimeMillis(); | |
// compress data | |
ByteArrayOutputStream out = new ByteArrayOutputStream(data.length); | |
GZIPOutputStream gzip = new GZIPOutputStream(out); | |
gzip.write(data); | |
gzip.close(); | |
byte[] compressed = out.toByteArray(); | |
long t2 = System.currentTimeMillis(); | |
// decompress data | |
ByteArrayOutputStream outGzip = new ByteArrayOutputStream(); | |
GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(compressed)); | |
byte[] buffer = new byte[1024]; | |
int len; | |
while ((len = gzipIS.read(buffer)) != -1) { | |
outGzip.write(buffer, 0, len); | |
} | |
byte[] decompressed = outGzip.toByteArray(); | |
long t3 = System.currentTimeMillis(); | |
Assertions.assertTrue(Arrays.equals(data, decompressed)); | |
stats("gzip", data, compressed, t1, t2, t3); | |
} | |
private byte[] getRawBytes(String fileName) throws IOException { | |
File file = ResourceUtils.getFile(fileName + ".json"); | |
return Files.readAllBytes(file.toPath()); | |
} | |
public void stats(String type, byte[] data, byte[] compressed, long t1, long t2, long t3) { | |
float ratio = ((float) data.length / (float) compressed.length); | |
System.out.println("Type:" + type | |
+ ", Orig_size: " + data.length | |
+ ", Compressed: " + compressed.length | |
+ ", Ratio: " + ratio | |
+ ", Comp_time: " + (t2 - t1) | |
+ ", Decom_time: " + (t3 - t2) | |
+ ", Total_time: " + (t3 - t1) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<!-- existing dependencies --> | |
<dependency> | |
<groupId>com.github.luben</groupId> | |
<artifactId>zstd-jni</artifactId> | |
<version>1.4.4-7</version> | |
<classifier>linux_amd64</classifier> | |
</dependency> | |
<dependency> | |
<groupId>org.lz4</groupId> | |
<artifactId>lz4-java</artifactId> | |
<version>1.7.1</version> | |
</dependency> | |
<dependency> | |
<dependencies> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment