6.5.3 muduo 与Nginx 的吞吐量对比(1)
本节简单对比了Nginx 1.0.12 和muduo 0.3.1 内置的简陋HTTP 服务器的长连接性能。其中muduo 的HTTP 实现和测试代码位于muduo/net/http/。
测试环境
服务端,运行HTTP server,8 核DELL 490 工作站,Xeon E5320 CPU。
客户端,运行ab 22 和weighttp 23,4 核i5-2500 CPU。
网络:普通家用千兆网。
测试方法为了公平起见,Nginx 和muduo 都没有访问文件,而是直接返回内存中的数据。毕竟我们想比较的是程序的网络性能,而不是机器的磁盘性能。另外,这里客户机的性能优于服务机,因为我们要给服务端HTTP server 施压,试图使其饱和,而不是测试HTTP client 的性能。
muduo HTTP 测试服务器的主要代码:
- muduo/net/http/tests/HttpServer_test.cc
- void onRequest(const HttpRequest& req, HttpResponse* resp)
- {
- if (req.path() == "/") {
- // ...
- } else if (req.path() == "/hello") {
- resp->setStatusCode(HttpResponse::k200Ok);
- resp->setStatusMessage("OK");
- resp->setContentType("text/plain");
- resp->addHeader("Server", "Muduo");
- resp->setBody("hello, world!\n");
- } else {
- resp->setStatusCode(HttpResponse::k404NotFound);
- resp->setStatusMessage("Not Found");
- resp->setCloseConnection(true);
- }
- }
- int main(int argc, char* argv[])
- {
- int numThreads = 0;
- if (argc > 1)
- {
- benchmark = true;
- Logger::setLogLevel(Logger::WARN);
- numThreads = atoi(argv[1]);
- }
- EventLoop loop;
- HttpServer server(&loop, InetAddress(8000), "dummy");
- server.setHttpCallback(onRequest);
- server.setThreadNum(numThreads);
- server.start();
- loop.loop();
- }
- muduo/net/http/tests/HttpServer_test.cc
Nginx 使用了章亦春的HTTP echo 模块24 来实现直接返回数据。配置文件如下: - #user nobody;
- worker_processes 4;
- events {
- worker_connections 10240;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- access_log off;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 65;
- server {
- listen 8080;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm;
- }
- location /hello {
- default_type text/plain;
- echo "hello, world!";
- }
- }
- }