mongols C++ 服务器基础设施开源项目

我要开发同款
匿名用户2018年07月23日
57阅读
开发技术C/C++
所属分类高性能网络开发库、程序开发
授权协议MPL-2.0

作品详情

mongols 是一个C++库。

依赖:

linux

pcre-devel

hiredis-devel

zlib-devel

openssl-devel

curl-devel

sudo yum install pcre-devel zlib-devel openssl-devel hiredis-devel libcurl-devel

或者:

sudo apt-get install libpcre3-dev zlib1g-dev libssl-dev libhiredis-dev libcurl4-openssl-dev特性:

epollorepoll+multi-threading

tcpsever

httpserver

websocketserver

webserver

leveldbserver

luaserver

sqliteserver

utils

安装:

makeclean&&make-j2&&sudomakeinstall&&sudoldconfig

使用:

pkg-config--libs--cflagsmongols

例子:#include<mongols/tcp_server.hpp>#include<mongols/tcp_threading_server.hpp>#include<mongols/http_server.hpp>#include<mongols/ws_server.hpp>#include<mongols/web_server.hpp>#include<mongols/leveldb_server.hpp>#include<mongols/util.hpp>#include/*//websocket serverint main(int,char**){int port=9090;const char* host="127.0.0.1";mongols::ws_server server(host,port,5000,2048,4);auto f=[](const std::string& input            , bool& keepalive            , bool& send_to_other            , mongols::tcp_server::client_t& client            , mongols::tcp_server::filter_handler_function& send_to_other_filter){keepalive = KEEPALIVE_CONNECTION;send_to_other=true;if(input=="close"){keepalive = CLOSE_CONNECTION;send_to_other = false;}};server.run(f);//server.run();}*//*//tcp server or tcp multi-threading serverint main(int,char**){auto f=[](const std::string& input , bool & keepalive                , bool& send_to_other                , mongols::tcp_server::client_t& client                , mongols::tcp_server::filter_handler_function& send_to_other_filter){keepalive= KEEPALIVE_CONNECTION;send_to_other=true;return input;};int port=9090;const char* host="127.0.0.1";//mongols::tcp_threading_servermongols::tcp_serverserver(host,port);server.run(f);}*///http server or multi-threading serverint main(int,char**){auto f=[](const mongols::request&){return true;};auto g=[](const mongols::request& req,mongols::response& res){//std::unordered_map::const_iterator i;//if((i=req.session.find("test"))!=req.session.end()){//long test=std::stol(i->second)+1;//res.content=std::to_string(test);//res.session["test"]=res.content;//}else{//res.content=std::to_string(0);;//res.session["test"]=res.content;//}res.content=std::move("hello,world");res.status=200;};int port=9090;const char* host="127.0.0.1";mongols::http_server //server(host,port,5000,1024,4);server(host,port);server.set_enable_session(false);server.set_enable_cache(false);server.run(f,g);}/*//web server or multi-threading serverint main(int,char**){auto f=[](const mongols::request&){if(req.method=="GET"&&req.uri.find("..")==std::string::npos){return true;}return false;};int port=9090;const char* host="127.0.0.1";mongols::web_server //server(host,port,5000,1024,4);server(host,port);server.set_root_path("html");server.set_mime_type_file("mime.conf");server.set_list_directory(true);server.run(f);}*//*//leveldb_server or multi-threading serverint main(int,char**){int port=9090;const char* host="127.0.0.1";mongols::leveldb_server //server(host,port,5000,1024,4);server(host,port);server.run("html/leveldb");}*/

压力测试:

[centos@localhost demo]$ uname -aLinux localhost 3.10.0-862.9.1.el7.x86_64 #1 SMP Mon Jul 16 16:29:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux[centos@localhost demo]$ cat /proc/cpuinfo| grep "processor"| wc -l4[centos@localhost demo]$ cat /proc/cpuinfo| grep "cpu cores"| uniqcpu cores: 2[centos@localhost demo]$ cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l1ab-c1000-n100000https://127.0.0.1:9090/Server Software:        mongols/0.9.0Server Hostname:        127.0.0.1Server Port:            9090Document Path:          /Document Length:        13 bytesConcurrency Level:      1000Time taken for tests:   8.180 secondsComplete requests:      100000Failed requests:        0Write errors:           0Keep-Alive requests:    0Total transferred:      13300000 bytesHTML transferred:       1300000 bytesRequests per second:    12225.00 [#/sec] (mean)Time per request:       81.800 [ms] (mean)Time per request:       0.082 [ms] (mean, across all concurrent requests)Transfer rate:          1587.82 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0   66 263.3      7    3020Processing:     2   11  11.9      9     831Waiting:        0    8  11.5      7     829Total:          6   77 265.8     16    3033Percentage of the requests served within a certain time (ms)  50%     16  66%     20  75%     22  80%     23  90%     29  95%   1017  98%   1025  99%   1030 100%   3033 (longest request)ab-kc1000-n100000-H'Connection:keep-alive'https://127.0.0.1:9090/Server Software:        mongols/0.9.0Server Hostname:        127.0.0.1Server Port:            9090Document Path:          /Document Length:        13 bytesConcurrency Level:      1000Time taken for tests:   1.935 secondsComplete requests:      100000Failed requests:        0Write errors:           0Keep-Alive requests:    100000Total transferred:      13800000 bytesHTML transferred:       1300000 bytesRequests per second:    51671.74 [#/sec] (mean)Time per request:       19.353 [ms] (mean)Time per request:       0.019 [ms] (mean, across all concurrent requests)Transfer rate:          6963.57 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   3.2      0      48Processing:     8   19   1.4     18      48Waiting:        0   19   1.4     18      25Total:          8   19   3.1     18      64Percentage of the requests served within a certain time (ms)  50%     18  66%     19  75%     19  80%     19  90%     20  95%     21  98%     23  99%     28 100%     64 (longest request)
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论