Skip to content

Instantly share code, notes, and snippets.

@onlyeat3
Created June 20, 2018 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onlyeat3/d6cfd882b465b81c94687880770fac15 to your computer and use it in GitHub Desktop.
Save onlyeat3/d6cfd882b465b81c94687880770fac15 to your computer and use it in GitHub Desktop.
simple-benchmarking
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import lombok.Data;
import org.junit.Test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HttpServerTest {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8001), 128);
server.createContext("/jdk", new HttpHandler() {
public void handle(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(200, 0);
OutputStream out = exchange.getResponseBody();
out.write("Hello World".getBytes());
out.close();
}
});
int nThreads = Runtime.getRuntime().availableProcessors() * 2;
// ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
ExecutorService executorService = Executors.newWorkStealingPool(nThreads);
server.setExecutor(executorService);
System.out.println(server.getExecutor());
server.start();
System.out.println("end.");
}
}
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import lombok.Data;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
public class NettyHttpTest {
public static void main(String[] args) throws InterruptedException {
int nThreads = Runtime.getRuntime().availableProcessors() * 2;
NioEventLoopGroup bossGroup = new NioEventLoopGroup(nThreads);
NioEventLoopGroup workerGroup = new NioEventLoopGroup(nThreads);
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new HttpResponseEncoder())
.addLast(new HttpRequestDecoder())
.addLast(new HttpServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG,128);
ChannelFuture cf = serverBootstrap.bind(8002).sync();
cf.channel().closeFuture().sync();
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
static class HttpServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
FullHttpResponse response = new DefaultFullHttpResponse(
HTTP_1_1, OK, Unpooled.wrappedBuffer("Hello World"
.getBytes()));
ctx.write(response);
ctx.close();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
ctx.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment