Skip to content

Commit 437a76e

Browse files
committed
Updates code to use new send method.
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
1 parent 8c47f6c commit 437a76e

File tree

3 files changed

+75
-47
lines changed

3 files changed

+75
-47
lines changed

frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/JsonSerializer.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.IOException;
44
import java.util.Arrays;
5-
import java.util.Map;
65
import java.util.List;
6+
import java.util.Map;
77

88
import com.jsoniter.output.JsonStream;
99
import com.jsoniter.output.JsonStreamPool;
@@ -15,7 +15,7 @@ private JsonSerializer() {
1515
}
1616

1717
/**
18-
* Serialize an instance into a JSON object and return it as a byte array.
18+
* Serialize an instance into a byte array.
1919
*
2020
* @param obj the instance
2121
* @return the byte array
@@ -28,19 +28,31 @@ public static byte[] serialize(Object obj) {
2828
return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
2929
} catch (IOException e) {
3030
throw new JsonException(e);
31-
} finally {
32-
JsonStreamPool.returnJsonStream(stream);
3331
}
3432
}
3533

3634
/**
37-
* Serialize a map of strings into a JSON object and return it as a byte array.
35+
* Serialize an instance into a JSON stream.
36+
*
37+
* @param obj the instance
38+
* @param stream the JSON stream
39+
*/
40+
public static void serialize(Object obj, JsonStream stream) {
41+
try {
42+
stream.reset(null);
43+
stream.writeVal(obj.getClass(), obj);
44+
} catch (IOException e) {
45+
throw new JsonException(e);
46+
}
47+
}
48+
49+
/**
50+
* Serialize a map of strings into a JSON stream.
3851
*
3952
* @param map the map
40-
* @return the byte array
53+
* @param stream the JSON stream
4154
*/
42-
public static byte[] serialize(Map<String, String> map) {
43-
JsonStream stream = JsonStreamPool.borrowJsonStream();
55+
public static void serialize(Map<String, String> map, JsonStream stream) {
4456
try {
4557
stream.reset(null);
4658
stream.writeObjectStart();
@@ -53,22 +65,18 @@ public static byte[] serialize(Map<String, String> map) {
5365
}
5466
});
5567
stream.writeObjectEnd();
56-
return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
5768
} catch (IOException e) {
5869
throw new JsonException(e);
59-
} finally {
60-
JsonStreamPool.returnJsonStream(stream);
6170
}
6271
}
6372

6473
/**
65-
* Serialize a list of objects into a JSON array and return it as a byte array.
74+
* Serialize a list of objects into a JSON stream.
6675
*
6776
* @param objs the list of objects
68-
* @return the byte array
77+
* @param stream the JSON stream
6978
*/
70-
public static byte[] serialize(List<?> objs) {
71-
JsonStream stream = JsonStreamPool.borrowJsonStream();
79+
public static void serialize(List<?> objs, JsonStream stream) {
7280
try {
7381
stream.reset(null);
7482
stream.writeArrayStart();
@@ -82,11 +90,8 @@ public static byte[] serialize(List<?> objs) {
8290

8391
}
8492
stream.writeArrayEnd();
85-
return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
8693
} catch (IOException e) {
8794
throw new JsonException(e);
88-
} finally {
89-
JsonStreamPool.returnJsonStream(stream);
9095
}
9196
}
9297
}

frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/Main.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,16 +19,18 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.util.logging.Logger;
2121

22+
import com.jsoniter.output.JsonStream;
23+
import com.jsoniter.output.JsonStreamPool;
2224
import io.helidon.benchmark.nima.models.DbRepository;
2325
import io.helidon.benchmark.nima.models.HikariJdbcRepository;
2426
import io.helidon.benchmark.nima.models.PgClientRepository;
2527
import io.helidon.benchmark.nima.services.DbService;
2628
import io.helidon.benchmark.nima.services.FortuneHandler;
29+
import io.helidon.config.Config;
30+
import io.helidon.config.ConfigException;
2731
import io.helidon.http.Header;
2832
import io.helidon.http.HeaderNames;
2933
import io.helidon.http.HeaderValues;
30-
import io.helidon.config.Config;
31-
import io.helidon.config.ConfigException;
3234
import io.helidon.logging.common.LogConfig;
3335
import io.helidon.webserver.WebServer;
3436
import io.helidon.webserver.http.Handler;
@@ -93,7 +95,7 @@ static void routing(HttpRules rules) {
9395

9496
static class PlaintextHandler implements Handler {
9597
static final Header CONTENT_TYPE = HeaderValues.createCached(HeaderNames.CONTENT_TYPE,
96-
"text/plain; charset=UTF-8");
98+
"text/plain; charset=UTF-8");
9799
static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH, "13");
98100
private static final byte[] RESPONSE_BYTES = "Hello, World!".getBytes(StandardCharsets.UTF_8);
99101

