Skip to content

Commit a621929

Browse files
echofacegonghuan
andauthored
ltio: update bench code and pin to a fixed branch (#6756)
* [ltio] a coroutine based c++ netlib * add benchmark source code * json api encode response every time * update bench branch to fcontex * fix server name error Co-authored-by: gonghuan <[email protected]>
1 parent 1613b1a commit a621929

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

frameworks/C++/ltio/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ http://localhost:5006/json
2020
### PLAINTEXT
2121

2222
http://localhost:5006/plaintext
23-

frameworks/C++/ltio/http_benchmark.cc

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
#include <vector>
21
#include <csignal>
2+
#include <vector>
33

44
#include "base/utils/string/str_utils.h"
55
#include "fmt/chrono.h"
6+
#include "fmt/format.h"
67
#include "gflags/gflags.h"
7-
#include "nlohmann/json.hpp"
88
#include "net_io/server/http_server/http_server.h"
9-
9+
#include "nlohmann/json.hpp"
1010

1111
using namespace lt::net;
1212
using namespace lt;
1313
using namespace base;
14+
using namespace co;
1415

1516
DEFINE_int32(loops, 4, "how many loops use for handle message and io");
16-
DEFINE_string(http, "0.0.0.0:5006", "host:port used for http service listen on");
17+
DEFINE_bool(echo, false, "just echo response without any logic");
18+
DEFINE_bool(coro, false, "using coro context handle request");
19+
DEFINE_string(http,
20+
"0.0.0.0:5006",
21+
"host:port used for http service listen on");
1722

18-
class HttpBenchMarkServer {
23+
class HttpBenchServer {
1924
public:
20-
~HttpBenchMarkServer() {
25+
~HttpBenchServer() {
2126
LOG(INFO) << __func__ << " close app";
2227
for (auto loop : loops) {
2328
delete loop;
@@ -26,43 +31,47 @@ class HttpBenchMarkServer {
2631
}
2732

2833
void Run() {
29-
main_loop.Start();
30-
34+
json_message["message"] = "Hello, World!";
3135

32-
int loop_count = std::max(FLAGS_loops, int(std::thread::hardware_concurrency()));
36+
int loop_count =
37+
std::max(FLAGS_loops, int(std::thread::hardware_concurrency()));
3338
LOG(INFO) << __func__ << " use loop count:" << loop_count;
3439

3540
for (int i = 0; i < loop_count; i++) {
36-
auto loop = new(base::MessageLoop);
37-
loop->Start();
38-
loops.push_back(loop);
41+
loops.push_back(new base::MessageLoop(fmt::format("io_{}", i)));
42+
loops.back()->Start();
43+
CoroRunner::RegisteRunner(loops.back());
3944
}
4045

41-
http_server.WithIOLoops(loops)
42-
.WithAddress(base::StrUtil::Concat("http://", FLAGS_http))
43-
.ServeAddress([this](const RefHttpRequestCtx& context) {
44-
VLOG(GLOG_VTRACE) << " got Http request from benchmark api";
45-
46-
const HttpRequest* req = context->Request();
47-
//TODO: response freelist
48-
auto response = HttpResponse::CreateWithCode(200);
49-
response->SetKeepAlive(req->IsKeepAlive());
50-
46+
auto func = [this](const RefHttpRequestCtx& context) {
47+
const HttpRequest* req = context->Request();
48+
// TODO: response freelist
49+
auto response = HttpResponse::CreateWithCode(200);
50+
response->SetKeepAlive(req->IsKeepAlive());
51+
if (FLAGS_echo) {
52+
response->MutableBody() = "echo";
53+
} else {
5154
response->InsertHeader("Server", "ltio");
5255
auto tm = fmt::gmtime(std::time(nullptr));
53-
response->InsertHeader("Date", fmt::format("{:%a, %d %b %Y %H:%M:%S %Z}", tm));
56+
response->InsertHeader("Date",
57+
fmt::format("{:%a, %d %b %Y %H:%M:%S %Z}", tm));
5458
if (req->RequestUrl() == "/plaintext") {
5559
response->MutableBody() = "Hello, World!";
5660
} else if (req->RequestUrl() == "/json") {
57-
nlohmann::json json_message;
58-
json_message["message"] = "Hello, World!";
59-
response->MutableBody() = std::move(json_message.dump());
6061
response->InsertHeader("Content-Type", "application/json");
62+
response->MutableBody() = std::move(json_message.dump());
6163
}
62-
return context->Response(response);
63-
});
64+
}
65+
return context->Response(response);
66+
};
6467

65-
main_loop.WaitLoopEnd();
68+
handler.reset(FLAGS_coro ? NewHttpCoroHandler(func) : NewHttpHandler(func));
69+
70+
// ProfilerStart("perf.out");
71+
http_server.WithIOLoops(loops)
72+
.WithAddress(base::StrUtil::Concat("http://", FLAGS_http))
73+
.ServeAddress(handler.get());
74+
loops.back()->WaitLoopEnd();
6675
}
6776

6877
void Stop() {
@@ -71,32 +80,32 @@ class HttpBenchMarkServer {
7180
http_server.StopServer(CO_RESUMER);
7281
CO_YIELD;
7382
LOG(INFO) << __FUNCTION__ << " stop leave";
74-
main_loop.QuitLoop();
83+
loops.back()->QuitLoop();
7584
}
7685

7786
HttpServer http_server;
78-
//HttpCoroServer http_server;
87+
std::unique_ptr<CodecService::Handler> handler;
7988
base::MessageLoop main_loop;
89+
nlohmann::json json_message;
8090
std::vector<base::MessageLoop*> loops;
8191
};
8292

83-
HttpBenchMarkServer app;
93+
HttpBenchServer app;
8494

85-
void signalHandler( int signum ){
95+
void signalHandler(int signum) {
8696
LOG(INFO) << "sighandler sig:" << signum;
87-
CO_GO std::bind(&HttpBenchMarkServer::Stop, &app);
97+
CO_GO std::bind(&HttpBenchServer::Stop, &app);
8898
}
8999

90100
int main(int argc, char* argv[]) {
91101
gflags::ParseCommandLineFlags(&argc, &argv, true);
92102
gflags::SetUsageMessage("usage: exec --http=ip:port ");
93103

94-
//google::InitGoogleLogging(argv[0]);
95-
//google::SetVLOGLevel(NULL, 26);
104+
// google::InitGoogleLogging(argv[0]);
105+
// google::SetVLOGLevel(NULL, 26);
96106

97107
signal(SIGINT, signalHandler);
98108
signal(SIGTERM, signalHandler);
99109

100110
app.Run();
101111
}
102-

frameworks/C++/ltio/ltio.dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM buildpack-deps:focal
22

33
RUN apt-get update -yqq && apt-get install -yqq software-properties-common unzip cmake
4-
RUN apt-get install -yqq build-essential libgoogle-perftools-dev git-core
4+
RUN apt-get install -yqq build-essential libgoogle-perftools-dev git-core libssl-dev zlib1g-dev
55

66
EXPOSE 5006
77
RUN mkdir build_ltio
@@ -10,7 +10,7 @@ ADD CMakeLists.txt build_ltio/
1010
ADD http_benchmark.cc build_ltio/
1111

1212
WORKDIR /build_ltio
13-
RUN pwd; git clone https://github.com/echoface/ltio.git --recurse
13+
RUN pwd; git clone -b benchmark https://github.com/echoface/ltio.git --recurse
1414

1515
RUN ls; cmake .; make
1616

0 commit comments

Comments
 (0)