-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9adc06a
commit 1ee17db
Showing
6 changed files
with
137 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/**************************************************************************** | ||
Copyright (c) 2019, 李柯鹏 | ||
All rights reserved. | ||
Author: 李柯鹏 <[email protected]> | ||
****************************************************************************/ | ||
#include <algorithm> | ||
#include <numeric> | ||
#include <vector> | ||
#include <thread> | ||
#include <chrono> | ||
#include <glog/logging.h> | ||
#include "myframe/common.h" | ||
#include "myframe/log.h" | ||
#include "myframe/msg.h" | ||
#include "myframe/actor.h" | ||
#include "myframe/mod_manager.h" | ||
#include "myframe/app.h" | ||
|
||
#include "performance_test_config.h" | ||
|
||
class EchoActorTest : public myframe::Actor { | ||
public: | ||
int Init(const char* param) override { | ||
(void)param; | ||
LOG(INFO) << "init EchoActorTest"; | ||
return 0; | ||
} | ||
|
||
void Proc(const std::shared_ptr<const myframe::Msg>& msg) override { | ||
if (msg->GetData() == "hello") { | ||
auto re = std::make_shared<myframe::Msg>( | ||
"resp:" + std::to_string(seq_++)); | ||
auto mailbox = GetMailbox(); | ||
mailbox->Send(msg->GetSrc(), std::move(re)); | ||
} | ||
} | ||
|
||
private: | ||
int seq_{0}; | ||
}; | ||
|
||
int main() { | ||
auto lib_dir = | ||
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); | ||
auto log_dir = | ||
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); | ||
|
||
myframe::InitLog(log_dir, "app_send_req_test"); | ||
|
||
auto app = std::make_shared<myframe::App>(); | ||
if (false == app->Init(lib_dir, 4)) { | ||
LOG(ERROR) << "Init failed"; | ||
return -1; | ||
} | ||
|
||
// mod manager | ||
auto& mod = app->GetModManager(); | ||
|
||
// 注册echo Actor | ||
mod->RegActor("EchoActorTest", [](const std::string&) { | ||
return std::make_shared<EchoActorTest>(); | ||
}); | ||
auto actor = mod->CreateActorInst("class", "EchoActorTest"); | ||
app->AddActor("1", "", actor); | ||
|
||
// 压力测试SendRequest函数 | ||
std::mutex mtx; | ||
int th_cnt = 5; | ||
int exit_th_cnt = 0; | ||
int send_cnt = 10000; | ||
std::vector<std::thread> th_vec; | ||
for (int i = 0; i < th_cnt; ++i) { | ||
th_vec.push_back(std::thread([&, i](){ | ||
int cnt = send_cnt; | ||
while (cnt--) { | ||
auto msg = std::make_shared<myframe::Msg>("hello"); | ||
msg->SetDst("actor.EchoActorTest.1"); | ||
auto resp = app->SendRequest(std::move(msg)); | ||
LOG(INFO) << "thread " << i << " resp: " << resp->GetData(); | ||
} | ||
std::lock_guard<std::mutex> g(mtx); | ||
LOG(INFO) << "user thread " << i << " exit"; | ||
++exit_th_cnt; | ||
if (exit_th_cnt == th_cnt) { | ||
app->Quit(); | ||
} | ||
})); | ||
} | ||
|
||
app->Exec(); | ||
for (int i = 0; i < th_cnt; ++i) { | ||
if (th_vec[i].joinable()) { | ||
th_vec[i].join(); | ||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ Author: 李柯鹏 <[email protected]> | |
#include <vector> | ||
#include <thread> | ||
#include <chrono> | ||
|
||
#include "myframe/common.h" | ||
#include "myframe/log.h" | ||
#include "myframe/msg.h" | ||
|
@@ -29,16 +28,7 @@ class EchoActorTest : public myframe::Actor { | |
|
||
void Proc(const std::shared_ptr<const myframe::Msg>& msg) override { | ||
LOG(INFO) << "recv " << msg->GetSrc() << ":" << msg->GetData(); | ||
if (msg->GetData() == "hello") { | ||
auto re = std::make_shared<myframe::Msg>( | ||
"resp:" + std::to_string(seq_++)); | ||
auto mailbox = GetMailbox(); | ||
mailbox->Send(msg->GetSrc(), re); | ||
} | ||
} | ||
|
||
private: | ||
int seq_{0}; | ||
}; | ||
|
||
int main() { | ||
|
@@ -65,23 +55,36 @@ int main() { | |
auto actor = mod->CreateActorInst("class", "EchoActorTest"); | ||
app->AddActor("1", "", actor); | ||
|
||
// 创建一个线程请求服务 | ||
std::thread th([&]() { | ||
int cnt = 100; | ||
while (cnt--) { | ||
auto msg = std::make_shared<myframe::Msg>("hello"); | ||
msg->SetDst("actor.EchoActorTest.1"); | ||
auto resp = app->SendRequest(msg); | ||
LOG(INFO) << "get resp: " << resp->GetData(); | ||
auto msg2 = std::make_shared<myframe::Msg>("world"); | ||
msg2->SetDst("actor.EchoActorTest.1"); | ||
app->Send(msg2); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
} | ||
app->Quit(); | ||
}); | ||
// 测试Send函数 | ||
std::mutex mtx; | ||
int th_cnt = 5; | ||
int exit_th_cnt = 0; | ||
int send_cnt = 10000; | ||
std::vector<std::thread> th_vec; | ||
for (int i = 0; i < th_cnt; ++i) { | ||
th_vec.push_back(std::thread([&, i](){ | ||
int cnt = send_cnt; | ||
while (cnt--) { | ||
auto msg = std::make_shared<myframe::Msg>( | ||
"world " + std::to_string(i)); | ||
msg->SetDst("actor.EchoActorTest.1"); | ||
app->Send(std::move(msg)); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
} | ||
std::lock_guard<std::mutex> g(mtx); | ||
LOG(INFO) << "user thread " << i << " exit"; | ||
++exit_th_cnt; | ||
if (exit_th_cnt == th_cnt) { | ||
app->Quit(); | ||
} | ||
})); | ||
} | ||
|
||
app->Exec(); | ||
th.join(); | ||
for (int i = 0; i < th_cnt; ++i) { | ||
if (th_vec[i].joinable()) { | ||
th_vec[i].join(); | ||
} | ||
} | ||
return 0; | ||
} |