Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27a174f

Browse files
committed
🪲 fix LT logic && retest
1 parent c6bfcf2 commit 27a174f

File tree

7 files changed

+97
-31
lines changed

7 files changed

+97
-31
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,27 @@ Demo演示
5757

5858
压力测试
5959
-------------
60-
在关闭日志后,使用Webbench对服务器进行压力测试,在ET和LT模式下均可实现上万的并发连接.
60+
在关闭日志后,使用Webbench对服务器进行压力测试,对listenfd和connfd分别采用ET和LT模式,均可实现上万的并发连接,下面列出的是两者组合后的测试结果.
6161

62-
> * Proactor,LT,84016 QPS
62+
> * Proactor,LT + LT,93251 QPS
6363
64-
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1geotkkw531j30f906tn0f.jpg" height="201"/> </div>
64+
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjqu2hptkj30gz07474n.jpg" height="201"/> </div>
6565

66-
> * Proactor,ET,83419 QPS
66+
> * Proactor,LT + ET,97459 QPS
6767
68-
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1geotkozc4nj30f806p0vu.jpg" height="201"/> </div>
68+
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr1xppdgj30h206zdg6.jpg" height="201"/> </div>
6969

70-
> * Reactor,LT,60218 QPS
70+
> * Proactor,ET + LT,80498 QPS
7171
72-
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1geotkad1v2j30f906rad8.jpg" height="201"/> </div>
72+
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr24vmjtj30gz0720t3.jpg" height="201"/> </div>
7373

74-
> * Reactor,ET,58138 QPS
74+
> * Proactor,ET + ET,92167 QPS
7575
76-
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1geotk12vwxj30fa06t0w3.jpg" height="201"/> </div>
76+
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr2bbxo9j30gz06z0t3.jpg" height="201"/> </div>
77+
78+
> * Reactor,LT + ET,69175 QPS
79+
80+
<div align=center><img src="http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr1humcbj30h207474n.jpg" height="201"/> </div>
7781

7882
> * 并发连接总数:10500
7983
> * 访问服务器时间:5s
@@ -184,9 +188,11 @@ Demo演示
184188
* -l,选择日志写入方式,默认同步写入
185189
* 0,同步写入
186190
* 1,异步写入
187-
* -m,epoll的触发模式,默认使用LT
188-
* 0,表示使用LT
189-
* 1,表示使用ET
191+
* -m,listenfd和connfd的模式组合,默认使用LT + LT
192+
* 0,表示使用LT + LT
193+
* 1,表示使用LT + ET
194+
* 2,表示使用ET + LT
195+
* 3,表示使用ET + ET
190196
* -o,优雅关闭连接,默认不使用
191197
* 0,不使用
192198
* 1,使用
@@ -210,7 +216,7 @@ Demo演示
210216
- [x] 端口9007
211217
- [x] CGI校验,使用连接池
212218
- [x] 异步写入日志
213-
- [x] 使用LT水平触发
219+
- [x] 使用LT + LT组合
214220
- [x] 使用优雅关闭连接
215221
- [x] 数据库连接池内有10条连接
216222
- [x] 线程池内有10条线程
@@ -219,8 +225,9 @@ Demo演示
219225

220226
庖丁解牛
221227
------------
222-
近期版本迭代较快,以下内容多以旧版本(raw_version)代码为蓝本进行详解,重构版本讲解将于下月重制发布,感谢理解.
228+
近期版本迭代较快,以下内容多以旧版本(raw_version)代码为蓝本进行详解.
223229

