diff --git a/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/JsonSerializer.java b/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/JsonSerializer.java index 322a7cf030c..0564fa8a911 100644 --- a/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/JsonSerializer.java +++ b/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/JsonSerializer.java @@ -2,8 +2,8 @@ import java.io.IOException; import java.util.Arrays; -import java.util.Map; import java.util.List; +import java.util.Map; import com.jsoniter.output.JsonStream; import com.jsoniter.output.JsonStreamPool; @@ -15,7 +15,7 @@ private JsonSerializer() { } /** - * Serialize an instance into a JSON object and return it as a byte array. + * Serialize an instance into a byte array. * * @param obj the instance * @return the byte array @@ -28,19 +28,31 @@ public static byte[] serialize(Object obj) { return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail()); } catch (IOException e) { throw new JsonException(e); - } finally { - JsonStreamPool.returnJsonStream(stream); } } /** - * Serialize a map of strings into a JSON object and return it as a byte array. + * Serialize an instance into a JSON stream. + * + * @param obj the instance + * @param stream the JSON stream + */ + public static void serialize(Object obj, JsonStream stream) { + try { + stream.reset(null); + stream.writeVal(obj.getClass(), obj); + } catch (IOException e) { + throw new JsonException(e); + } + } + + /** + * Serialize a map of strings into a JSON stream. * * @param map the map - * @return the byte array + * @param stream the JSON stream */ - public static byte[] serialize(Map map) { - JsonStream stream = JsonStreamPool.borrowJsonStream(); + public static void serialize(Map map, JsonStream stream) { try { stream.reset(null); stream.writeObjectStart(); @@ -53,22 +65,18 @@ public static byte[] serialize(Map map) { } }); stream.writeObjectEnd(); - return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail()); } catch (IOException e) { throw new JsonException(e); - } finally { - JsonStreamPool.returnJsonStream(stream); } } /** - * Serialize a list of objects into a JSON array and return it as a byte array. + * Serialize a list of objects into a JSON stream. * * @param objs the list of objects - * @return the byte array + * @param stream the JSON stream */ - public static byte[] serialize(List objs) { - JsonStream stream = JsonStreamPool.borrowJsonStream(); + public static void serialize(List objs, JsonStream stream) { try { stream.reset(null); stream.writeArrayStart(); @@ -82,11 +90,8 @@ public static byte[] serialize(List objs) { } stream.writeArrayEnd(); - return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail()); } catch (IOException e) { throw new JsonException(e); - } finally { - JsonStreamPool.returnJsonStream(stream); } } } diff --git a/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/Main.java b/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/Main.java index df669d8a7a7..800ce927537 100644 --- a/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/Main.java +++ b/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/Main.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2025 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,16 +19,18 @@ import java.nio.charset.StandardCharsets; import java.util.logging.Logger; +import com.jsoniter.output.JsonStream; +import com.jsoniter.output.JsonStreamPool; import io.helidon.benchmark.nima.models.DbRepository; import io.helidon.benchmark.nima.models.HikariJdbcRepository; import io.helidon.benchmark.nima.models.PgClientRepository; import io.helidon.benchmark.nima.services.DbService; import io.helidon.benchmark.nima.services.FortuneHandler; +import io.helidon.config.Config; +import io.helidon.config.ConfigException; import io.helidon.http.Header; import io.helidon.http.HeaderNames; import io.helidon.http.HeaderValues; -import io.helidon.config.Config; -import io.helidon.config.ConfigException; import io.helidon.logging.common.LogConfig; import io.helidon.webserver.WebServer; import io.helidon.webserver.http.Handler; @@ -93,7 +95,7 @@ static void routing(HttpRules rules) { static class PlaintextHandler implements Handler { static final Header CONTENT_TYPE = HeaderValues.createCached(HeaderNames.CONTENT_TYPE, - "text/plain; charset=UTF-8"); + "text/plain; charset=UTF-8"); static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH, "13"); private static final byte[] RESPONSE_BYTES = "Hello, World!".getBytes(StandardCharsets.UTF_8); @@ -110,14 +112,20 @@ static class JsonHandler implements Handler { private static final String MESSAGE = "Hello, World!"; private static final int JSON_LENGTH = serialize(new Message(MESSAGE)).length; static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH, - String.valueOf(JSON_LENGTH)); + String.valueOf(JSON_LENGTH)); @Override public void handle(ServerRequest req, ServerResponse res) { - res.header(CONTENT_LENGTH); - res.header(HeaderValues.CONTENT_TYPE_JSON); - res.header(Main.SERVER); - res.send(serialize(new Message(MESSAGE))); + JsonStream stream = JsonStreamPool.borrowJsonStream(); + try { + res.header(CONTENT_LENGTH); + res.header(HeaderValues.CONTENT_TYPE_JSON); + res.header(Main.SERVER); + serialize(new Message(MESSAGE), stream); + res.send(stream.buffer().data(), 0, stream.buffer().tail()); + } finally { + JsonStreamPool.returnJsonStream(stream); + } } } @@ -147,4 +155,4 @@ public String getMessage() { return message; } } -} +} \ No newline at end of file diff --git a/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/DbService.java b/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/DbService.java index e3bd1fe39fc..a1e97de44b5 100644 --- a/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/DbService.java +++ b/frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/DbService.java @@ -1,21 +1,19 @@ - package io.helidon.benchmark.nima.services; -import java.util.List; - +import com.jsoniter.output.JsonStream; +import com.jsoniter.output.JsonStreamPool; import io.helidon.benchmark.nima.models.DbRepository; -import io.helidon.benchmark.nima.models.World; +import io.helidon.common.mapper.OptionalValue; import io.helidon.common.parameters.Parameters; import io.helidon.http.HeaderValues; import io.helidon.webserver.http.HttpRules; import io.helidon.webserver.http.HttpService; import io.helidon.webserver.http.ServerRequest; import io.helidon.webserver.http.ServerResponse; -import io.helidon.common.mapper.OptionalValue; +import static io.helidon.benchmark.nima.JsonSerializer.serialize; import static io.helidon.benchmark.nima.Main.SERVER; import static io.helidon.benchmark.nima.models.DbRepository.randomWorldNumber; -import static io.helidon.benchmark.nima.JsonSerializer.serialize; public class DbService implements HttpService { @@ -33,24 +31,41 @@ public void routing(HttpRules httpRules) { } private void db(ServerRequest req, ServerResponse res) { - res.header(SERVER); - res.header(HeaderValues.CONTENT_TYPE_JSON); - res.send(serialize(repository.getWorld(randomWorldNumber()))); + JsonStream stream = JsonStreamPool.borrowJsonStream(); + try { + res.header(SERVER); + res.header(HeaderValues.CONTENT_TYPE_JSON); + serialize(repository.getWorld(randomWorldNumber()), stream); + res.send(stream.buffer().data(), 0, stream.buffer().tail()); + } finally { + JsonStreamPool.returnJsonStream(stream); + } } private void queries(ServerRequest req, ServerResponse res) { - res.header(SERVER); - res.header(HeaderValues.CONTENT_TYPE_JSON); - int count = parseQueryCount(req.query()); - res.send(serialize(repository.getWorlds(count))); + JsonStream stream = JsonStreamPool.borrowJsonStream(); + try { + res.header(SERVER); + res.header(HeaderValues.CONTENT_TYPE_JSON); + int count = parseQueryCount(req.query()); + serialize(repository.getWorlds(count), stream); + res.send(stream.buffer().data(), 0, stream.buffer().tail()); + } finally { + JsonStreamPool.returnJsonStream(stream); + } } private void updates(ServerRequest req, ServerResponse res) { - res.header(SERVER); - res.header(HeaderValues.CONTENT_TYPE_JSON); - int count = parseQueryCount(req.query()); - List worlds = repository.updateWorlds(count); - res.send(serialize(worlds)); + JsonStream stream = JsonStreamPool.borrowJsonStream(); + try { + res.header(SERVER); + res.header(HeaderValues.CONTENT_TYPE_JSON); + int count = parseQueryCount(req.query()); + serialize(repository.updateWorlds(count), stream); + res.send(stream.buffer().data(), 0, stream.buffer().tail()); + } finally { + JsonStreamPool.returnJsonStream(stream); + } } private int parseQueryCount(Parameters parameters) { @@ -66,4 +81,4 @@ private int parseQueryCount(Parameters parameters) { } return Math.min(500, Math.max(1, parsedValue)); } -} \ No newline at end of file +}