|
|
4 gadi atpakaļ | |
|---|---|---|
| .github | 4 gadi atpakaļ | |
| base | 4 gadi atpakaļ | |
| cert | 6 gadi atpakaļ | |
| cmake | 4 gadi atpakaļ | |
| cpputil | 4 gadi atpakaļ | |
| docs | 4 gadi atpakaļ | |
| echo-servers | 4 gadi atpakaļ | |
| etc | 4 gadi atpakaļ | |
| event | 4 gadi atpakaļ | |
| evpp | 4 gadi atpakaļ | |
| examples | 4 gadi atpakaļ | |
| html | 4 gadi atpakaļ | |
| http | 4 gadi atpakaļ | |
| misc | 6 gadi atpakaļ | |
| protocol | 4 gadi atpakaļ | |
| scripts | 4 gadi atpakaļ | |
| unittest | 4 gadi atpakaļ | |
| util | 4 gadi atpakaļ | |
| .clang-format | 4 gadi atpakaļ | |
| .gitattributes | 4 gadi atpakaļ | |
| .gitignore | 4 gadi atpakaļ | |
| .travis.yml | 4 gadi atpakaļ | |
| BUILD.md | 4 gadi atpakaļ | |
| CMakeLists.txt | 4 gadi atpakaļ | |
| LICENSE | 6 gadi atpakaļ | |
| Makefile | 4 gadi atpakaļ | |
| Makefile.in | 4 gadi atpakaļ | |
| Makefile.vars | 4 gadi atpakaļ | |
| README-CN.md | 4 gadi atpakaļ | |
| README.md | 4 gadi atpakaļ | |
| TREE.md | 4 gadi atpakaļ | |
| config.ini | 4 gadi atpakaļ | |
| config.mk | 4 gadi atpakaļ | |
| configure | 4 gadi atpakaļ | |
| getting_started.sh | 4 gadi atpakaļ | |
| hconfig.h | 4 gadi atpakaļ | |
| hconfig.h.in | 4 gadi atpakaļ | |
| hexport.h | 4 gadi atpakaļ | |
| hv.h | 5 gadi atpakaļ |
English | 中文
libhv是一个类似于libevent、libev、libuv的跨平台网络库,提供了更简单的接口和更丰富的协议。
libhv提供了以下构建方式:
1、通过Makefile:
./configure
make
sudo make install
2、通过cmake:
mkdir build
cd build
cmake ..
cmake --build .
3、通过vcpkg:
vcpkg install libhv
4、通过xmake:
xrepo install libhv
运行脚本./getting_started.sh:
# 下载编译
git clone https://github.com/ithewei/libhv.git
cd libhv
make
# 运行httpd服务
bin/httpd -h
bin/httpd -d
#bin/httpd -c etc/httpd.conf -s restart -d
ps aux | grep httpd
# 文件服务
bin/curl -v localhost:8080
# 目录服务
bin/curl -v localhost:8080/downloads/
# API服务
bin/curl -v localhost:8080/ping
bin/curl -v localhost:8080/echo -d "hello,world!"
bin/curl -v localhost:8080/query?page_no=1\&page_size=10
bin/curl -v localhost:8080/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
bin/curl -v localhost:8080/upload -F "file=@LICENSE"
bin/curl -v localhost:8080/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
bin/curl -v localhost:8080/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
bin/curl -v localhost:8080/test -F 'bool=1 int=123 float=3.14 string=hello'
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/group/test/user/123
见examples/http_server_test.cpp
golang gin style
#include "HttpServer.h"
int main() {
HttpService router;
router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
return resp->String("pong");
});
router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
static char data[] = "0123456789";
return resp->Data(data, 10);
});
router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
return resp->Json(router.Paths());
});
router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
resp->json["origin"] = req->client_addr.ip;
resp->json["url"] = req->url;
resp->json["args"] = req->query_params;
resp->json["headers"] = req->headers;
return 200;
});
router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
resp->content_type = req->content_type;
resp->body = req->body;
return 200;
});
http_server_t server;
server.port = 8080;
server.service = &router;
http_server_run(&server);
return 0;
}
见examples/http_client_test.cpp
python requests style
#include "requests.h"
int main() {
auto resp = requests::get("http://www.example.com");
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%s\n", resp->body.c_str());
}
resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%s\n", resp->body.c_str());
}
return 0;
}
js axios style
#include "axios.h"
int main() {
const char* strReq = R"({
"method": "POST",
"url": "http://127.0.0.1:8080/echo",
"params": {
"page_no": "1",
"page_size": "10"
},
"headers": {
"Content-Type": "application/json"
},
"body": {
"app_id": "123456",
"app_secret": "abcdefg"
}
})";
// sync
auto resp = axios::axios(strReq);
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%s\n", resp->body.c_str());
}
// async
int finished = 0;
axios::axios(strReq, [&finished](const HttpResponsePtr& resp) {
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%s\n", resp->body.c_str());
}
finished = 1;
});
// wait async finished
while (!finished) hv_sleep(1);
return 0;
}
# sudo apt install wrk
wrk -c 100 -t 4 -d 10s http://127.0.0.1:8080/
# sudo apt install apache2-utils
ab -c 100 -n 100000 http://127.0.0.1:8080/
libhv(port:8080) vs nginx(port:80)

cd echo-servers
./build.sh
./benchmark.sh
吞吐量:
libevent running on port 2001
libev running on port 2002
libuv running on port 2003
libhv running on port 2004
asio running on port 2005
poco running on port 2006
==============2001=====================================
[127.0.0.1:2001] 4 threads 1000 connections run 10s
total readcount=1616761 readbytes=1655563264
throughput = 157 MB/s
==============2002=====================================
[127.0.0.1:2002] 4 threads 1000 connections run 10s
total readcount=2153171 readbytes=2204847104
throughput = 210 MB/s
==============2003=====================================
[127.0.0.1:2003] 4 threads 1000 connections run 10s
total readcount=1599727 readbytes=1638120448
throughput = 156 MB/s
==============2004=====================================
[127.0.0.1:2004] 4 threads 1000 connections run 10s
total readcount=2202271 readbytes=2255125504
throughput = 215 MB/s
==============2005=====================================
[127.0.0.1:2005] 4 threads 1000 connections run 10s
total readcount=1354230 readbytes=1386731520
throughput = 132 MB/s
==============2006=====================================
[127.0.0.1:2006] 4 threads 1000 connections run 10s
total readcount=1699652 readbytes=1740443648
throughput = 165 MB/s
739352073,欢迎加群交流如果您在使用libhv,欢迎通过PR将信息提交至此列表,让更多的用户了解libhv的实际使用场景,以建立更好的网络生态。
| 用户 (公司名/项目名/个人联系方式) | 案例 (项目简介/业务场景) |
|---|---|
| 阅面科技 | 猎户AIoT平台设备管理、人脸检测HTTP服务、人脸搜索HTTP服务 |
| socks5-libhv | socks5代理 |
| hvloop | 类似uvloop的python异步IO事件循环 |