230+
* [小白视角:一文读懂社长的TinyWebServer](https://huixxi.github.io/2020/06/02/%E5%B0%8F%E7%99%BD%E8%A7%86%E8%A7%92%EF%BC%9A%E4%B8%80%E6%96%87%E8%AF%BB%E6%87%82%E7%A4%BE%E9%95%BF%E7%9A%84TinyWebServer/#more)
224231
* [最新版Web服务器项目详解 - 01 线程同步机制封装类](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==&mid=2649274278&idx=3&sn=5840ff698e3f963c7855d702e842ec47&chksm=83ffbefeb48837e86fed9754986bca6db364a6fe2e2923549a378e8e5dec6e3cf732cdb198e2&scene=0&xtrack=1#rd)
225232
* [最新版Web服务器项目详解 - 02 半同步半反应堆线程池(上)](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==&mid=2649274278&idx=4&sn=caa323faf0c51d882453c0e0c6a62282&chksm=83ffbefeb48837e841a6dbff292217475d9075e91cbe14042ad6e55b87437dcd01e6d9219e7d&scene=0&xtrack=1#rd)
226233
* [最新版Web服务器项目详解 - 03 半同步半反应堆线程池(下)](https://mp.weixin.qq.com/s/PB8vMwi8sB4Jw3WzAKpWOQ)
@@ -240,4 +247,4 @@ Demo演示
240247
------------
241248
Linux高性能服务器编程,游双著.
242249

243-
感谢以下朋友的PR和帮助: [@RownH](https://github.com/RownH)[@mapleFU](https://github.com/mapleFU)[@ZWiley](https://github.com/ZWiley)[@zjuHong](https://github.com/zjuHong)[@mamil](https://github.com/mamil)[@byfate](https://github.com/byfate)[@MaJun827](https://github.com/MaJun827)[@BBLiu-coder](https://github.com/BBLiu-coder)[@smoky96](https://github.com/smoky96)[@yfBong](https://github.com/yfBong)[@liuwuyao](https://github.com/liuwuyao).
250+
感谢以下朋友的PR和帮助: [@RownH](https://github.com/RownH)[@mapleFU](https://github.com/mapleFU)[@ZWiley](https://github.com/ZWiley)[@zjuHong](https://github.com/zjuHong)[@mamil](https://github.com/mamil)[@byfate](https://github.com/byfate)[@MaJun827](https://github.com/MaJun827)[@BBLiu-coder](https://github.com/BBLiu-coder)[@smoky96](https://github.com/smoky96)[@yfBong](https://github.com/yfBong)[@liuwuyao](https://github.com/liuwuyao)[@Huixxi](https://github.com/Huixxi).

config.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ Config::Config(){
1010
//日志写入方式,默认同步
1111
LOGWrite = 0;
1212

13-
//触发模式,默认LT
13+
//触发组合模式,默认listenfd LT + connfd LT
1414
TRIGMode = 0;
1515

16+
//listenfd触发模式,默认LT
17+
LISTENTrigmode = 0;
18+
19+
//connfd触发模式,默认LT
20+
CONNTrigmode = 0;
21+
1622
//优雅关闭链接,默认不使用
1723
OPT_LINGER = 0;
1824

config.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ class Config
2222
//日志写入方式
2323
int LOGWrite;
2424

25-
//触发模式
25+
//触发组合模式
2626
int TRIGMode;
2727

28+
//listenfd触发模式
29+
int LISTENTrigmode;
30+
31+
//connfd触发模式
32+
int CONNTrigmode;
33+
2834
//优雅关闭链接
2935
int OPT_LINGER;
3036

http/http_conn.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,40 @@ bool http_conn::read_once()
237237
return false;
238238
}
239239
int bytes_read = 0;
240-
while (true)
240+
241+
//LT读取数据
242+
if (0 == m_TRIGMode)
241243
{
242244
bytes_read = recv(m_sockfd, m_read_buf + m_read_idx, READ_BUFFER_SIZE - m_read_idx, 0);
243-
if (bytes_read == -1)
245+
m_read_idx += bytes_read;
246+
247+
if (bytes_read <= 0)
244248
{
245-
if (errno == EAGAIN || errno == EWOULDBLOCK)
246-
break;
247249
return false;
248250
}
249-
else if (bytes_read == 0)
251+
252+
return true;
253+
}
254+
//ET读数据
255+
else
256+
{
257+
while (true)
250258
{
251-
return false;
259+
bytes_read = recv(m_sockfd, m_read_buf + m_read_idx, READ_BUFFER_SIZE - m_read_idx, 0);
260+
if (bytes_read == -1)
261+
{
262+
if (errno == EAGAIN || errno == EWOULDBLOCK)
263+
break;
264+
return false;
265+
}
266+
else if (bytes_read == 0)
267+
{
268+
return false;
269+
}
270+
m_read_idx += bytes_read;
252271
}
253-
m_read_idx += bytes_read;
272+
return true;
254273
}
255-
return true;
256274
}
257275

258276
//解析http请求行,获得请求方法,目标url及http版本号

main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int main(int argc, char *argv[])
1818
config.OPT_LINGER, config.TRIGMode, config.sql_num, config.thread_num,
1919
config.close_log, config.actor_model);
2020

21+
2122
//日志
2223
server.log_write();
2324

@@ -27,11 +28,14 @@ int main(int argc, char *argv[])
2728
//线程池
2829
server.thread_pool();
2930

31+
//触发模式
32+
server.trig_mode();
33+
3034
//监听
3135
server.eventListen();
3236

3337
//运行
3438
server.eventLoop();
3539

3640
return 0;
37-
}
41+
}

webserver.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ void WebServer::init(int port, string user, string passWord, string databaseName
4545
m_actormodel = actor_model;
4646
}
4747

48+
void WebServer::trig_mode(){
49+
//LT + LT
50+
if(0 == m_TRIGMode){
51+
m_LISTENTrigmode = 0;
52+
m_CONNTrigmode = 0;
53+
}
54+
//LT + ET
55+
else if(1 == m_TRIGMode){
56+
m_LISTENTrigmode = 0;
57+
m_CONNTrigmode = 1;
58+
}
59+
//ET + LT
60+
else if(2 == m_TRIGMode){
61+
m_LISTENTrigmode = 1;
62+
m_CONNTrigmode = 0;
63+
}
64+
//ET + ET
65+
else if(3 == m_TRIGMode){
66+
m_LISTENTrigmode = 1;
67+
m_CONNTrigmode = 1;
68+
}
69+
}
70+
4871
void WebServer::log_write()
4972
{
5073
if (0 == m_close_log)
@@ -119,13 +142,13 @@ void WebServer::eventListen()
119142
m_epollfd = epoll_create(5);
120143
assert(m_epollfd != -1);
121144

122-
utils.addfd(m_epollfd, m_listenfd, false, m_TRIGMode);
145+
utils.addfd(m_epollfd, m_listenfd, false, m_LISTENTrigmode);
123146
http_conn::m_epollfd = m_epollfd;
124147

125148
ret = socketpair(PF_UNIX, SOCK_STREAM, 0, m_pipefd);
126149
assert(ret != -1);
127150
utils.setnonblocking(m_pipefd[1]);
128-
utils.addfd(m_epollfd, m_pipefd[0], false, m_TRIGMode);
151+
utils.addfd(m_epollfd, m_pipefd[0], false, 0);
129152

130153
utils.addsig(SIGPIPE, SIG_IGN);
131154
utils.addsig(SIGALRM, utils.sig_handler, false);
@@ -136,7 +159,7 @@ void WebServer::eventListen()
136159

137160
void WebServer::timer(int connfd, struct sockaddr_in client_address)
138161
{
139-
users[connfd].init(connfd, client_address, m_root, m_SQLVerify, m_TRIGMode, m_close_log, m_user, m_passWord, m_databaseName);
162+
users[connfd].init(connfd, client_address, m_root, m_SQLVerify, m_CONNTrigmode, m_close_log, m_user, m_passWord, m_databaseName);
140163

141164
//初始化client_data数据
142165
//创建定时器,设置回调函数和超时时间,绑定用户数据,将定时器添加到链表中
@@ -177,7 +200,7 @@ bool WebServer::dealclinetdata()
177200
{
178201
struct sockaddr_in client_address;
179202
socklen_t client_addrlength = sizeof(client_address);
180-
if (0 == m_TRIGMode)
203+
if (0 == m_LISTENTrigmode)
181204
{
182205
int connfd = accept(m_listenfd, (struct sockaddr *)&client_address, &client_addrlength);
183206
if (connfd < 0)

webserver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class WebServer
3232
void thread_pool();
3333
void sql_pool();
3434
void log_write();
35-
35+
void trig_mode();
3636
void eventListen();
3737
void eventLoop();
3838
void timer(int connfd, struct sockaddr_in client_address);
@@ -73,6 +73,8 @@ class WebServer
7373
int m_listenfd;
7474
int m_OPT_LINGER;
7575
int m_TRIGMode;
76+
int m_LISTENTrigmode;
77+
int m_CONNTrigmode;
7678

7779
//定时器相关
7880
sort_timer_lst timer_lst;

0 commit comments

Comments
 (0)