Browse Source

Updates code to use new send method.

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
Santiago Pericas-Geertsen 7 months ago
parent
commit
437a76eb1a

+ 23 - 18
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/JsonSerializer.java

@@ -2,8 +2,8 @@ package io.helidon.benchmark.nima;
 
 
 import java.io.IOException;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Arrays;
-import java.util.Map;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 
 
 import com.jsoniter.output.JsonStream;
 import com.jsoniter.output.JsonStream;
 import com.jsoniter.output.JsonStreamPool;
 import com.jsoniter.output.JsonStreamPool;
@@ -15,7 +15,7 @@ public class 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
      * @param obj the instance
      * @return the byte array
      * @return the byte array
@@ -28,19 +28,31 @@ public class JsonSerializer {
             return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
             return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
         } catch (IOException e) {
         } catch (IOException e) {
             throw new JsonException(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
      * @param map the map
-     * @return the byte array
+     * @param stream the JSON stream
      */
      */
-    public static byte[] serialize(Map<String, String> map) {
-        JsonStream stream = JsonStreamPool.borrowJsonStream();
+    public static void serialize(Map<String, String> map, JsonStream stream) {
         try {
         try {
             stream.reset(null);
             stream.reset(null);
             stream.writeObjectStart();
             stream.writeObjectStart();
@@ -53,22 +65,18 @@ public class JsonSerializer {
                 }
                 }
             });
             });
             stream.writeObjectEnd();
             stream.writeObjectEnd();
-            return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
         } catch (IOException e) {
         } catch (IOException e) {
             throw new JsonException(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
      * @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 {
         try {
             stream.reset(null);
             stream.reset(null);
             stream.writeArrayStart();
             stream.writeArrayStart();
@@ -82,11 +90,8 @@ public class JsonSerializer {
 
 
             }
             }
             stream.writeArrayEnd();
             stream.writeArrayEnd();
-            return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
         } catch (IOException e) {
         } catch (IOException e) {
             throw new JsonException(e);
             throw new JsonException(e);
-        } finally {
-            JsonStreamPool.returnJsonStream(stream);
         }
         }
     }
     }
 }
 }

+ 18 - 10
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");
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * you may not use this file except in compliance with the License.
@@ -19,16 +19,18 @@ package io.helidon.benchmark.nima;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.StandardCharsets;
 import java.util.logging.Logger;
 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.DbRepository;
 import io.helidon.benchmark.nima.models.HikariJdbcRepository;
 import io.helidon.benchmark.nima.models.HikariJdbcRepository;
 import io.helidon.benchmark.nima.models.PgClientRepository;
 import io.helidon.benchmark.nima.models.PgClientRepository;
 import io.helidon.benchmark.nima.services.DbService;
 import io.helidon.benchmark.nima.services.DbService;
 import io.helidon.benchmark.nima.services.FortuneHandler;
 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.Header;
 import io.helidon.http.HeaderNames;
 import io.helidon.http.HeaderNames;
 import io.helidon.http.HeaderValues;
 import io.helidon.http.HeaderValues;
-import io.helidon.config.Config;
-import io.helidon.config.ConfigException;
 import io.helidon.logging.common.LogConfig;
 import io.helidon.logging.common.LogConfig;
 import io.helidon.webserver.WebServer;
 import io.helidon.webserver.WebServer;
 import io.helidon.webserver.http.Handler;
 import io.helidon.webserver.http.Handler;
@@ -93,7 +95,7 @@ public final class Main {
 
 
     static class PlaintextHandler implements Handler {
     static class PlaintextHandler implements Handler {
         static final Header CONTENT_TYPE = HeaderValues.createCached(HeaderNames.CONTENT_TYPE,
         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");
         static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH, "13");
         private static final byte[] RESPONSE_BYTES = "Hello, World!".getBytes(StandardCharsets.UTF_8);
         private static final byte[] RESPONSE_BYTES = "Hello, World!".getBytes(StandardCharsets.UTF_8);
 
 
@@ -110,14 +112,20 @@ public final class Main {
         private static final String MESSAGE = "Hello, World!";
         private static final String MESSAGE = "Hello, World!";
         private static final int JSON_LENGTH = serialize(new Message(MESSAGE)).length;
         private static final int JSON_LENGTH = serialize(new Message(MESSAGE)).length;
         static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH,
         static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH,
-                                                                       String.valueOf(JSON_LENGTH));
+                String.valueOf(JSON_LENGTH));
 
 
         @Override
         @Override
         public void handle(ServerRequest req, ServerResponse res) {
         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 final class Main {
             return message;
             return message;
         }
         }
     }
     }
-}
+}

+ 34 - 19
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/DbService.java

@@ -1,21 +1,19 @@
-
 package io.helidon.benchmark.nima.services;
 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.DbRepository;
-import io.helidon.benchmark.nima.models.World;
+import io.helidon.common.mapper.OptionalValue;
 import io.helidon.common.parameters.Parameters;
 import io.helidon.common.parameters.Parameters;
 import io.helidon.http.HeaderValues;
 import io.helidon.http.HeaderValues;
 import io.helidon.webserver.http.HttpRules;
 import io.helidon.webserver.http.HttpRules;
 import io.helidon.webserver.http.HttpService;
 import io.helidon.webserver.http.HttpService;
 import io.helidon.webserver.http.ServerRequest;
 import io.helidon.webserver.http.ServerRequest;
 import io.helidon.webserver.http.ServerResponse;
 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.Main.SERVER;
 import static io.helidon.benchmark.nima.models.DbRepository.randomWorldNumber;
 import static io.helidon.benchmark.nima.models.DbRepository.randomWorldNumber;
-import static io.helidon.benchmark.nima.JsonSerializer.serialize;
 
 
 public class DbService implements HttpService {
 public class DbService implements HttpService {
 
 
@@ -33,24 +31,41 @@ public class DbService implements HttpService {
     }
     }
 
 
     private void db(ServerRequest req, ServerResponse res) {
     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) {
     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) {
     private void updates(ServerRequest req, ServerResponse res) {
-        res.header(SERVER);
-        res.header(HeaderValues.CONTENT_TYPE_JSON);
-        int count = parseQueryCount(req.query());
-        List<World> 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) {
     private int parseQueryCount(Parameters parameters) {
@@ -66,4 +81,4 @@ public class DbService implements HttpService {
         }
         }
         return Math.min(500, Math.max(1, parsedValue));
         return Math.min(500, Math.max(1, parsedValue));
     }
     }
-}
+}