Last active
March 12, 2021 14:22
-
-
Save punchouty/8d7cf5d41e3d1bb054715c7a2c0dfa23 to your computer and use it in GitHub Desktop.
Proto size in bytes | Map Vs List
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
/** | |
syntax = "proto3"; | |
option java_package = "com.jio.fabric.demo"; | |
option java_multiple_files = true; | |
message ProtoMap { | |
map<string, uint32> properties = 1; | |
} | |
message StringList { | |
repeated string property = 1; | |
} | |
**/ | |
package com.jio.protos; | |
import com.jio.fabric.demo.ProtoMap; | |
import com.jio.fabric.demo.StringList; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.util.*; | |
import java.util.zip.DataFormatException; | |
import java.util.zip.Deflater; | |
import java.util.zip.Inflater; | |
public class ProtoTest { | |
private String generateRandomString() { | |
int leftLimit = 97; // letter 'a' | |
int rightLimit = 122; // letter 'z' | |
int targetStringLength = 32; | |
Random random = new Random(); | |
return random.ints(leftLimit, rightLimit + 1) | |
.limit(targetStringLength) | |
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) | |
.toString(); | |
} | |
public static byte[] compress(byte[] data) throws IOException { | |
Deflater deflater = new Deflater(); | |
deflater.setInput(data); | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); | |
deflater.finish(); | |
byte[] buffer = new byte[1024]; | |
while (!deflater.finished()) { | |
int count = deflater.deflate(buffer); // returns the generated code... index | |
outputStream.write(buffer, 0, count); | |
} | |
outputStream.close(); | |
byte[] output = outputStream.toByteArray(); | |
return output; | |
} | |
public static byte[] decompress(byte[] data) throws IOException, DataFormatException { | |
Inflater inflater = new Inflater(); | |
inflater.setInput(data); | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); | |
byte[] buffer = new byte[1024]; | |
while (!inflater.finished()) { | |
int count = inflater.inflate(buffer); | |
outputStream.write(buffer, 0, count); | |
} | |
outputStream.close(); | |
byte[] output = outputStream.toByteArray(); | |
return output; | |
} | |
@Test | |
public void testMetrics() throws IOException, DataFormatException { | |
for (int i = 0; i < 100; i++) { | |
test(); | |
System.out.println("\n\n"); | |
} | |
} | |
public void test() throws IOException, DataFormatException { | |
Map<String, Integer> values = new HashMap<>(); | |
List<Integer> valueList = new ArrayList<>(); | |
Random rand = new Random(); | |
for (int i = 0; i < 10; i++) { | |
String s = generateRandomString(); | |
int integer = rand.nextInt(Integer.MAX_VALUE); | |
values.put(s, integer); | |
valueList.add(integer); | |
} | |
ProtoMap protoMap = ProtoMap.newBuilder().putAllProperties(values).build(); | |
byte [] mapBytes = protoMap.toByteArray(); | |
long start = System.nanoTime(); | |
byte [] compressMapBytes = compress(mapBytes); | |
long end = System.nanoTime(); | |
long processingTime = end - start; | |
System.out.println("Total bytes in map : " + mapBytes.length); | |
System.out.println("Total bytes in compressed map : " + compressMapBytes.length); | |
System.out.println("Nano second time taken in compression : " + processingTime); | |
start = System.nanoTime(); | |
byte [] decompressMapBytes = decompress(compressMapBytes); | |
end = System.nanoTime(); | |
processingTime = end - start; | |
System.out.println("Nano second time taken in decompression : " + processingTime); | |
Assertions.assertArrayEquals(mapBytes, decompressMapBytes); | |
System.out.println(); | |
StringList stringList = StringList.newBuilder().addAllProperty(valueList).build(); | |
byte [] listBytes = stringList.toByteArray(); | |
start = System.nanoTime(); | |
byte [] compressListBytes = compress(listBytes); | |
end = System.nanoTime(); | |
processingTime = end - start; | |
System.out.println("Total bytes in list : " + listBytes.length); | |
System.out.println("Total bytes in compressed list : " + compressListBytes.length); | |
System.out.println("Nano second time taken in compression : " + processingTime); | |
start = System.nanoTime(); | |
byte [] decompressListBytes = decompress(compressListBytes); | |
end = System.nanoTime(); | |
processingTime = end - start; | |
System.out.println("Nano second time taken in decompression : " + processingTime); | |
Assertions.assertArrayEquals(listBytes, decompressListBytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment