From 99588db075f5f370bd4eb3fbe5b1c392870a897f Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Mon, 19 Sep 2022 06:58:38 +0200 Subject: [PATCH 1/5] JNH connector perf tests Signed-off-by: Maxim Nesen --- pom.xml | 2 +- tests/performance/benchmarks/pom.xml | 4 + .../benchmark/JNHConnectorBenchmark.java | 127 ++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java diff --git a/pom.xml b/pom.xml index d1acba3fc8..8dea2c6d55 100644 --- a/pom.xml +++ b/pom.xml @@ -2195,7 +2195,7 @@ 1.19.3 ${jersey1.version} 1.3.7 - 1.10.2 + 1.35 1.44 5.6.0 4.0.2 diff --git a/tests/performance/benchmarks/pom.xml b/tests/performance/benchmarks/pom.xml index 07cbc44237..2b10d245c8 100644 --- a/tests/performance/benchmarks/pom.xml +++ b/tests/performance/benchmarks/pom.xml @@ -58,6 +58,10 @@ org.glassfish.jersey.connectors jersey-grizzly-connector + + org.glassfish.jersey.connectors + jersey-jnh-connector + jakarta.annotation jakarta.annotation-api diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java new file mode 100644 index 0000000000..677ffdc927 --- /dev/null +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.performance.benchmark; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.grizzly.nio.transport.TCPNIOTransport; +import org.glassfish.grizzly.threadpool.ThreadPoolConfig; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.jackson.JacksonFeature; +import org.glassfish.jersey.jnh.connector.JavaNetHttpConnectorProvider; +import org.glassfish.jersey.tests.performance.benchmark.entity.json.JacksonApplication; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.net.URI; +import java.util.concurrent.TimeUnit; + +/** + * jersey-jnh-connector {@link org.glassfish.jersey.jnh.connector.JavaNetHttpConnector} benchmark. + * + */ +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.SECONDS) +@Warmup(iterations = 1, time = 1) +@Measurement(iterations = 160, time = 1) +@Fork(8) +@State(Scope.Benchmark) +public class JNHConnectorBenchmark { + + private static final URI BASE_URI = URI.create("http://localhost:8080/"); + + private static final String REQUEST_TARGET = "http://localhost:8080/projects/detailed"; + private volatile HttpServer server; + + private volatile Client client; + private volatile Client defaultClient; + + @Setup + public void start() throws Exception { + server = + GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new JacksonApplication(Boolean.FALSE), false); + final TCPNIOTransport transport = server.getListener("grizzly").getTransport(); + transport.setSelectorRunnersCount(4); + transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(8).setMaxPoolSize(8)); + + server.start(); + client = ClientBuilder.newClient( + new ClientConfig() + .connectorProvider(new JavaNetHttpConnectorProvider()) + .register(JacksonFeature.class) + ); + defaultClient = ClientBuilder.newClient( + new ClientConfig() + .register(JacksonFeature.class) + ); + + } + + @Setup(Level.Iteration) + public void request() { + } + + @TearDown + public void shutdown() { + server.shutdownNow(); + } + + @Benchmark + public void measureEmptyGetResource() { + client.target(REQUEST_TARGET).request(MediaType.APPLICATION_XML_TYPE).get(); + } + + @Benchmark + public void measureEntityGetResource() { + client.target(REQUEST_TARGET).request(MediaType.APPLICATION_JSON_TYPE).get(); + } + @Benchmark + public void measureEntityGetDefaultResource() { + defaultClient.target(REQUEST_TARGET).request(MediaType.APPLICATION_JSON_TYPE).get(); + } + + @Benchmark + public void measureEmptyGetDefaultResource() { + defaultClient.target(REQUEST_TARGET).request(MediaType.APPLICATION_XML_TYPE).get(); + } + + public static void main(final String[] args) throws Exception { + final Options opt = new OptionsBuilder() + // Register our benchmarks. + .include(JNHConnectorBenchmark.class.getSimpleName()) + .build(); + + new Runner(opt).run(); + } +} \ No newline at end of file From 87521a259a3d6fb8a532764b55fcd7c39979a1d9 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 21 Sep 2022 08:51:07 +0200 Subject: [PATCH 2/5] JNH connector perf tests Signed-off-by: Maxim Nesen --- tests/performance/benchmarks/pom.xml | 8 + .../benchmark/JNHConnectorBenchmark.java | 127 ----------- .../JNHConnectorHttpMethodsBenchmark.java | 208 ++++++++++++++++++ .../benchmark/server/jnh/Book.java | 61 +++++ .../benchmark/server/jnh/JNHApplication.java | 25 +++ .../server/jnh/JNHttpMethodResource.java | 91 ++++++++ 6 files changed, 393 insertions(+), 127 deletions(-) delete mode 100644 tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java create mode 100644 tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java create mode 100644 tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java create mode 100644 tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java create mode 100644 tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java diff --git a/tests/performance/benchmarks/pom.xml b/tests/performance/benchmarks/pom.xml index 2b10d245c8..1c7567964d 100644 --- a/tests/performance/benchmarks/pom.xml +++ b/tests/performance/benchmarks/pom.xml @@ -46,6 +46,14 @@ org.glassfish.jersey.media jersey-media-json-jackson + + org.glassfish.jersey.media + jersey-media-jaxb + + + com.sun.xml.bind + jaxb-osgi + org.glassfish.jersey.ext jersey-entity-filtering diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java deleted file mode 100644 index 677ffdc927..0000000000 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0, which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the - * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, - * version 2 with the GNU Classpath Exception, which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 - */ - -package org.glassfish.jersey.tests.performance.benchmark; - -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.Response; -import org.glassfish.grizzly.http.server.HttpServer; -import org.glassfish.grizzly.nio.transport.TCPNIOTransport; -import org.glassfish.grizzly.threadpool.ThreadPoolConfig; -import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.jackson.JacksonFeature; -import org.glassfish.jersey.jnh.connector.JavaNetHttpConnectorProvider; -import org.glassfish.jersey.tests.performance.benchmark.entity.json.JacksonApplication; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.TearDown; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.net.URI; -import java.util.concurrent.TimeUnit; - -/** - * jersey-jnh-connector {@link org.glassfish.jersey.jnh.connector.JavaNetHttpConnector} benchmark. - * - */ -@BenchmarkMode(Mode.Throughput) -@OutputTimeUnit(TimeUnit.SECONDS) -@Warmup(iterations = 1, time = 1) -@Measurement(iterations = 160, time = 1) -@Fork(8) -@State(Scope.Benchmark) -public class JNHConnectorBenchmark { - - private static final URI BASE_URI = URI.create("http://localhost:8080/"); - - private static final String REQUEST_TARGET = "http://localhost:8080/projects/detailed"; - private volatile HttpServer server; - - private volatile Client client; - private volatile Client defaultClient; - - @Setup - public void start() throws Exception { - server = - GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new JacksonApplication(Boolean.FALSE), false); - final TCPNIOTransport transport = server.getListener("grizzly").getTransport(); - transport.setSelectorRunnersCount(4); - transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(8).setMaxPoolSize(8)); - - server.start(); - client = ClientBuilder.newClient( - new ClientConfig() - .connectorProvider(new JavaNetHttpConnectorProvider()) - .register(JacksonFeature.class) - ); - defaultClient = ClientBuilder.newClient( - new ClientConfig() - .register(JacksonFeature.class) - ); - - } - - @Setup(Level.Iteration) - public void request() { - } - - @TearDown - public void shutdown() { - server.shutdownNow(); - } - - @Benchmark - public void measureEmptyGetResource() { - client.target(REQUEST_TARGET).request(MediaType.APPLICATION_XML_TYPE).get(); - } - - @Benchmark - public void measureEntityGetResource() { - client.target(REQUEST_TARGET).request(MediaType.APPLICATION_JSON_TYPE).get(); - } - @Benchmark - public void measureEntityGetDefaultResource() { - defaultClient.target(REQUEST_TARGET).request(MediaType.APPLICATION_JSON_TYPE).get(); - } - - @Benchmark - public void measureEmptyGetDefaultResource() { - defaultClient.target(REQUEST_TARGET).request(MediaType.APPLICATION_XML_TYPE).get(); - } - - public static void main(final String[] args) throws Exception { - final Options opt = new OptionsBuilder() - // Register our benchmarks. - .include(JNHConnectorBenchmark.class.getSimpleName()) - .build(); - - new Runner(opt).run(); - } -} \ No newline at end of file diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java new file mode 100644 index 0000000000..31fa92bcaf --- /dev/null +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.performance.benchmark; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.core.MediaType; +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.grizzly.nio.transport.TCPNIOTransport; +import org.glassfish.grizzly.threadpool.ThreadPoolConfig; +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.jackson.JacksonFeature; +import org.glassfish.jersey.jnh.connector.JavaNetHttpConnectorProvider; +import org.glassfish.jersey.tests.performance.benchmark.server.jnh.Book; +import org.glassfish.jersey.tests.performance.benchmark.server.jnh.JNHApplication; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.util.concurrent.TimeUnit; + +/** + * jersey-jnh-connector {@link org.glassfish.jersey.jnh.connector.JavaNetHttpConnector} benchmark. + * + */ +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.SECONDS) +@Warmup(iterations = 1, time = 1) +@Measurement(iterations = 16, time = 1) +@Fork(1) +@State(Scope.Benchmark) +public class JNHConnectorHttpMethodsBenchmark { + + private static final URI BASE_URI = URI.create("http://localhost:8080/"); + + private static final String REQUEST_TARGET = "HttpMethod"; + private volatile HttpServer server; + + private volatile Client client; + private volatile Client defaultClient; + + private final Book postBook = new Book("The Bonfire of the Vanities", "Tom Wolfe", 11); + private final Book postDefaultBook = new Book("More Die of Heartbreak", "Saul Bellow", 22); + private final Book putBook = new Book("Silmarillion", "J. R. R. Tolkien", 110); + private final Book putDefaultBook = new Book("The Lord of the Rings", "John Tolkien", 220); + + @Setup + public void start() throws Exception { + server = + GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new JNHApplication(), false); + final TCPNIOTransport transport = server.getListener("grizzly").getTransport(); + transport.setSelectorRunnersCount(4); + transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(8).setMaxPoolSize(8)); + + server.start(); + client = ClientBuilder.newClient( + new ClientConfig() + .connectorProvider(new JavaNetHttpConnectorProvider()) + .register(JacksonFeature.class) + ); + defaultClient = ClientBuilder.newClient( + new ClientConfig() + .register(JacksonFeature.class) + ); + + } + + @Setup(Level.Iteration) + public void request() { + } + + @TearDown + public void shutdown() { + server.shutdownNow(); + } + + @Benchmark + public void measureGetStringResource() { + client.target(BASE_URI).path(REQUEST_TARGET).request(MediaType.TEXT_PLAIN).get(String.class); + } + + @Benchmark + public void measureGetStringDefaultResource() { + defaultClient.target(BASE_URI).path(REQUEST_TARGET).request(MediaType.TEXT_PLAIN).get(String.class); + } + + @Benchmark + public void measureEntityGetResource() { + client.target(BASE_URI).path(REQUEST_TARGET).path("books") + .request(MediaType.APPLICATION_JSON_TYPE).get(); + } + @Benchmark + public void measureEntityGetXmlResource() throws IOException { + final InputStream stream = client.target(BASE_URI).path(REQUEST_TARGET).path("books") + .request(MediaType.APPLICATION_XML_TYPE).get(InputStream.class); + try { + System.out.print(new String(stream.readAllBytes()).charAt(0)); + } finally { + stream.close(); + } + } + + @Benchmark + public void measureEntityGetOneXmlResource() throws IOException { + final Book book = client.target(BASE_URI).path(REQUEST_TARGET).path("book") + .request(MediaType.APPLICATION_XML_TYPE).get(Book.class); + System.out.print(book.getId()); + } + @Benchmark + public void measureEntityGetDefaultResource() { + defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("books") + .request(MediaType.APPLICATION_JSON_TYPE).get(); + } + + @Benchmark + public void measureEntityGetXmlDefaultResource() throws IOException { + final InputStream stream = defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("books") + .request(MediaType.APPLICATION_XML_TYPE).get(InputStream.class); + try { + System.out.print(new String(stream.readAllBytes()).charAt(0)); + } finally { + stream.close(); + } + } + @Benchmark + public void measureEntityGetOneXmlDefaultResource() { + final Book book = defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("book") + .request(MediaType.APPLICATION_XML_TYPE).get(Book.class); + System.out.print(book.getId()); + } + + @Benchmark + public void measurePost() { + client.target(BASE_URI).path(REQUEST_TARGET).path("postBook") + .request().post(Entity.json(postBook)); + } + + @Benchmark + public void measurePostDefault() { + defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("postBook") + .request().post(Entity.json(postDefaultBook)); + } + + @Benchmark + public void measurePut() { + client.target(BASE_URI).path(REQUEST_TARGET).path("putBook") + .request().put(Entity.json(putBook)); + } + + @Benchmark + public void measurePutDefault() { + defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("putBook") + .request().put(Entity.json(putDefaultBook)); + } + + @Benchmark + public void measureDelete() { + client.target(BASE_URI).path(REQUEST_TARGET).path("deleteBook") + .request().delete(); + } + + @Benchmark + public void measureDeleteDefault() { + defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("deleteBook") + .request().delete(); + } + + + public static void main(final String[] args) throws Exception { + final Options opt = new OptionsBuilder() + // Register our benchmarks. + .include(JNHConnectorHttpMethodsBenchmark.class.getSimpleName()) + .build(); + + new Runner(opt).run(); + } +} \ No newline at end of file diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java new file mode 100644 index 0000000000..732c72d8cc --- /dev/null +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.performance.benchmark.server.jnh; + +import jakarta.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Book { + + private String title; + private String author; + private int id; + + + public Book(String title, String author, int id) { + this.title = title; + this.author = author; + this.id = id; + } + + public Book() { + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java new file mode 100644 index 0000000000..bd3bb83606 --- /dev/null +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.performance.benchmark.server.jnh; + +import org.glassfish.jersey.server.ResourceConfig; + +public class JNHApplication extends ResourceConfig { + public JNHApplication() { + register(JNHttpMethodResource.class); + } +} diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java new file mode 100644 index 0000000000..3cdaa66ff3 --- /dev/null +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.performance.benchmark.server.jnh; + +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import org.glassfish.grizzly.http.util.HttpStatus; + +import java.util.ArrayList; +import java.util.List; + +@Path("HttpMethod") +public class JNHttpMethodResource { + + private final static int BOOK_LIST_SIZE=1000; + + private final static List bookList = generateBooks(); + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String getStatus() { + return HttpStatus.OK_200.getReasonPhrase(); + } + + @GET + @Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON}) + @Path("books") + public List getBooks() { + return bookList; + } + + @GET + @Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON}) + @Path("book") + public Book getBook() { + return new Book("1", "2", 3); + } + + @POST + @Path("postBook") + public void postBook(Book book) { + /*System.out.println(String.format("Just got a book by: %s, which is called: %s, giving it an id: %d", + book.getAuthor(), + book.getTitle(), + book.getId()));*/ + } + + @PUT + @Path("putBook") + public void putBook(Book book) { + /*System.out.println(String.format("A book by: %s, with name: %s, is putted back under the id: %d", + book.getAuthor(), + book.getTitle(), + book.getId()));*/ + } + + @DELETE + @Path("deleteBook") + @Produces(MediaType.TEXT_PLAIN) + public String deleteBook() { + return "HttpStatus.OK_200"; + + } + + private static final List generateBooks() { + final List books = new ArrayList<>(); + for (int i = 0; i < BOOK_LIST_SIZE; i++) { + books.add(new Book("Title: " + i, "Author: " + i, i)); + } + return books; + } +} From 59e405c165a1ba121b64d400cbbe2268b03045c2 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 21 Sep 2022 10:34:06 +0200 Subject: [PATCH 3/5] JNH connector perf tests Signed-off-by: Maxim Nesen --- .../benchmark/server/jnh/JNHttpMethodResource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java index 3cdaa66ff3..00d475f53e 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java @@ -31,9 +31,9 @@ @Path("HttpMethod") public class JNHttpMethodResource { - private final static int BOOK_LIST_SIZE=1000; + private static final int BOOK_LIST_SIZE = 1000; - private final static List bookList = generateBooks(); + private static final List bookList = generateBooks(); @GET @Produces(MediaType.TEXT_PLAIN) From eff159e4056ab151c0daca0698483848bb6125ce Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 21 Sep 2022 15:14:14 +0200 Subject: [PATCH 4/5] JNH connector perf tests Signed-off-by: Maxim Nesen --- .../JNHConnectorHttpMethodsBenchmark.java | 27 +++++++++++++++++++ .../benchmark/server/jnh/Book.java | 4 +++ .../server/jnh/JNHttpMethodResource.java | 18 +++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java index 31fa92bcaf..e9e6dda953 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java @@ -20,6 +20,7 @@ import jakarta.ws.rs.client.ClientBuilder; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.StreamingOutput; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.grizzly.nio.transport.TCPNIOTransport; import org.glassfish.grizzly.threadpool.ThreadPoolConfig; @@ -29,6 +30,7 @@ import org.glassfish.jersey.jnh.connector.JavaNetHttpConnectorProvider; import org.glassfish.jersey.tests.performance.benchmark.server.jnh.Book; import org.glassfish.jersey.tests.performance.benchmark.server.jnh.JNHApplication; +import org.glassfish.jersey.tests.performance.benchmark.server.jnh.JNHttpMethodResource; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -48,6 +50,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.util.List; import java.util.concurrent.TimeUnit; /** @@ -172,6 +175,30 @@ public void measurePostDefault() { .request().post(Entity.json(postDefaultBook)); } + @Benchmark + public void measureBigPost() { + client.target(BASE_URI).path(REQUEST_TARGET).path("postBooks") + .request().post(Entity.entity(convertLongEntity(JNHttpMethodResource.bookList), MediaType.TEXT_PLAIN_TYPE)); + } + + @Benchmark + public void measureBigPostDefault() { + defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("postBooks") + .request().post(Entity.entity(convertLongEntity(JNHttpMethodResource.bookList), MediaType.TEXT_PLAIN_TYPE)); + } + + private static final StreamingOutput convertLongEntity(List books) { + return out -> { + int offset = 0; + while (offset < books.size()) { + out.write(books.get(offset).toByteArray()); + out.write('\n'); + offset++; + } + }; + } + + @Benchmark public void measurePut() { client.target(BASE_URI).path(REQUEST_TARGET).path("putBook") diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java index 732c72d8cc..93e1d39de6 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/Book.java @@ -58,4 +58,8 @@ public int getId() { public void setId(int id) { this.id = id; } + + public byte[] toByteArray() { + return String.format("%s, %s, %d", title, author, id).getBytes(); + } } diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java index 00d475f53e..03bae9eb81 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java @@ -23,9 +23,15 @@ import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; import org.glassfish.grizzly.http.util.HttpStatus; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Collections; import java.util.List; @Path("HttpMethod") @@ -33,7 +39,7 @@ public class JNHttpMethodResource { private static final int BOOK_LIST_SIZE = 1000; - private static final List bookList = generateBooks(); + public static final List bookList = generateBooks(); @GET @Produces(MediaType.TEXT_PLAIN) @@ -63,6 +69,14 @@ public void postBook(Book book) { book.getTitle(), book.getId()));*/ } + @POST + @Path("postBooks") + public Response postBooks(InputStream data) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(data)); + long linesReceived = reader.lines().count(); + reader.close(); + return linesReceived == bookList.size() ? Response.ok().build() : Response.serverError().build(); + } @PUT @Path("putBook") @@ -86,6 +100,6 @@ private static final List generateBooks() { for (int i = 0; i < BOOK_LIST_SIZE; i++) { books.add(new Book("Title: " + i, "Author: " + i, i)); } - return books; + return Collections.unmodifiableList(books); } } From cbfd58e3ada06e34f1b8a28bc6ad9f227b1e7a83 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Thu, 22 Sep 2022 15:06:34 +0200 Subject: [PATCH 5/5] JNH connector perf tests Signed-off-by: Maxim Nesen --- ...dsBenchmark.java => JNHConnectorBenchmark.java} | 14 +++++++------- ...HApplication.java => BookShelfApplication.java} | 6 +++--- ...pMethodResource.java => BookShelfResource.java} | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) rename tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/{JNHConnectorHttpMethodsBenchmark.java => JNHConnectorBenchmark.java} (96%) rename tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/{JNHApplication.java => BookShelfApplication.java} (85%) rename tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/{JNHttpMethodResource.java => BookShelfResource.java} (98%) diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java similarity index 96% rename from tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java rename to tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java index e9e6dda953..42e212f10b 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorHttpMethodsBenchmark.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/JNHConnectorBenchmark.java @@ -29,8 +29,8 @@ import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.jnh.connector.JavaNetHttpConnectorProvider; import org.glassfish.jersey.tests.performance.benchmark.server.jnh.Book; -import org.glassfish.jersey.tests.performance.benchmark.server.jnh.JNHApplication; -import org.glassfish.jersey.tests.performance.benchmark.server.jnh.JNHttpMethodResource; +import org.glassfish.jersey.tests.performance.benchmark.server.jnh.BookShelfApplication; +import org.glassfish.jersey.tests.performance.benchmark.server.jnh.BookShelfResource; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -63,7 +63,7 @@ @Measurement(iterations = 16, time = 1) @Fork(1) @State(Scope.Benchmark) -public class JNHConnectorHttpMethodsBenchmark { +public class JNHConnectorBenchmark { private static final URI BASE_URI = URI.create("http://localhost:8080/"); @@ -81,7 +81,7 @@ public class JNHConnectorHttpMethodsBenchmark { @Setup public void start() throws Exception { server = - GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new JNHApplication(), false); + GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new BookShelfApplication(), false); final TCPNIOTransport transport = server.getListener("grizzly").getTransport(); transport.setSelectorRunnersCount(4); transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig().setCorePoolSize(8).setMaxPoolSize(8)); @@ -178,13 +178,13 @@ public void measurePostDefault() { @Benchmark public void measureBigPost() { client.target(BASE_URI).path(REQUEST_TARGET).path("postBooks") - .request().post(Entity.entity(convertLongEntity(JNHttpMethodResource.bookList), MediaType.TEXT_PLAIN_TYPE)); + .request().post(Entity.entity(convertLongEntity(BookShelfResource.bookList), MediaType.TEXT_PLAIN_TYPE)); } @Benchmark public void measureBigPostDefault() { defaultClient.target(BASE_URI).path(REQUEST_TARGET).path("postBooks") - .request().post(Entity.entity(convertLongEntity(JNHttpMethodResource.bookList), MediaType.TEXT_PLAIN_TYPE)); + .request().post(Entity.entity(convertLongEntity(BookShelfResource.bookList), MediaType.TEXT_PLAIN_TYPE)); } private static final StreamingOutput convertLongEntity(List books) { @@ -227,7 +227,7 @@ public void measureDeleteDefault() { public static void main(final String[] args) throws Exception { final Options opt = new OptionsBuilder() // Register our benchmarks. - .include(JNHConnectorHttpMethodsBenchmark.class.getSimpleName()) + .include(JNHConnectorBenchmark.class.getSimpleName()) .build(); new Runner(opt).run(); diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/BookShelfApplication.java similarity index 85% rename from tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java rename to tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/BookShelfApplication.java index bd3bb83606..d5569d0460 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHApplication.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/BookShelfApplication.java @@ -18,8 +18,8 @@ import org.glassfish.jersey.server.ResourceConfig; -public class JNHApplication extends ResourceConfig { - public JNHApplication() { - register(JNHttpMethodResource.class); +public class BookShelfApplication extends ResourceConfig { + public BookShelfApplication() { + register(BookShelfResource.class); } } diff --git a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/BookShelfResource.java similarity index 98% rename from tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java rename to tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/BookShelfResource.java index 03bae9eb81..03f6c4402d 100644 --- a/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/JNHttpMethodResource.java +++ b/tests/performance/benchmarks/src/main/java/org/glassfish/jersey/tests/performance/benchmark/server/jnh/BookShelfResource.java @@ -35,7 +35,7 @@ import java.util.List; @Path("HttpMethod") -public class JNHttpMethodResource { +public class BookShelfResource { private static final int BOOK_LIST_SIZE = 1000;