mongols-0.9.2 发布,C++ 服务器基础设施
2018-08-06 来源:oschina
mongols-0.9.2已经发布。
主要更新:
添加zstd库
为http server优化会话和缓存管理
介绍:
mongols是一个主要针对c++服务器端开发的基础设施库。主要特性如下:
tcp sever (epoll)
tcp multi-threading server (epoll+multi-threading)
http server (epoll)
http multi-threading server (epoll+multi-threading)
websocket server (epoll+multi-threading)
utils
依赖:
linux
gcc(c11),g++(c++11)
pcre-devel
zlib-devel
openssl-devel
sudo yum install pcre-devel zlib-devel openssl-devel
或者:
sudo apt-get install libpcre3-dev zlib1g-dev libssl-dev
安装:
make clean && make -j2 && sudo make install && sudo ldconfig
使用:
pkg-config --libs --cflags mongols
例子:
#include <mongols/tcp_server.hpp> #include <mongols/tcp_threading_server.hpp> #include <mongols/http_server.hpp> #include <mongols/ws_server.hpp> #include <mongols/util.hpp> #include <iostream> /* //websocket server int main(int,char**){ int port=9090; const char* host="127.0.0.1"; mongols::ws_server server(host,port); auto f=[](const std::string& input , bool& keepalive , bool& send_to_other , std::pair<size_t, size_t>& g_u_id , mongols::tcp_server::filter_handler_function& send_to_other_filter){ keepalive = KEEPALIVE_CONNECTION; send_to_other=true; return input; }; server.run(f); server.run(); } */ /* //tcp server or tcp multi-threading server int main(int,char**) { auto f=[](const std::string& input , bool& send_to_other , std::pair<size_t, size_t>& g_u_id , mongols::tcp_server::filter_handler_function& send_to_other_filter){ send_to_other=true; return std::make_pair(input,KEEPALIVE_CONNECTION); }; int port=9090; const char* host="127.0.0.1"; mongols::tcp_threading_server //mongols::tcp_server server(host,port); server.run(f); } */ //http server or multi-threading server int main(int,char**) { auto f=[](const mongols::request&){ return true; }; auto g=[](const mongols::request& req,mongols::response& res){ /*std::unordered_map<std::string, std::string>::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); }
压力测试:
2核4G,centos7
ab -c1000 -n100000 http://127.0.0.1:9090/
RPS: 10000+
ab -kc1000 -n100000 -H'Connection: keep-alive' http://127.0.0.1:9090/
RPS: 50000+
在ubuntu 18.04上RPS更高,keep-alive,高达80000-120000。
github: https://github.com/webcpp/mongols
websocket demo: http://fusheng.hi-nginx.com/
demo source: https://github.com/webcpp/fusheng
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。