• Email:
  • Feeds

  • Archive for the ‘编程’ Category


    C, Erlang, Java and Go Web Server performance test

    I had tested a hello world web server in C, Erlang, Java and the Go programming language.
    * C, use the well-known high performance web server nginx, with a hello world nginx module
    * Erlang/OTP
    * Java, using the MINA 2.0 framework, now the JBoss Netty framework.
    * Go, http://golang.org/

    1. Test environment

    1.1 Hardware/OS

    2 Linux boxes in a gigabit ethernet LAN, 1 server and 1 test client
    Linux Centos 5.2 64bit
    Intel(R) Xeon(R) CPU E5410  @ 2.33GHz (L2 cache: 6M), Quad-Core * 2
    8G memory
    SCSI disk (standalone disk, no other access)

    1.2 Software version

    nginx, nginx-0.7.63.tar.gz
    Erlang, otp_src_R13B02-1.tar.gz
    Java, jdk-6u17-linux-x64.bin, mina-2.0.0-RC1.tar.gz, netty-3.2.0.ALPHA1-dist.tar.bz2
    Go, hg clone -r release https://go.googlecode.com/hg/ $GOROOT (Nov 12, 2009)

    1.3 Source code and configuration

    Linux, run sysctl -p

    net.ipv4.ip_forward = 0
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    kernel.panic = 1
    net.ipv4.tcp_rmem = 8192	873800	8738000
    net.ipv4.tcp_wmem = 4096	655360	6553600
    net.ipv4.ip_local_port_range = 1024	65000
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216

    # ulimit -n
    150000

    C: ngnix hello world module, copy the code ngx_http_hello_module.c from http://timyang.net/web/nginx-module/

    in nginx.conf, set “worker_processes  1; worker_connections 10240″ for 1 cpu test, set “worker_processes  4; worker_connections 2048″ for multi-core cpu test. Turn off all access or debug log in nginx.conf, as follows

    worker_processes  1;
    worker_rlimit_nofile 10240;
    events {
        worker_connections  10240;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  0;
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
              location /hello {
                ngx_hello_module;
                hello 1234;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

    $ taskset -c 1 ./nginx or $ taskset -c 1-7 ./nginx

    Erlang hello world server
    The source code is available at yufeng’s blog, see http://blog.yufeng.info/archives/105
    Just copy the code after “cat ehttpd.erl”, and compile it.

    $ erlc ehttpd.erl
    $ taskset -c 1 erl +K true +h 99999 +P 99999 -smp enable +S 2:1 -s ehttpd
    $ taskset -c 1-7 erl +K true -s ehttpd
    We use taskset to limit erlang vm to use only 1 CPU/core or use all CPU cores. The 2nd line is run in single CPU mode, and the 3rd line is run in multi-core CPU mode.

    Java source code, save the 2 class as HttpServer.java and HttpProtocolHandler.java, and do necessary import.

    public class HttpServer {
        public static void main(String[] args) throws Exception {
            SocketAcceptor acceptor = new NioSocketAcceptor(4);
            acceptor.setReuseAddress( true );
    
    		int port = 8080;
    		String hostname = null;
    		if (args.length > 1) {
    			hostname = args[0];
    			port = Integer.parseInt(args[1]);
    		}
    
            // Bind
            acceptor.setHandler(new HttpProtocolHandler());
            if (hostname != null)
            	acceptor.bind(new InetSocketAddress(hostname, port));
            else
            	acceptor.bind(new InetSocketAddress(port));
    
            System.out.println("Listening on port " + port);
            Thread.currentThread().join();
        }
    }
    
    public class HttpProtocolHandler extends IoHandlerAdapter {
        public void sessionCreated(IoSession session) {
            session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
            session.setAttribute(SslFilter.USE_NOTIFICATION);
        }
    
        public void sessionClosed(IoSession session) throws Exception {}
        public void sessionOpened(IoSession session) throws Exception {}
        public void sessionIdle(IoSession session, IdleStatus status) {}
        public void exceptionCaught(IoSession session, Throwable cause) {
            session.close(true);
        }
    
        static IoBuffer RESULT = null;
    	public static String HTTP_200 = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\n" +
    			"hello world\r\n";
    	static {
        	RESULT = IoBuffer.allocate(32).setAutoExpand(true);
        	RESULT.put(HTTP_200.getBytes());
        	RESULT.flip();
        }
        public void messageReceived(IoSession session, Object message)
                throws Exception {
            if (message instanceof IoBuffer) {
            	IoBuffer buf = (IoBuffer) message;
            	int c = buf.get();
            	if (c == 'G' || c == 'g') {
            		session.write(RESULT.duplicate());
            	}
            	session.close(false);
            }
        }
    }

    Nov 24 update Because the above Mina code doesn’t parse HTTP request and handle the necessary HTTP protocol, replaced with org.jboss.netty.example.http.snoop.HttpServer from Netty example, but removed all the string builder code from HttpRequestHandler.messageReceived() and just return a “hello world” result in HttpRequestHandler.writeResponse(). Please read the source code and the Netty documentation for more information.

    $ taskset -c 1-7 \
    java -server -Xmx1024m -Xms1024m -XX:+UseConcMarkSweepGC -classpath . test.HttpServer 192.168.10.1 8080

    We use taskset to limit java only use cpu1-7, and not use cpu0, because we want cpu0 dedicate for system call(Linux use CPU0 for network interruptions).

    Go language, source code

    package main
    import (
       "http";
        "io";
    )
    func HelloServer(c *http.Conn, req *http.Request) {
        io.WriteString(c, "hello, world!\n");
    }
    func main() {
         runtime.GOMAXPROCS(8); // 8 cores
         http.Handle("/", http.HandlerFunc(HelloServer));
         err := http.ListenAndServe(":8080", nil);
        if err != nil {
            panic("ListenAndServe: ", err.String())
        }
    }

    $ 6g httpd2.go
    $ 6l httpd2.6
    $ taskset -c 1-7 ./6.out

    1.4 Performance test client

    ApacheBench client, for 30, 100, 1,000, 5,000 concurrent threads
    ab -c 30 -n 1000000 http://192.168.10.1:8080/
    ab -c 100 -n 1000000 http://192.168.10.1:8080/
    1000 thread, 334 from 3 different machine
    ab -c 334 -n 334000 http://192.168.10.1:8080/
    5000 thread, 1667 from 3 different machine
    ab -c 1667 -n 334000 http://192.168.10.1:8080/

    2. Test results

    2.1 request per second

    30 (threads) 100 1,000 5,000
    Nginx html(1C) 21,301 21,331 23,746 23,502
    Nginx module(1C) 25,809 25,735 30,380 29,667
    Nginx module(Multi-core) 25,057 24,507 31,544 33,274
    Erlang(1C) 11,585 12,367 12,852 12,815
    Erlang(Multi-Core) 15,101 20,255 26,468 25,865
    Java, Mina2(without HTTP parse)
    30,631 26,846 31,911 31,653
    Java, Netty 24,152 24,423 25,487 25,521
    Go 14,080 14,748 15,799 16,110

    c_erlang_java_go
    2.2 latency, 99% requests within(ms)

    30 100 1,000 5,000
    Nginx html(1C) 1 4 42 3,079
    Nginx module(1C) 1 4 32 3,047
    Nginx module(Multi-core) 1 6 205 3,036
    Erlang(1C) 3 8 629 6,337
    Erlang(Multi-Core) 2 7 223 3,084
    Java, Netty 1 3 3 3,084
    Go 26 33 47 9,005

    3. Notes

    * On large concurrent connections, C, Erlang, Java no big difference on their performance, results are very close.
    * Java runs better on small connections, but the code in this test doesn’t parse the HTTP request header (the MINA code).
    * Although Mr. Yu Feng (the Erlang guru in China) mentioned that Erlang performance better on single CPU(prevent context switch), but the result tells that Erlang has big latency(> 1S) under 1,000 or 5,000 connections.
    * Go language is very close to Erlang, but still not good under heavy load (5,000 threads)
    After redo 1,000 and 5,000 tests on Nov 18
    * Nginx module is the winner on 5,000 concurrent requests.
    * Although there is improvement space for Go, Go has the same performance from 30-5,000 threads.
    * Erlang process is impressive on large concurrent request, still as good as nginx (5,000 threads).

    4. Update Log

    Nov 12, change nginx.conf work_connections from 1024 to 10240
    Nov 13, add runtime.GOMAXPROCS(8); to go’s code, add sysctl -p env
    Nov 18, realized that ApacheBench itself is a bottleneck under 1,000 or 5,000 threads, so use 3 clients from 3 different machines to redo all tests of 1,000 and 5,000 concurrent tests.
    Nov 24, use Netty with full HTTP implementation to replace Mina 2 for the Java web server. Still very fast and low latency after added HTTP handle code.

    关于阙宏宇lighttpd与mod_cache在twitter上的一些讨论

    6月28日的beta技术沙龙上阙宏宇介绍了lighttpdmodcache,视频及slide在 http://club.blogbeta.com/76.html,modcache是阙宏宇开发的一个lighttpd module, 它在部分程度上可以替代squid。在活动的介绍上提到

    lighttpd是基于事件驱动的高性能web服务器。mod_cache是在lighttpd上运行的缓存插件。lighttpd+mod_cache 搭建的缓存系统,具有配置简单,性能高,在很多大型系统得到了广泛应用。本次活动由mod_cache作者阙宏宇介绍lighttpd的基础知识,讲解 lighttpd高性能的原理,讨论lighttpd的扩展编写方法等高级话题。也将分享mod_cache插件的特点和使用经验。

    在看视频的过程中我在twitter上也分享了阙宏宇的一些观点,同时也引起了一些网友的关注及讨论,部分内容摘录如下(下文每行开头的链接是发言人的twitter ID)

    xmpp: 刚看到阙宏宇的lighttpd mod_cache视频,他的调优建议把keep-alive设成=0(相当于关掉),个人不是很认同

    xmpp: 阙宏宇称”使用lighttpd+mod_cache比squid快一个数量级,原因是lighttpd用event/file, squid用thread/db“ 视频第41分钟

    xmpp: 阙宏宇最后说web server使用多线程是倒退,并拿memcached单线程跑得很好来证明。但是我觉得并不能单纯这么考虑,一是多核CPU被浪费,二是web server(io密集, 慢连接)和cache server特性有区别不能简单相提并论。

    SnaiX: @xmpp 我觉得阙宏宇的言论很有问题,首先所谓的event based也是需要较大消耗的,且,squid有n中模式可以选择,包括了所谓的db based files based。

    xmpp: @SnaiX 是阿,他的软件对于某些场景可能有意义,但是大部分情况还是没法代替squid的,视频最后Q&A也看到,比如没法实现合并多个相同资源的并发请求等

    xmpp: @eishn: “对于任务的处理, 不可避免会涉及到 multithreading 的问题” 是 @yingfeng 提到的,在我的理解,“任务”是指为了返回请求,需要消耗cpu及io等待,直至输出内容返回网络这个执行过程

    yingfeng: @xmpp 读过POSA II便可知道event based只是解决了acceptor端的问题,对于任务的处理,不可避免会涉及到multithreading的问题

    xmpp: @SnaiX: 批评多线程者无非就是批评锁,所以1最极端者就用单线程,2退一步者就线程不共享内存,线程间用消息通讯,3普通多线程通常加锁,因此会带来等待锁(其实Semaphore锁对等待线程没什么开销的)及线程切换开销,因此开发多线程程序只要了解底层原理就没什么恐惧的

    xmpp: @eishn 你说的是实现方法是协程吗,我倒很想看到你说的实现方法详细描述。如果是协程,它虽然避免了锁,但还是存在切换开销,而且也不能随时切换,我觉得如要利用多核优势,多线程是大势所趋

    alpha86: @xmpp 静态文件的请求可以开keep-alive, 动态请求的话其实keep-alive起不到多大效果。

    Joshfeng: @xmpp 我个人觉得keep-alive的设置要看服务器上主要是什么类型的文件

    xiaoxiaolu: RT @xmpp: … 他的调优建议把keep-alive设成=0(相当于关掉),个人不是很认同//就web而言,我挺认同的

    mikespook: RT: @xiaoxiaolu: RT @xmpp: 刚看到….调优建议把keep-alive设成=0(相当于关掉),个人不是很认同//就web而言,我挺认同的// 具体情况具体处理,keep-alive 对小文件友好,对高并发不利~

    rockyzh: @xmpp 同意,现在已经进入多核时代了,当年的squid以高效的单线程著称,现在可得改改了

    TerryTsui: RT: @xmpp: 阙宏宇最后说web server使用多线程是倒退,并拿memcached单线程跑得很好来证明。。。 //memcache 也可以是多线程跑啊

    xiaoxiaolu: RT @mikespook: RT: @xiaoxiaolu: RT @xmpp:调优建议把keep-alive设成=0,个人不是很认同//我挺认同// 具体情况具体处理,keep-alive 对小文件友好,对高并发不利~//高并发看情况,若跳出率很高,就关掉,省事的话,一律关掉

    SnaiX: @xmpp 在他视频20分钟左右的位置,提到squid和lighty的比较。提到“能不用线程就不用线程,尤其是几个线程在做同一个事情,用这种事件驱动的模型更好“,我不赞同,这种并发的目的就是用来做同一个事情的。其次就是线程模型和事件驱动一点也不冲突。

    SnaiX: @xmpp memcached的模型明显是比较简单的模型,为了实现方便而已。所以mc这种东西如果你不适用线程模式或者不对其进行一些特殊优化, 是很难用来吞吐大数据的,因为单线程会使得在write较多数据的时候占用的CPU时间太长,从而直接影响了对别的请求的响应。

    tangfl: @SnaiX @xmpp snaix 应该大力推销一下 csf 模式:前端 event driven,中间任务队列调度,后面线程池处理,完美了。。。

    eishn: @xmpp 阙宏宇说webserv使用多线程是倒退,基本赞同。不过其mcachd例子不妥,常规evt负载亦不低。然单线程异步io性能甚于多线程,高并发场合由 是,故多线程非最佳。然实现逻辑,多线程是最易用的,故最佳形式是以模拟多线程写逻辑,而以单线程异步实现,这也是我的做法。

    jiaojing: RT: @jeffz_cn: rt @xmpp RT: @yingfeng: @xmpp 读过POSA II便可知道event based只是解决了acceptor端的问题,对于任务的处理,…//多线程也只是用来做cpu时间片的分配,协程也是一个不错的选择.切换代价更 小

    SnaiX: @xmpp 我认为,如果仅仅是为了避免多线程所造成的程序复杂度上升而在该使用多线程的地方却不使用多线程,是一种无能的表现。譬如说,有言论说:多线程造成你的程序一旦有问题,整个进程就core dump了,所以不稳定。可关键是,你的程序应该有问题么?

    SnaiX: RT @tangfl: @SnaiX @xmpp 其实CSF也没什么可高深的,只是我觉得需要有这么一个东西,这样很多东西就可控了。做技术的,不能整天靠凑合来凑合去的过日子。

    eishn: @xmpp stackless python 协程可以做到随时切换, 不过不能达到 erlang 那样的协程级别的多核分配, 尽管可以由用户自己非原生实现。协程虽然还是有切换开销, 但是没有无谓切换, 只在类似 io 等待的场合下才会发生切换, 所以切换次数远小于线程。

    jiaojing: RT: @eishn: @xmpp stackless python 协程可以做到随时切换, 不过不能达到 erlang 那样的协程级别的多核分配, …。// erlang的smp也是用多线程(一个核一个线程,ps保证)跑多个scheduler.

    flierlu: 多线程领域也是在不停发展的,近几年 lock-free 和 wait-free 算法逐渐成熟,加上线程局部内存池等等技术,只要设计上不出大问题,肯定能超越多进程

    zhuzhaoyuan: @xmpp: @yingfeng: 读过POSA II便可知道event based只是解决了acceptor端的问题,对于任务的处理,不可避免会涉及到multithreading的问题 || 反对,要看服务类型是IO密集型还是计算密集型的,IO密集型的不需要多线程。

    附加说明

    • 在Twitter中,@表示reply某人, 类似email中的答复,RT表示retweet,类似email中的转发。
    • 从上面看到,Twitter在讨论上处理线索功能上还是比较弱,需要重复RT原文来串联上下文,很需要类似gmail那样conversation的功能。
    • 我的twitter ID是xmpp,欢迎大家follow。

    中国共有多少台服务器-初略估算初窥

    自从看了《编程珠玑》第7章粗略估算之后就养成了一个奇怪的习惯,喜欢去计算身边的数字。比如一个城市共有多少台电梯,中国共有多少座桥梁等。

    粗略估算又叫费米近似(Fermi problem),比如坐广州到深圳的和谐号动车组时候就想,广深线每分钟进入火车站人流量有多大?这个似乎要请专业的调查公司才能得到结果,但实际上每个人几乎都可以算出来,尤其是程序员。

    1. 和谐号列车有8个车厢,1号和8号是头等车厢,一般坐不满 ,满员估算50人。除去5号餐车。因此共有5×100+100=600个座位。
    2. 15分钟一趟,每天从6点到22点,共发车18*(60/15)=72次
    3. 根据观察,广深线平时都有座位,但也不会太空。假如平均坐满70%座位的话,每天单向人流量为 72 * 600 * 70% = 30240 人
    4. 每分钟人流量为30240/18/60=28人
    5. 进一步考虑,需要几个售票窗口。估算平均每个乘客购票需要15秒,则至少7个窗口才不会引起排队现象。这跟平时观察一致,平时非高峰时刻如果窗口在5个以下会出现排长队现象。

    72法则

    72法则是会计上的一个经验。假设最初投资金额为100元,复息年利率9%(r%),利用“72法则”,将72除以9(增长率),得8(y),即需约8年时间,投资金额滚存至200元(两倍于100元),而准确需时为8.0432年。上面的r和y换成任何数字,只要相乘总数是72, 该法则都成立。

    72法则在初略估算中经常要用到,比如上面广深线的例子,假如客流月增长率3%, 则24月之后,广深线客流量会翻一番。(24*3=72)

    编程领域估算

    服务器编程领域经常面临预先估算,因为在程序开发前实际的场景并不存在。据去年《程序员》杂志介绍,奥运订票网站的瘫痪,是因为每秒请求数超过2200次。假如这个请求数都不能预先估算到的话,应该算是架构设计的失败。

    更多有趣例子及深入阅读

    • 中国共有多少台正在运行中的服务器?
    • 你有多少根头发?
    • 成年人体的骨头块数。
    • 孔子的出生年份(公元)

    更多有趣的例子可参看美国马里兰大学更多初略估算测试大全(英文)