Skip to content

Commit bee344e

Browse files
Update Undertow benchmarks
- Update to Undertow Alpha19 - Change to using the builder API to setup the server - Add plantext test support
1 parent 72c51f4 commit bee344e

File tree

4 files changed

+34
-55
lines changed

4 files changed

+34
-55
lines changed

undertow/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
This is the undertow portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
44

55
### JSON Encoding Test
6-
* [JSON test source](src/main/java/hello/HelloServerHandler.java)
6+
* [JSON test source](src/main/java/hello/HelloWebServer.java)
77

88
## Versions
9-
Undertow 1.0.0.Alpha15 (https://github.com/undertow-io/)
9+
Undertow 1.0.0.Alpha19 (http://undertow.io)
1010

1111
## Test URLs
1212

undertow/benchmark_config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"default": {
55
"setup_file": "setup",
66
"json_url": "/",
7+
"plaintext_url": "/plaintext",
78
"port": 8080,
89
"sort": 120
910
}

undertow/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<dependency>
1313
<groupId>io.undertow</groupId>
1414
<artifactId>undertow-core</artifactId>
15-
<version>1.0.0.Alpha15</version>
15+
<version>1.0.0.Alpha19</version>
1616
</dependency>
1717
<dependency>
1818
<groupId>com.fasterxml.jackson.core</groupId>

undertow/src/main/java/hello/HelloWebServer.java

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,55 @@
11
package hello;
22

3-
import java.net.InetAddress;
4-
import java.net.InetSocketAddress;
53
import java.nio.ByteBuffer;
6-
import java.util.HashMap;
4+
import java.util.Collections;
75
import java.util.Map;
86

97
import com.fasterxml.jackson.databind.ObjectMapper;
10-
import io.undertow.UndertowOptions;
11-
import io.undertow.io.IoCallback;
8+
import com.fasterxml.jackson.databind.ObjectWriter;
9+
import io.undertow.Undertow;
1210
import io.undertow.server.HttpHandler;
13-
import io.undertow.server.HttpOpenListener;
1411
import io.undertow.server.HttpServerExchange;
1512
import io.undertow.util.Headers;
16-
import org.xnio.BufferAllocator;
17-
import org.xnio.ByteBufferSlicePool;
18-
import org.xnio.ChannelListener;
19-
import org.xnio.ChannelListeners;
20-
import org.xnio.OptionMap;
21-
import org.xnio.Options;
22-
import org.xnio.Pool;
23-
import org.xnio.StreamConnection;
24-
import org.xnio.Xnio;
25-
import org.xnio.XnioWorker;
26-
import org.xnio.channels.AcceptingChannel;
13+
14+
import static io.undertow.Undertow.builder;
2715

2816
public class HelloWebServer {
2917

30-
private static final ObjectMapper mapper = new ObjectMapper();
18+
private static final ObjectWriter writer = new ObjectMapper().writerWithType(Map.class);
19+
20+
public static final String HELLO_WORLD = "Hello, World!";
3121

3222
private final int port;
23+
private final ByteBuffer buffer;
3324

3425
public HelloWebServer(int port) {
3526
this.port = port;
27+
buffer = ByteBuffer.allocateDirect(HELLO_WORLD.getBytes().length);
28+
buffer.put(HELLO_WORLD.getBytes());
29+
buffer.flip();
3630
}
3731

3832
public void run() throws Exception {
3933

40-
Xnio xnio = Xnio.getInstance("nio", HelloWebServer.class.getClassLoader());
41-
XnioWorker worker = xnio.createWorker(OptionMap.builder()
42-
.set(Options.WORKER_IO_THREADS, Runtime.getRuntime().availableProcessors() * 2)
43-
.set(Options.CONNECTION_HIGH_WATER, 1000000)
44-
.set(Options.CONNECTION_LOW_WATER, 1000000)
45-
.set(Options.TCP_NODELAY, true)
46-
.set(Options.CORK, true)
47-
.getMap());
48-
49-
OptionMap serverOptions = OptionMap.builder()
50-
.set(Options.TCP_NODELAY, true)
51-
.set(Options.REUSE_ADDRESSES, true)
52-
.getMap();
53-
54-
Pool<ByteBuffer> buffers = new ByteBufferSlicePool(BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR, 2048, 2048 * 2048);
55-
56-
HttpHandler rootHandler = new HttpHandler() {
57-
@Override
58-
public void handleRequest(final HttpServerExchange exchange) throws Exception {
59-
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
60-
Map<String, String> data = new HashMap<String, String>();
61-
data.put("message", "Hello, world");
62-
String response = mapper.writeValueAsString(data);
63-
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, response.length());
64-
exchange.getResponseSender().send(response, IoCallback.END_EXCHANGE);
65-
}
66-
};
67-
68-
HttpOpenListener openListener = new HttpOpenListener(buffers, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true), 2048);
69-
openListener.setRootHandler(rootHandler);
70-
ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
71-
AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(InetAddress.getByAddress(new byte[]{0, 0, 0, 0}), port), acceptListener, serverOptions);
72-
server.resumeAccepts();
73-
34+
Undertow undertow = builder()
35+
.addListener(port, "0.0.0.0")
36+
.setBufferSize(1024 * 16)
37+
.setHandler(new HttpHandler() {
38+
@Override
39+
public void handleRequest(final HttpServerExchange exchange) throws Exception {
40+
if (exchange.getRelativePath().equals("/plaintext")) {
41+
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
42+
exchange.getResponseSender().send(buffer.duplicate());
43+
} else {
44+
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
45+
Map<String, String> data = Collections.singletonMap("message", "Hello, world");
46+
String response = writer.writeValueAsString(data);
47+
exchange.getResponseSender().send(response);
48+
}
49+
}
50+
}).build();
7451

52+
undertow.start();
7553
}
7654

7755
public static void main(String[] args) throws Exception {

0 commit comments

Comments
 (0)