Skip to content

Commit

Permalink
Updates code to use new send method.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Jan 21, 2025
1 parent 8c47f6c commit 437a76e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<String, String> map) {
JsonStream stream = JsonStreamPool.borrowJsonStream();
public static void serialize(Map<String, String> map, JsonStream stream) {
try {
stream.reset(null);
stream.writeObjectStart();
Expand All @@ -53,22 +65,18 @@ public static byte[] serialize(Map<String, String> 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();
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -147,4 +155,4 @@ public String getMessage() {
return message;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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<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) {
Expand All @@ -66,4 +81,4 @@ private int parseQueryCount(Parameters parameters) {
}
return Math.min(500, Math.max(1, parsedValue));
}
}
}

0 comments on commit 437a76e

Please sign in to comment.