Windows:
> netstat -ano | findstr :8080
> taskkill PID <pid> /F
Linux:
> sudo lsof -i 8080
> sudo kill -9 <pid>
Windows:
> netstat -ano | findstr :8080
> taskkill PID <pid> /F
Linux:
> sudo lsof -i 8080
> sudo kill -9 <pid>
public class PerformanceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(PerformanceTest.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) throws Exception {
var protoPerson = Person.newBuilder()
.setLastName("T")
.setAge(32)
.setEmail("t@email.com")
.setEmployed(true)
.setSalary(123004.5)
.setBankAccountNumber(38495959093040944L)
.setBalance(-100)
.build();
LOGGER.info("how many bytes protoPerson has? {}", protoPerson.toByteArray().length); // 41
var jsonPerson = new JsonPerson("T", 32, "t@email.com", true, 123004.5, 38495959093040944L, -100);
var bytes = MAPPER.writeValueAsBytes(jsonPerson);
LOGGER.info("how many bytes jsonPerson has? {}", bytes.length); // 130 = 3x more!
for (int i=0; i<5; i++) { // first run to be ignored (warmup JVM)
runTest("json", () -> json(jsonPerson));
runTest("proto", () -> proto(protoPerson)); // 7x faster!
}
}
private static void runTest(String testName, Runnable runnable) {
var start = System.currentTimeMillis();
for (int i=0; i<5_000_000; i++) {
runnable.run();
}
var end = System.currentTimeMillis();
LOGGER.info("time taken for {} = {} ms", testName, (end - start));
}
private static void proto(Person person) {
try {
var bytes = person.toByteArray();
Person.parseFrom(bytes);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
}
private static void json(JsonPerson person) {
try {
var bytes = MAPPER.writeValueAsBytes(person);
MAPPER.readValue(bytes, JsonPerson.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}