@@ -110,14 +112,20 @@ static class JsonHandler implements Handler {
110112
private static final String MESSAGE = "Hello, World!";
111113
private static final int JSON_LENGTH = serialize(new Message(MESSAGE)).length;
112114
static final Header CONTENT_LENGTH = HeaderValues.createCached(HeaderNames.CONTENT_LENGTH,
113-
String.valueOf(JSON_LENGTH));
115+
String.valueOf(JSON_LENGTH));
114116

115117
@Override
116118
public void handle(ServerRequest req, ServerResponse res) {
117-
res.header(CONTENT_LENGTH);
118-
res.header(HeaderValues.CONTENT_TYPE_JSON);
119-
res.header(Main.SERVER);
120-
res.send(serialize(new Message(MESSAGE)));
119+
JsonStream stream = JsonStreamPool.borrowJsonStream();
120+
try {
121+
res.header(CONTENT_LENGTH);
122+
res.header(HeaderValues.CONTENT_TYPE_JSON);
123+
res.header(Main.SERVER);
124+
serialize(new Message(MESSAGE), stream);
125+
res.send(stream.buffer().data(), 0, stream.buffer().tail());
126+
} finally {
127+
JsonStreamPool.returnJsonStream(stream);
128+
}
121129
}
122130
}
123131

@@ -147,4 +155,4 @@ public String getMessage() {
147155
return message;
148156
}
149157
}
150-
}
158+
}
Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
21
package io.helidon.benchmark.nima.services;
32

4-
import java.util.List;
5-
3+
import com.jsoniter.output.JsonStream;
4+
import com.jsoniter.output.JsonStreamPool;
65
import io.helidon.benchmark.nima.models.DbRepository;
7-
import io.helidon.benchmark.nima.models.World;
6+
import io.helidon.common.mapper.OptionalValue;
87
import io.helidon.common.parameters.Parameters;
98
import io.helidon.http.HeaderValues;
109
import io.helidon.webserver.http.HttpRules;
1110
import io.helidon.webserver.http.HttpService;
1211
import io.helidon.webserver.http.ServerRequest;
1312
import io.helidon.webserver.http.ServerResponse;
14-
import io.helidon.common.mapper.OptionalValue;
1513

14+
import static io.helidon.benchmark.nima.JsonSerializer.serialize;
1615
import static io.helidon.benchmark.nima.Main.SERVER;
1716
import static io.helidon.benchmark.nima.models.DbRepository.randomWorldNumber;
18-
import static io.helidon.benchmark.nima.JsonSerializer.serialize;
1917

2018
public class DbService implements HttpService {
2119

@@ -33,24 +31,41 @@ public void routing(HttpRules httpRules) {
3331
}
3432

3533
private void db(ServerRequest req, ServerResponse res) {
36-
res.header(SERVER);
37-
res.header(HeaderValues.CONTENT_TYPE_JSON);
38-
res.send(serialize(repository.getWorld(randomWorldNumber())));
34+
JsonStream stream = JsonStreamPool.borrowJsonStream();
35+
try {
36+
res.header(SERVER);
37+
res.header(HeaderValues.CONTENT_TYPE_JSON);
38+
serialize(repository.getWorld(randomWorldNumber()), stream);
39+
res.send(stream.buffer().data(), 0, stream.buffer().tail());
40+
} finally {
41+
JsonStreamPool.returnJsonStream(stream);
42+
}
3943
}
4044

4145
private void queries(ServerRequest req, ServerResponse res) {
42-
res.header(SERVER);
43-
res.header(HeaderValues.CONTENT_TYPE_JSON);
44-
int count = parseQueryCount(req.query());
45-
res.send(serialize(repository.getWorlds(count)));
46+
JsonStream stream = JsonStreamPool.borrowJsonStream();
47+
try {
48+
res.header(SERVER);
49+
res.header(HeaderValues.CONTENT_TYPE_JSON);
50+
int count = parseQueryCount(req.query());
51+
serialize(repository.getWorlds(count), stream);
52+
res.send(stream.buffer().data(), 0, stream.buffer().tail());
53+
} finally {
54+
JsonStreamPool.returnJsonStream(stream);
55+
}
4656
}
4757

4858
private void updates(ServerRequest req, ServerResponse res) {
49-
res.header(SERVER);
50-
res.header(HeaderValues.CONTENT_TYPE_JSON);
51-
int count = parseQueryCount(req.query());
52-
List<World> worlds = repository.updateWorlds(count);
53-
res.send(serialize(worlds));
59+
JsonStream stream = JsonStreamPool.borrowJsonStream();
60+
try {
61+
res.header(SERVER);
62+
res.header(HeaderValues.CONTENT_TYPE_JSON);
63+
int count = parseQueryCount(req.query());
64+
serialize(repository.updateWorlds(count), stream);
65+
res.send(stream.buffer().data(), 0, stream.buffer().tail());
66+
} finally {
67+
JsonStreamPool.returnJsonStream(stream);
68+
}
5469
}
5570

5671
private int parseQueryCount(Parameters parameters) {
@@ -66,4 +81,4 @@ private int parseQueryCount(Parameters parameters) {
6681
}
6782
return Math.min(500, Math.max(1, parsedValue));
6883
}
69-
}
84+
}

0 commit comments

Comments
 (0)