Last active
September 3, 2023 18:30
-
-
Save sunng87/c3439bd1dbafc50062c520ee529929a4 to your computer and use it in GitHub Desktop.
Dead simple setup of Jetty servers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS org.eclipse.jetty:jetty-server:11.0.15 | |
//DEPS org.eclipse.jetty:jetty-servlet:11.0.15 | |
import static java.lang.System.*; | |
import java.io.IOException; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.ServerConnector; | |
import org.eclipse.jetty.server.Request; | |
import org.eclipse.jetty.server.Response; | |
import org.eclipse.jetty.server.handler.ContextHandler; | |
import org.eclipse.jetty.servlet.ServletHandler; | |
import org.eclipse.jetty.servlet.ServletContextHandler; | |
import jakarta.servlet.ServletException; | |
import jakarta.servlet.http.HttpServletRequest; | |
import jakarta.servlet.http.HttpServletResponse; | |
public class Jetty11 { | |
public static class EchoHandler extends ServletHandler { | |
@Override | |
public void doHandle(String target, | |
Request baseRequest, | |
HttpServletRequest request, | |
HttpServletResponse response) | |
throws IOException { | |
baseRequest.setHandled(true); | |
response.getWriter().println("data"); | |
} | |
} | |
public static void main(String... args) throws Exception { | |
var server = new Server(3000); | |
var connector = new ServerConnector(server); | |
server.addConnector(connector); | |
server.setHandler(new EchoHandler()); | |
server.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS org.eclipse.jetty:jetty-server:12.0.1 | |
import static java.lang.System.*; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.ServerConnector; | |
import org.eclipse.jetty.server.Request; | |
import org.eclipse.jetty.server.Response; | |
import org.eclipse.jetty.server.Handler; | |
import org.eclipse.jetty.server.handler.ContextHandler; | |
import org.eclipse.jetty.io.Content; | |
import org.eclipse.jetty.util.Callback; | |
public class Jetty12 { | |
public static class EchoHandler extends Handler.Abstract.NonBlocking { | |
@Override | |
public boolean handle(Request req, Response resp, Callback cb) { | |
Content.Sink.write(resp, true, "data", cb); | |
return true; | |
} | |
} | |
public static void main(String... args) throws Exception { | |
var server = new Server(3000); | |
var connector = new ServerConnector(server); | |
server.addConnector(connector); | |
var context = new ContextHandler("/"); | |
context.setHandler(new EchoHandler()); | |
server.setHandler(context); | |
server.start(); | |
} | |
} |
Thank you! Just updated these java code and also let Jetty11 write the same content. The results:
12
+ wrk --latency --timeout 1m -d 10s -c 1000 -t 50 http://localhost:3000/api
Running 10s test @ http://localhost:3000/api
50 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 0.88ms 1.17ms 212.00ms 96.07%
Req/Sec 24.92k 3.73k 51.92k 84.74%
Latency Distribution
50% 714.00us
75% 0.89ms
90% 1.33ms
99% 4.00ms
12243100 requests in 10.10s, 1.16GB read
Socket errors: connect 29, read 0, write 0, timeout 0
Requests/sec: 1212170.12
Transfer/sec: 117.91MB
11
+ wrk --latency --timeout 1m -d 10s -c 1000 -t 50 http://localhost:3000/api
Running 10s test @ http://localhost:3000/api
50 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.08ms 1.88ms 208.52ms 92.84%
Req/Sec 24.62k 6.19k 99.24k 81.95%
Latency Distribution
50% 633.00us
75% 1.18ms
90% 2.43ms
99% 6.82ms
12012328 requests in 10.10s, 1.16GB read
Socket errors: connect 29, read 0, write 0, timeout 0
Requests/sec: 1189640.70
Transfer/sec: 117.99MB
Jetty 12 is slightly fast in terms of throughput. But it has significant improvement on latency. At last we don't see major performance regression with 12. So I still need investigate the clojure side.
@sunng87 great, ping me when you have the data and need help or just to discuss. 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please delete line 23 (
cb.succeeded()
) from the Jetty 12 version, as the callback will be completed when the call toContent.Sink.write()
completes, as you are passingcb
as the callback for the write (no need to complete the callback twice).Also, try
EchoHandler extends Handler.Abstract.NonBlocking
as your implementation ofEchoHandler
is non-blocking, and see how it goes (you should see some performance benefit).Don't hesitate to ping me back if you need help! 😃