提交 9c3e4b67 编写于 作者: xc13262215230's avatar xc13262215230

性能提升:keystring优化std::string

上级 905fd3e1
......@@ -237,7 +237,7 @@ namespace doyou {
IO_DATA_BASE* MakeRecvIoData() // 投递接收任务准备
{
// 分析:接收任务投递和接收任务完成是一个完整过程(一一对应),不能一次性进行多次接收任务准备
if (_isPostRecv || IsClose()) { // 已经投递过
if (_isPostRecv || IsClose() || _sockfd == INVALID_SOCKET) { // 已经投递过 || 客户端已关闭 || 客户端socket无效
return nullptr;
}
......@@ -254,7 +254,7 @@ namespace doyou {
IO_DATA_BASE* MakeSendIoData() // 先投递,再发送
{
if (_isPostSend || IsClose()) {
if (_isPostSend || IsClose() || _sockfd == INVALID_SOCKET) {
return nullptr; // 避免数据发送任务多次投递
}
......
......@@ -3,6 +3,7 @@
#include "Client.hpp"
#include "SplitString.hpp"
#include "KeyString.hpp"
namespace doyou {
namespace io { // 增加两层命令空间,避免在其它项目应用时类名出现冲突
......@@ -439,8 +440,8 @@ namespace doyou {
int _bodyLen = 0; // 请求体数据字节总长度
// 分析:采用char*代替std::string可以减少内部new/delete过程,提高程序执行效率,
// 当然了,程序出问题的风险也增加了。
std::map<std::string, char*> _headerMap;
std::map<std::string, char*> _argsMap;
std::map<KeyString, char*> _headerMap; // std::string被优化为KeyString
std::map<KeyString, char*> _argsMap; // std::string
RequestType _requestType = HttpClient::UNKNOWN;
char* _method = nullptr; // 分析:只有指针变量拷贝,避免使用std::string封装的内部会频繁使用new/delete
char* _url = nullptr;
......
#ifndef _DOYOU_IO_KEY_STRING_HPP_
#define _DOYOU_IO_KEY_STRING_HPP_
#include <cstring>
namespace doyou {
namespace io {
class KeyString
{
private:
char const* _str = nullptr; // 不对字符指针变量指向的字符串内容作任何的修改
public:
//
KeyString(char const* str) // 自定义有参构造函数
{
Set(str);
}
//
void Set(char const* str)
{
_str = str; // 指针变量的值拷贝
}
//
char const* Get() // 遵守AUTO SAR C++ 14规范
{
return _str;
}
friend bool operator < (KeyString const& left, KeyString const& right); // 当前类的友元函数,在友元函数内部可以访问当前类私有属性或方法
}; // KeyString
// 建议:全局函数推荐写成静态函数更安全
static bool operator < (KeyString const& left, KeyString const& right) // 操作符重载 <
{
return strcmp(left._str, right._str) < 0;
}
} // namespace io
} // namespace doyou
#endif // !_DOYOU_IO_KEY_STRING_HPP_
\ No newline at end of file
......@@ -63,8 +63,9 @@ g++ server.cpp -std=c++11 -pthread -o EasyServer
40.web服务器支持http长连接和短连接-keep-alive识别 102+
41.IOCP网络通信模型测试 050+
42.linux系统下面epoll网络模型web服务器功能测试 070+
43.IOCP-Server完善测试2 106+
code lines sum -> 2961+
code lines sum -> 3067+
//===================================================================================================================
[注意:linux编译指令]
......@@ -492,18 +493,25 @@ select网络通信模型和epoll网络通信模型的底层时同步机制实现
87.浏览器作为http客户端,不同的浏览器的具体实现机制是不完全相同的。
88.测试分类:
物理机测试(本机测试),虚拟机测试(跨机测试),外网阿里云测试(跨域测试)
89.采用std::string并高频使用会降低程序性能。采用c风格字符串的程序
执行效率更高。在高频使用std::string位置应该采用c风格字符串进行
优化。低频使用位置可以不做处理。std::string内部封装的new/delete操作。
90.测试分析:ubuntu虚拟机局域网测试;阿里云外网测试。
91.性能优化分析:找到待优化程序的性能瓶颈,针对瓶颈进行有效性能优化,
才会有效果。
92.性能优化点分析:[HttpClient::std::map<std::string, char*> _headerMap;]
高频通信,比如每秒几千条消息,或者数十万条消息的吞吐量时,每一次采用这个std::
string, 内部都会自动执行new/delete过程,这个过程很消耗系统性能,是程序性能的
瓶颈,应该优化为char*,固定一段内存,避免new/delete创建和销毁对象的系统消耗。
93.在visual studio ide工具中,出现编译错误,采用鼠标双击错误鼠标可自动定位
到错误位置。
......
......@@ -14,9 +14,6 @@ public:
// 处理网路消息
virtual void OnNetMsg(Server* pServer, Client* pClient, netmsg_DataHeader* header)
{
// 消息计数(子类显示调用继承自父类的同名方法)
TcpServer::OnNetMsg(pServer, pClient, header);
// 收到消息
HttpClient* pHttpClient = dynamic_cast<HttpClient*>(pClient); // 将父类指针变量强转为子类指针变量
if (nullptr == pHttpClient) {
......@@ -30,6 +27,9 @@ public:
return;
}
// 消息计数(子类显示调用继承自父类的同名方法)
TcpServer::OnNetMsg(pServer, pClient, header);
// 分析:优化方案,采用map映射方式,key作为调用方法token,val为调用方法的函数地址
// 处理消息
if (pHttpClient->UrlCompare("/add")) { // nginx
......
Info [2023-12-6 23:52:13]Log::setLogPath success,<ServerLog.txt,w>
Error [2023-12-6 23:52:13]Config::getStr not find <strIP>
Info [2023-12-6 23:52:13]Config::getStr strIP=any
Error [2023-12-6 23:52:13]Config::getStr not find <nPort>
Info [2023-12-6 23:52:13]Config::getInt nPort=4568
Error [2023-12-6 23:52:13]Config::getStr not find <nThread>
Info [2023-12-6 23:52:13]Config::getInt nThread=1
Error [2023-12-6 23:52:13]Config::getStr not find <nSendBuffSize>
Info [2023-12-6 23:52:13]Config::getInt nSendBuffSize=10240
Error [2023-12-6 23:52:13]Config::getStr not find <nRecvBuffSize>
Info [2023-12-6 23:52:13]Config::getInt nRecvBuffSize=8192
Error [2023-12-6 23:52:13]Config::getStr not find <nMaxClient>
Info [2023-12-6 23:52:13]Config::getInt nMaxClient=65535
Info [2023-12-6 23:52:13]-ipv4
Info [2023-12-6 23:52:13]create socket<460> success...
Error [2023-12-6 23:52:13]Config::getStr not find <wwwroot>
Info [2023-12-6 23:52:13]Config::getStr wwwroot=./www
Error [2023-12-6 23:52:13]Config::getStr not find <indexpage>
Info [2023-12-6 23:52:13]Config::getStr indexpage=index.html
Info [2023-12-6 23:52:13]bind port<4568> success...
Info [2023-12-6 23:52:13]listen port<460> success...
Info [2023-12-6 23:52:14]thread<1>,time<1.000364>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:15]thread<1>,time<1.001280>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:16]thread<1>,time<1.000398>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:17]thread<1>,time<1.000325>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:18]thread<1>,time<1.001384>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:19]thread<1>,time<1.000319>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:20]thread<1>,time<1.001622>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:21]thread<1>,time<1.000395>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:22]thread<1>,time<1.001341>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:23]thread<1>,time<1.000381>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:24]thread<1>,time<1.000349>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:25]thread<1>,time<1.000348>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:26]thread<1>,time<1.000344>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:27]thread<1>,time<1.000345>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:52:28]AcceptEx ipv4_local[127.0.0.1]
Info [2023-12-6 23:52:28]AcceptEx ipv4_remote[127.0.0.1]
Info [2023-12-6 23:52:28]new client[sockfd=516][ip=127.0.0.1] to join.
Info [2023-12-6 23:52:41]Accept New Client : [ip=127.0.0.1] [cSock=516]
Info [2023-12-6 23:52:41]thread<1>,time<14.478189>,socket<460>,Accept<1>,Join<1>,recv<0>,msg<0>
Info [2023-12-6 23:52:41]AcceptEx ipv4_local[127.0.0.1]
Info [2023-12-6 23:52:41]AcceptEx ipv4_remote[127.0.0.1]
Info [2023-12-6 23:52:41]new client[sockfd=520][ip=127.0.0.1] to join.
Info [2023-12-6 23:52:41]Accept New Client : [ip=127.0.0.1] [cSock=520]
Info [2023-12-6 23:52:42]thread<1>,time<1.001325>,socket<460>,Accept<2>,Join<2>,recv<1>,msg<2>
Info [2023-12-6 23:52:43]thread<1>,time<1.000323>,socket<460>,Accept<2>,Join<2>,recv<0>,msg<0>
Info [2023-12-6 23:52:44]thread<1>,time<1.000388>,socket<460>,Accept<2>,Join<2>,recv<0>,msg<0>
Info [2023-12-6 23:52:45]thread<1>,time<1.000339>,socket<460>,Accept<2>,Join<2>,recv<0>,msg<0>
Info [2023-12-6 23:52:46]thread<1>,time<1.001283>,socket<460>,Accept<2>,Join<2>,recv<0>,msg<0>
Info [2023-12-6 23:52:47]thread<1>,time<1.001325>,socket<460>,Accept<2>,Join<2>,recv<0>,msg<0>
Info [2023-12-6 23:52:48]thread<1>,time<1.001325>,socket<460>,Accept<2>,Join<2>,recv<0>,msg<0>
Info [2023-12-6 23:52:49]thread<1>,time<1.000452>,socket<460>,Accept<2>,Join<2>,recv<62>,msg<62>
Info [2023-12-6 23:52:50]thread<1>,time<1.000054>,socket<460>,Accept<2>,Join<2>,recv<294>,msg<294>
Info [2023-12-6 23:52:51]thread<1>,time<1.001323>,socket<460>,Accept<2>,Join<2>,recv<274>,msg<274>
Info [2023-12-6 23:52:52]thread<1>,time<1.001284>,socket<460>,Accept<2>,Join<2>,recv<274>,msg<275>
Info [2023-12-6 23:52:53]thread<1>,time<1.000377>,socket<460>,Accept<2>,Join<2>,recv<282>,msg<282>
Info [2023-12-6 23:52:54]thread<1>,time<1.000288>,socket<460>,Accept<2>,Join<2>,recv<294>,msg<294>
Info [2023-12-6 23:52:55]thread<1>,time<1.000324>,socket<460>,Accept<2>,Join<2>,recv<254>,msg<254>
Info [2023-12-6 23:52:56]thread<1>,time<1.000326>,socket<460>,Accept<2>,Join<2>,recv<258>,msg<259>
Info [2023-12-6 23:52:57]thread<1>,time<1.000286>,socket<460>,Accept<2>,Join<2>,recv<270>,msg<271>
Info [2023-12-6 23:52:58]thread<1>,time<1.001324>,socket<460>,Accept<2>,Join<2>,recv<278>,msg<279>
Info [2023-12-6 23:52:59]thread<1>,time<1.000289>,socket<460>,Accept<2>,Join<2>,recv<270>,msg<271>
Info [2023-12-6 23:53:0]thread<1>,time<1.000322>,socket<460>,Accept<2>,Join<2>,recv<292>,msg<293>
Info [2023-12-6 23:53:1]thread<1>,time<1.001115>,socket<460>,Accept<2>,Join<2>,recv<304>,msg<304>
Info [2023-12-6 23:53:2]thread<1>,time<1.001324>,socket<460>,Accept<2>,Join<2>,recv<254>,msg<255>
Info [2023-12-6 23:53:3]thread<1>,time<1.001321>,socket<460>,Accept<2>,Join<2>,recv<278>,msg<279>
Info [2023-12-6 23:53:4]thread<1>,time<1.000380>,socket<460>,Accept<2>,Join<2>,recv<186>,msg<187>
Info [2023-12-6 23:53:4]RmClient socket[sockClient=520], IO_TYPE::RECV bytesTrans=[0].
Info [2023-12-6 23:53:4]socket[520] OnClose.
Info [2023-12-6 23:53:4]RmClient socket[sockClient=516], IO_TYPE::RECV bytesTrans=[0].
Info [2023-12-6 23:53:4]socket[516] OnClose.
Info [2023-12-6 23:53:5]client<516> leave
Info [2023-12-6 23:53:5]~Client[sId=1 id=1 socket=516]
Info [2023-12-6 23:53:5]Client::Destory[sId=1 id=1 socket=516]
Info [2023-12-6 23:53:5]client<520> leave
Info [2023-12-6 23:53:5]~Client[sId=1 id=2 socket=520]
Info [2023-12-6 23:53:5]Client::Destory[sId=1 id=2 socket=520]
Info [2023-12-6 23:53:5]thread<1>,time<1.001280>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:6]thread<1>,time<1.000325>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:7]thread<1>,time<1.000324>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:8]thread<1>,time<1.000386>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:9]thread<1>,time<1.001383>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:10]thread<1>,time<1.001342>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:11]thread<1>,time<1.001318>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:12]thread<1>,time<1.000535>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:13]thread<1>,time<1.001278>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:14]thread<1>,time<1.001386>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:15]thread<1>,time<1.001322>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:16]thread<1>,time<1.001323>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:17]thread<1>,time<1.001318>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:18]thread<1>,time<1.001277>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:19]thread<1>,time<1.000324>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:20]thread<1>,time<1.001320>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:21]thread<1>,time<1.001373>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:22]thread<1>,time<1.000028>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:23]thread<1>,time<1.001322>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:24]thread<1>,time<1.000339>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:25]thread<1>,time<1.001349>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:26]thread<1>,time<1.001343>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:27]thread<1>,time<1.001383>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:28]thread<1>,time<1.001281>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000371>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000393>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001448>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000931>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000239>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000194>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001438>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000408>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000966>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000265>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000485>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.002405>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001166>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000863>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001472>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000601>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000125>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001514>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000617>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001346>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000880>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.000985>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:52]thread<1>,time<1.001253>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:53]thread<1>,time<1.001359>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:54]thread<1>,time<1.000790>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:55]thread<1>,time<1.000107>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:56]thread<1>,time<1.000923>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:57]thread<1>,time<1.001238>,socket<460>,Accept<0>,Join<0>,recv<0>,msg<0>
Info [2023-12-6 23:53:57]TcpServer.Close begin
Info [2023-12-6 23:53:57]Server1.Close begin
Info [2023-12-6 23:53:57]Server1.OnRun exit
Info [2023-12-6 23:53:57]Server1.Close end
Info [2023-12-6 23:53:57]Server1.~Server exit begin
Info [2023-12-6 23:53:57]Server1.Close begin
Info [2023-12-6 23:53:57]Server1.Close end
Info [2023-12-6 23:53:57]Server1.~Server exit end
Info [2023-12-6 23:53:57]TcpServer.Close end
Info [2023-12-6 23:53:57]exit.
Info [2023-12-6 23:53:57]TcpServer.Close begin
Info [2023-12-6 23:53:57]TcpServer.Close end
......@@ -76,6 +76,7 @@
<ClInclude Include="..\Depends\include\EpollServer.hpp" />
<ClInclude Include="..\Depends\include\FDSet.hpp" />
<ClInclude Include="..\Depends\include\HttpClient.hpp" />
<ClInclude Include="..\Depends\include\KeyString.hpp" />
<ClInclude Include="..\Depends\include\SplitString.hpp" />
<ClInclude Include="..\Depends\include\TcpHttpServer.hpp" />
<ClInclude Include="..\Depends\include\INetEvent.hpp" />
......
......@@ -114,5 +114,8 @@
<ClInclude Include="..\Depends\include\SplitString.hpp">
<Filter>源文件</Filter>
</ClInclude>
<ClInclude Include="..\Depends\include\KeyString.hpp">
<Filter>源文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册