开发者社区> 问答> 正文

【译】Java 7 NIO.2 异步 IO vs ANSI C 性能测试? 400 报错

【译】Java 7 NIO.2 异步 IO vs ANSI C 性能测试? 400 报错

原文:http://www.hernandezgomez.com/2011/01/23/java-7-nio-2-asynchronous-io-vs-ansi-c-performance/

我喜欢JAVA,我也喜欢ANSI C

曾经,我认为ANSI CJAVA 快的多,但是最近看起来这并不完全正确了。

我对Java 7 new NIO.2 异步 I/O APIs非常感兴趣,让我们来做个简单的比较。

我喜欢 G-WAN它是我见过的最快的服务器.它是由 ANSI C写的.

让我们写一个G-WAN上的hello.c” servlet 示例:

#include "gwan.h"    // G-WAN exported functions

int main(int argc, char *argv[])
{
	xbuf_t reply;
	get_reply(argv, &reply);

	xbuf_cat(&reply, "<h1>HELLO WORLD</h1>");

	set_reply(argv, &reply);
	return 200; // return an HTTP code (200:'OK')
}

这是一个返回“Hello worldHTML页面的简单servlet

让我们用Apache Benchmark在我的奔四3G老电脑上做个测试:

# ab -c 4 -t 60 -n 10000000 http://127.0.0.1:8080/csp/hello

结果:

完成请求336194
HTML 传输: 6723880 bytes

并发加到100:

# ab -c 100 -t 60 -n 10000000 http://127.0.0.1:8080/csp/hello

结果差不多:

完成请求339000
HTML 传输: 6780000 bytes

并发加到200:

# ab -c 200 -t 60 -n 10000000 http://127.0.0.1:8080/csp/hello

结果还是类似:

完成请求333252
HTML 传输: 6665040 bytes

OK, 让我们用Java 7 NIO.2试试这个例子

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyServer {

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

    ExecutorService executorService =
        Executors.newCachedThreadPool(
            Executors.defaultThreadFactory());

    AsynchronousChannelGroup threadGroup =
        AsynchronousChannelGroup
            .withCachedThreadPool(executorService, 1);

    final AsynchronousServerSocketChannel server =
        AsynchronousServerSocketChannel.open(threadGroup);

    server.bind(new InetSocketAddress(8080));

    server.accept(null,
        new CompletionHandler() {

      ByteBuffer readBuffer = ByteBuffer.allocate(1024);
      byte[] request = new byte[1024];

      final ByteBuffer helloRequest =
          ByteBuffer.wrap("/csp/hello".getBytes());

      public void completed(AsynchronousSocketChannel result,
          Void attachment) {
        try {
          readBuffer.clear();
          result.read(readBuffer).get();
          readBuffer.position(4);
          readBuffer.get(request, 0, 10);

          if (ByteBuffer.wrap(request, 0, 10)
              .compareTo(helloRequest) == 0) {
            result.write(ByteBuffer.wrap("HTTP/1.1 200 OK\n"
                + "Content-Type: text/html; charset=utf-8\n"
                + "Content-Length: 20\n\n"
                + "<h1>HELLO WORLD</h1>"
                .getBytes()));
          }
        }
        catch (Exception e) {
          System.out.println(e.toString());
        }
        finally {
          try {
            result.close();
            server.accept(null, this);
          }
          catch (Exception e) {
            System.out.println(e.toString());
          }
        }
      }

      public void failed(Throwable exc, Void attachment) {
        throw new UnsupportedOperationException("Not supported yet.");
      }
    });

    // Wait
    System.in.read();

    server.close();
  }
}

编译并运行上面的代码:

# javac -O -g:none MyServer.java
# java -server MyServer

我们的JAVA服务器正在运行.

让我们把刚才在G-WAN上的测试用JAVA再做一遍

# ab -c 4 -t 60 -n 10000000 http://127.0.0.1:8080/csp/hello

结果:

完成请求302652
HTML 传输: 6053040 bytes

并发加到100:

# ab -c 100 -t 60 -n 10000000 http://127.0.0.1:8080/csp/hello

结果:

完成请求308275
HTML 传输: 6165500 bytes

并发加到200:

# ab -c 200 -t 60 -n 10000000 http://127.0.0.1:8080/csp/hello

结果:

完成请求301695
HTML 传输: 6033900 bytes

在这个例子里C测试服务器比JAVA测试快了10%

我本来以为JAVA会更慢的。

你可以自己测试一下。

展开
收起
爱吃鱼的程序员 2020-06-03 14:51:16 599 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    用NIO2做一个下载服务器,然后让100个客户端来下载一个文件,计算每个用户的下载时间可以实现吗?如何实现?谢谢######下載服務器一般不需要經過app層

    2020-06-03 17:20:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
多IO线程优化版 立即下载