2019-09-04, golang vs java 性能测试,HTTP服务,keepalive

aside section ._1OhGeD · · 766 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

先贴代码,这种测试没多少意义,仅作为记录。以后搞清楚golang的套路之后,估计性能还能有提高的空间。

因为对golang还不是很熟悉,所以只能写成这个程度了。希望未来能打脸。

package main

import "net/http"

type handler struct{}

var welcome = []byte{97, 98}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    //OK
    w.WriteHeader(200)
    if _, e := w.Write(welcome); e != nil {
        panic(e)
    }
}

func main() {
    server := http.Server{
        Addr:    "0.0.0.0:8088",
        Handler: &handler{},
    }

    err := server.ListenAndServe()

    if err != nil {
        panic(err)
    }
}

Java使用最新的稳定版本netty 4.1.39.Final,不使用tc-native,不调优。

public class SimpleHttpServer {
    public static void main(String[] args) throws InterruptedException {

        ServerBootstrap server = new ServerBootstrap()
                .group(new NioEventLoopGroup(), new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new HttpServerCodec())
                                .addLast(new HttpHelloWorldServerHandler());
                    }
                });

        Channel ch = server.bind(8088).sync().channel();
        ch.closeFuture().sync();
    }
}
public class HttpHelloWorldServerHandler extends SimpleChannelInboundHandler<HttpObject> {
    private static final byte[] CONTENT = {97, 98};

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest req = (HttpRequest) msg;

            boolean keepAlive = HttpUtil.isKeepAlive(req);
            FullHttpResponse response = new DefaultFullHttpResponse(req.protocolVersion(), OK,
                    Unpooled.wrappedBuffer(CONTENT));

            if (keepAlive) {
                if (!req.protocolVersion().isKeepAliveDefault()) {
                    response.headers().set(CONNECTION, KEEP_ALIVE);
                }
            } else {
                // Tell the client we're going to close the connection.
                response.headers().set(CONNECTION, CLOSE);
            }

            ChannelFuture f = ctx.write(response);

            if (!keepAlive) {
                f.addListener(ChannelFutureListener.CLOSE);
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

笔记本测试,性能不是太高,CPU太烂(i5 8250U 1.6GHz 1.8GHz),系统(windows 10 home basic)

golang http server
28900 requests/sec

netty http server
冷启动:31000 requests/sec
热身后:35900 requests/sec

测试指令

ab -n 1000000 -c 1000 -k http://localhost:8088/hello/world

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:aside section ._1OhGeD

查看原文:2019-09-04, golang vs java 性能测试,HTTP服务,keepalive

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

766 次点击  
加入收藏 微博
1 回复  |  直到 2019-10-08 10:20:57
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传