-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from NiteshKant/master
Adding a perf optimized helloworld example
- Loading branch information
Showing
6 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
71 changes: 71 additions & 0 deletions
71
...ty-examples/src/main/java/io/reactivex/netty/examples/http/plaintext/PlainTextServer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.netty.examples.http.plaintext; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.handler.codec.http.HttpHeaders; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.protocol.http.server.HttpServer; | ||
import io.reactivex.netty.protocol.http.server.HttpServerRequest; | ||
import io.reactivex.netty.protocol.http.server.HttpServerResponse; | ||
import io.reactivex.netty.protocol.http.server.RequestHandler; | ||
import rx.Observable; | ||
|
||
/** | ||
* @author Nitesh Kant | ||
*/ | ||
public final class PlainTextServer { | ||
|
||
static final int DEFAULT_PORT = 8111; | ||
public static final String WELCOME_MSG = "Welcome!!"; | ||
private static final byte[] WELCOME_MSG_BYTES = WELCOME_MSG.getBytes(); | ||
private static final String CONTENT_LENGTH_HEADER_VAL = String.valueOf(WELCOME_MSG_BYTES.length); // Does not use int as this omits conversion to string for every response. | ||
|
||
private final int port; | ||
|
||
public PlainTextServer(int port) { | ||
this.port = port; | ||
} | ||
|
||
public HttpServer<ByteBuf, ByteBuf> createServer() { | ||
HttpServer<ByteBuf, ByteBuf> server = | ||
RxNetty.createHttpServer(port, | ||
new RequestHandler<ByteBuf, ByteBuf>() { | ||
@Override | ||
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, | ||
final HttpServerResponse<ByteBuf> response) { | ||
response.getHeaders().set(HttpHeaders.Names.CONTENT_LENGTH, | ||
CONTENT_LENGTH_HEADER_VAL); // This makes RxNetty write a single Full response as opposed to writing header, content & lastHttpContent. | ||
ByteBuf content = response.getAllocator() | ||
.buffer(WELCOME_MSG_BYTES.length) | ||
.writeBytes(WELCOME_MSG_BYTES); | ||
response.write(content); | ||
return response.close( | ||
false); // Let RxNetty take care of flushing appropriately. Do NOT use when processing in a different thread. | ||
} | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
public static void main(final String[] args) throws InterruptedException { | ||
HttpServer<ByteBuf, ByteBuf> server = new PlainTextServer(DEFAULT_PORT).createServer(); | ||
server.start(); | ||
System.out.println("HTTP plain text server started at port: " + server.getServerPort()); | ||
server.waitTillShutdown(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...tty-examples/src/main/java/io/reactivex/netty/examples/http/plaintext/README.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Overview | ||
======== | ||
|
||
This example is intended to be a sample used for "helloworld" performance tests for RxNetty and has optimizations specific | ||
for helloworld kind of tests. | ||
|
||
The following are the primary differences over the [HelloWorld example](../helloworld) | ||
|
||
- This example does NOT aggregate HTTP requests. | ||
- This example does NOT print the request headers. | ||
|
||
Running | ||
======= | ||
|
||
To run the example execute: | ||
|
||
``` | ||
$ cd RxNetty/rx-netty-examples | ||
$ ../gradlew runPlainTextServer | ||
``` | ||
|
||
and in another console: | ||
|
||
``` | ||
$ cd RxNetty/rx-netty-examples | ||
$ ../gradlew runPlainTextClient | ||
``` |
55 changes: 55 additions & 0 deletions
55
...xamples/src/test/java/io/reactivex/netty/examples/http/plaintext/PlainTextServerTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.netty.examples.http.plaintext; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.reactivex.netty.examples.ExamplesEnvironment; | ||
import io.reactivex.netty.examples.http.helloworld.HelloWorldClient; | ||
import io.reactivex.netty.protocol.http.server.HttpServer; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static io.reactivex.netty.examples.http.plaintext.PlainTextServer.DEFAULT_PORT; | ||
|
||
/** | ||
* @author Tomasz Bak | ||
*/ | ||
public class PlainTextServerTest extends ExamplesEnvironment { | ||
|
||
private HttpServer<ByteBuf, ByteBuf> server; | ||
|
||
@Before | ||
public void setupHttpHelloServer() { | ||
server = new PlainTextServer(DEFAULT_PORT).createServer(); | ||
server.start(); | ||
} | ||
|
||
@After | ||
public void stopServer() throws InterruptedException { | ||
server.shutdown(); | ||
} | ||
|
||
@Test | ||
public void testRequestReplySequence() { | ||
HelloWorldClient client = new HelloWorldClient(DEFAULT_PORT); // The client is no different than hello world. | ||
HttpResponseStatus statusCode = client.sendHelloRequest(); | ||
Assert.assertEquals(HttpResponseStatus.OK, statusCode); | ||
} | ||
} |