Build/MSA
Protocol Buffer
scsi to.
2024. 3. 24. 20:14
1. 프로토콜 버퍼란?
구글에서 개발한 데이터 직렬화 구조. 데이터를 주고 받을 때 JSON보다 더 효율적으로 정의하고 압축한다.
언어와 플랫폼에 중림적이다. 포맷을 정하기만 하면 컴파일시 C++, Java, Python등에서 이용 가능.
2. Type
Java Type | Proto Type |
int | int32/sint32 |
long | int64/sint64 |
float | float |
double | double |
boolean | bool |
String | string |
byte[] | bytes |
*sint means signed int (음수, 양수 모두 허용)
3. JSON 과 속도차이
- JSON을 ObjectMapper로 직렬화/역직렬화 하는것과 굉장한 차이가 난다. (거의 8배)
*자바는 cold start problem 때문에 첫번째 테스트 결과는 신뢰하기 힘드니 여러번 하는 것이 좋다.
20:06:28.775 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for ProtoBuf: 355 ms
20:06:30.566 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for Json: 1784 ms
20:06:30.567 [main] INFO com.example.sec03.Lec03PerformanceTest -- =====================================
20:06:30.709 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for ProtoBuf: 142 ms
20:06:31.816 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for Json: 1106 ms
20:06:31.817 [main] INFO com.example.sec03.Lec03PerformanceTest -- =====================================
20:06:31.962 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for ProtoBuf: 145 ms
20:06:33.052 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for Json: 1090 ms
20:06:33.052 [main] INFO com.example.sec03.Lec03PerformanceTest -- =====================================
20:06:33.195 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for ProtoBuf: 143 ms
20:06:34.327 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for Json: 1132 ms
20:06:34.327 [main] INFO com.example.sec03.Lec03PerformanceTest -- =====================================
20:06:34.463 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for ProtoBuf: 136 ms
20:06:35.515 [main] INFO com.example.sec03.Lec03PerformanceTest -- time taken for Json: 1052 ms
20:06:35.515 [main] INFO com.example.sec03.Lec03PerformanceTest -- =====================================
- 직렬화된 바이트의 길이도 차이가 크다.
20:13:48.300 [main] INFO com.example.sec03.Lec03PerformanceTest -- json bytes length: 128
20:13:48.355 [main] INFO com.example.sec03.Lec03PerformanceTest -- proto bytes length: 40
출처:
https://www.markany.com/blog/weekly-markany-5-protobuf/
Udemy: