1
- #include < vector>
2
1
#include < csignal>
2
+ #include < vector>
3
3
4
4
#include " base/utils/string/str_utils.h"
5
5
#include " fmt/chrono.h"
6
+ #include " fmt/format.h"
6
7
#include " gflags/gflags.h"
7
- #include " nlohmann/json.hpp"
8
8
#include " net_io/server/http_server/http_server.h"
9
-
9
+ # include " nlohmann/json.hpp "
10
10
11
11
using namespace lt ::net;
12
12
using namespace lt ;
13
13
using namespace base ;
14
+ using namespace co ;
14
15
15
16
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" );
17
22
18
- class HttpBenchMarkServer {
23
+ class HttpBenchServer {
19
24
public:
20
- ~HttpBenchMarkServer () {
25
+ ~HttpBenchServer () {
21
26
LOG (INFO) << __func__ << " close app" ;
22
27
for (auto loop : loops) {
23
28
delete loop;
@@ -26,43 +31,47 @@ class HttpBenchMarkServer {
26
31
}
27
32
28
33
void Run () {
29
- main_loop.Start ();
30
-
34
+ json_message[" message" ] = " Hello, World!" ;
31
35
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 ()));
33
38
LOG (INFO) << __func__ << " use loop count:" << loop_count;
34
39
35
40
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 () );
39
44
}
40
45
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 {
51
54
response->InsertHeader (" Server" , " ltio" );
52
55
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));
54
58
if (req->RequestUrl () == " /plaintext" ) {
55
59
response->MutableBody () = " Hello, World!" ;
56
60
} else if (req->RequestUrl () == " /json" ) {
57
- nlohmann::json json_message;
58
- json_message[" message" ] = " Hello, World!" ;
59
- response->MutableBody () = std::move (json_message.dump ());
60
61
response->InsertHeader (" Content-Type" , " application/json" );
62
+ response->MutableBody () = std::move (json_message.dump ());
61
63
}
62
- return context->Response (response);
63
- });
64
+ }
65
+ return context->Response (response);
66
+ };
64
67
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 ();
66
75
}
67
76
68
77
void Stop () {
@@ -71,32 +80,32 @@ class HttpBenchMarkServer {
71
80
http_server.StopServer (CO_RESUMER);
72
81
CO_YIELD;
73
82
LOG (INFO) << __FUNCTION__ << " stop leave" ;
74
- main_loop. QuitLoop ();
83
+ loops. back ()-> QuitLoop ();
75
84
}
76
85
77
86
HttpServer http_server;
78
- // HttpCoroServer http_server ;
87
+ std::unique_ptr<CodecService::Handler> handler ;
79
88
base::MessageLoop main_loop;
89
+ nlohmann::json json_message;
80
90
std::vector<base::MessageLoop*> loops;
81
91
};
82
92
83
- HttpBenchMarkServer app;
93
+ HttpBenchServer app;
84
94
85
- void signalHandler ( int signum ) {
95
+ void signalHandler (int signum) {
86
96
LOG (INFO) << " sighandler sig:" << signum;
87
- CO_GO std::bind (&HttpBenchMarkServer ::Stop, &app);
97
+ CO_GO std::bind (&HttpBenchServer ::Stop, &app);
88
98
}
89
99
90
100
int main (int argc, char * argv[]) {
91
101
gflags::ParseCommandLineFlags (&argc, &argv, true );
92
102
gflags::SetUsageMessage (" usage: exec --http=ip:port " );
93
103
94
- // google::InitGoogleLogging(argv[0]);
95
- // google::SetVLOGLevel(NULL, 26);
104
+ // google::InitGoogleLogging(argv[0]);
105
+ // google::SetVLOGLevel(NULL, 26);
96
106
97
107
signal (SIGINT, signalHandler);
98
108
signal (SIGTERM, signalHandler);
99
109
100
110
app.Run ();
101
111
}
102
-
0 commit comments