testing google snappy

This commit is contained in:
minjaesong
2023-12-20 16:11:56 +09:00
parent d6e024974b
commit c0c41adcee

View File

@@ -1,5 +1,7 @@
package net.torvald.terrarum.tests package net.torvald.terrarum.tests
import io.airlift.compress.snappy.SnappyFramedInputStream
import io.airlift.compress.snappy.SnappyFramedOutputStream
import io.airlift.compress.zstd.ZstdInputStream import io.airlift.compress.zstd.ZstdInputStream
import io.airlift.compress.zstd.ZstdOutputStream import io.airlift.compress.zstd.ZstdOutputStream
import net.torvald.random.HQRNG import net.torvald.random.HQRNG
@@ -8,6 +10,8 @@ import net.torvald.terrarum.realestate.LandUtil.CHUNK_W
import net.torvald.terrarum.savegame.ByteArray64 import net.torvald.terrarum.savegame.ByteArray64
import net.torvald.terrarum.savegame.ByteArray64GrowableOutputStream import net.torvald.terrarum.savegame.ByteArray64GrowableOutputStream
import net.torvald.terrarum.savegame.ByteArray64InputStream import net.torvald.terrarum.savegame.ByteArray64InputStream
import java.io.InputStream
import java.io.OutputStream
import java.util.zip.GZIPInputStream import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream import java.util.zip.GZIPOutputStream
import kotlin.math.roundToInt import kotlin.math.roundToInt
@@ -72,10 +76,11 @@ class ZipTest(val mode: String) {
private val testInput0 = Array(TEST_COUNT) { dataGenerator(CHUNKSIZE) } private val testInput0 = Array(TEST_COUNT) { dataGenerator(CHUNKSIZE) }
private val testInputG = testInput0.copyOf().also { it.shuffle() } private val testInputG = testInput0.copyOf().also { it.shuffle() }
private val testInputZ = testInput0.copyOf().also { it.shuffle() } private val testInputZ = testInput0.copyOf().also { it.shuffle() }
private val testInputS = testInput0.copyOf().also { it.shuffle() }
private fun compGzip(bytes: ByteArray64): ByteArray64 { private inline fun _comp(bytes: ByteArray64, zf: (ByteArray64GrowableOutputStream) -> OutputStream): ByteArray64 {
val bo = ByteArray64GrowableOutputStream() val bo = ByteArray64GrowableOutputStream()
val zo = GZIPOutputStream(bo) val zo = zf(bo)
bytes.iterator().forEach { bytes.iterator().forEach {
zo.write(it.toInt()) zo.write(it.toInt())
@@ -83,10 +88,9 @@ class ZipTest(val mode: String) {
zo.flush(); zo.close() zo.flush(); zo.close()
return bo.toByteArray64() return bo.toByteArray64()
} }
private fun _decomp(bytes: ByteArray64, zf: (ByteArray64InputStream) -> InputStream): ByteArray64 {
private fun decompGzip(bytes: ByteArray64): ByteArray64 {
val unzipdBytes = ByteArray64() val unzipdBytes = ByteArray64()
val zi = GZIPInputStream(ByteArray64InputStream(bytes)) val zi = zf(ByteArray64InputStream(bytes))
while (true) { while (true) {
val byte = zi.read() val byte = zi.read()
if (byte == -1) break if (byte == -1) break
@@ -96,74 +100,74 @@ class ZipTest(val mode: String) {
return unzipdBytes return unzipdBytes
} }
private fun compZstd(bytes: ByteArray64): ByteArray64 { private fun compGzip(bytes: ByteArray64) = _comp(bytes) { GZIPOutputStream(it) }
val bo = ByteArray64GrowableOutputStream() private fun decompGzip(bytes: ByteArray64) = _decomp(bytes) { GZIPInputStream(it) }
val zo = ZstdOutputStream(bo)
bytes.iterator().forEach { private fun compZstd(bytes: ByteArray64) = _comp(bytes) { ZstdOutputStream(it) }
zo.write(it.toInt()) private fun decompZstd(bytes: ByteArray64) = _decomp(bytes) { ZstdInputStream(it) }
}
zo.flush();zo.close()
return bo.toByteArray64()
}
private fun decompZstd(bytes: ByteArray64): ByteArray64 { private fun compSnappy(bytes: ByteArray64) = _comp(bytes) { SnappyFramedOutputStream(it) }
val unzipdBytes = ByteArray64() private fun decompSnappy(bytes: ByteArray64) = _decomp(bytes) { SnappyFramedInputStream(it) }
val zi = ZstdInputStream(ByteArray64InputStream(bytes))
while (true) {
val byte = zi.read()
if (byte == -1) break
unzipdBytes.appendByte(byte.toByte())
}
zi.close()
return unzipdBytes
}
fun main() { fun main() {
val compBufG = arrayOfNulls<ByteArray64>(TEST_COUNT) val compBufG = arrayOfNulls<ByteArray64>(TEST_COUNT)
val compBufZ = arrayOfNulls<ByteArray64>(TEST_COUNT) val compBufZ = arrayOfNulls<ByteArray64>(TEST_COUNT)
val compBufS = arrayOfNulls<ByteArray64>(TEST_COUNT)
val decompBufG = arrayOfNulls<ByteArray64>(TEST_COUNT) val decompBufG = arrayOfNulls<ByteArray64>(TEST_COUNT)
val decompBufZ = arrayOfNulls<ByteArray64>(TEST_COUNT) val decompBufZ = arrayOfNulls<ByteArray64>(TEST_COUNT)
val decompBufS = arrayOfNulls<ByteArray64>(TEST_COUNT)
// println("Compressing $TEST_COUNT samples of $CHUNKSIZE bytes using Gzip")
val gzipCompTime = measureNanoTime { val gzipCompTime = measureNanoTime {
for (i in 0 until TEST_COUNT) { for (i in 0 until TEST_COUNT) {
compBufG[i] = compGzip(testInputG[i]) compBufG[i] = compGzip(testInputG[i])
} }
} }
// println("Decompressing $TEST_COUNT samples of $CHUNKSIZE bytes using Gzip")
val gzipDecompTime = measureNanoTime { val gzipDecompTime = measureNanoTime {
for (i in 0 until TEST_COUNT) { for (i in 0 until TEST_COUNT) {
decompBufG[i] = decompGzip(compBufG[i]!!) decompBufG[i] = decompGzip(compBufG[i]!!)
} }
} }
// println("Compressing $TEST_COUNT samples of $CHUNKSIZE bytes using Zstd")
val zstdCompTime = measureNanoTime { val zstdCompTime = measureNanoTime {
for (i in 0 until TEST_COUNT) { for (i in 0 until TEST_COUNT) {
compBufZ[i] = compZstd(testInputZ[i]) compBufZ[i] = compZstd(testInputZ[i])
} }
} }
// println("Decompressing $TEST_COUNT samples of $CHUNKSIZE bytes using Zstd")
val zstdDecompTime = measureNanoTime { val zstdDecompTime = measureNanoTime {
for (i in 0 until TEST_COUNT) { for (i in 0 until TEST_COUNT) {
decompBufZ[i] = decompZstd(compBufZ[i]!!) decompBufZ[i] = decompZstd(compBufZ[i]!!)
} }
} }
val snappyCompTime = measureNanoTime {
for (i in 0 until TEST_COUNT) {
compBufS[i] = compSnappy(testInputS[i])
}
}
val snappyDecompTime = measureNanoTime {
for (i in 0 until TEST_COUNT) {
decompBufS[i] = decompSnappy(compBufS[i]!!)
}
}
val compSizeG = compBufG.sumOf { it!!.size } / TEST_COUNT val compSizeG = compBufG.sumOf { it!!.size } / TEST_COUNT
val compSizeZ = compBufZ.sumOf { it!!.size } / TEST_COUNT val compSizeZ = compBufZ.sumOf { it!!.size } / TEST_COUNT
val compSizeS = compBufS.sumOf { it!!.size } / TEST_COUNT
val origSize = testInput0.sumOf { it.size } / TEST_COUNT val origSize = testInput0.sumOf { it.size } / TEST_COUNT
val ratioG = ((1.0 - (compSizeG.toDouble() / origSize)) * 10000).roundToInt() / 100 val ratioG = ((1.0 - (compSizeG.toDouble() / origSize)) * 10000).roundToInt() / 100
val ratioZ = ((1.0 - (compSizeZ.toDouble() / origSize)) * 10000).roundToInt() / 100 val ratioZ = ((1.0 - (compSizeZ.toDouble() / origSize)) * 10000).roundToInt() / 100
val ratioS = ((1.0 - (compSizeS.toDouble() / origSize)) * 10000).roundToInt() / 100
println("==== $mode Data ($origSize bytes x $TEST_COUNT samples) ====") println("==== $mode Data ($origSize bytes x $TEST_COUNT samples) ====")
println("Gzip comp: $gzipCompTime ns") println("Gzip comp: $gzipCompTime ns")
println("Gzip decomp: $gzipDecompTime ns; ratio: $ratioG% (avr size: $compSizeG)") println("Gzip decomp: $gzipDecompTime ns; ratio: $ratioG% (avr size: $compSizeG)")
println("Zstd comp: $zstdCompTime ns") println("Zstd comp: $zstdCompTime ns")
println("Zstd decomp: $zstdDecompTime ns; ratio: $ratioZ% (avr size: $compSizeZ)") println("Zstd decomp: $zstdDecompTime ns; ratio: $ratioZ% (avr size: $compSizeZ)")
println("Snpy comp: $snappyCompTime ns")
println("Snpy decomp: $snappyDecompTime ns; ratio: $ratioS% (avr size: $compSizeS)")
println() println()
} }
} }