-
Notifications
You must be signed in to change notification settings - Fork 184
add WaitStrategy #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add WaitStrategy #44
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| #include <chrono> | ||
| #include <iomanip> | ||
| #include <mutex> | ||
| #include <vector> | ||
|
|
||
| #include "workspace/workspace.hpp" | ||
| #include "timewait.h" | ||
|
|
||
| int main(int argn, char** argvs) { | ||
| int task_nums, thread_nums; | ||
| if (argn == 3) { | ||
| thread_nums = atoi(argvs[1]); | ||
| task_nums = atoi(argvs[2]); | ||
| } else { | ||
| fprintf(stderr, "Invalid parameter! usage: [threads + tasks]\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| for (auto strategy : {wsp::WaitStrategy::LowLatencyMode, wsp::WaitStrategy::BalancedMode, wsp::WaitStrategy::SleepMode}) { | ||
| wsp::workbranch wb(thread_nums, strategy); | ||
| std::vector<long long> latencies; | ||
| latencies.reserve(task_nums); | ||
| std::mutex latency_mutex; | ||
|
|
||
| auto task = [&latencies, &latency_mutex](std::chrono::steady_clock::time_point submit_time) { | ||
| auto start_time = std::chrono::steady_clock::now(); | ||
| auto latency = std::chrono::duration_cast<std::chrono::microseconds>(start_time - submit_time).count(); | ||
| { | ||
| std::lock_guard<std::mutex> lock(latency_mutex); | ||
| latencies.push_back(latency); | ||
| } | ||
| }; | ||
|
|
||
| // record submit_time | ||
| for (int i = 0; i < task_nums; ++i) { | ||
| auto submit_time = std::chrono::steady_clock::now(); | ||
| wb.submit([=]() { task(submit_time); }); | ||
|
||
| } | ||
|
|
||
| wb.wait_tasks(); | ||
|
|
||
| // calculate latency | ||
| long long total_latency = 0; | ||
| long long max_latency = 0; | ||
| long long min_latency = latencies.empty() ? 0 : latencies[0]; | ||
|
|
||
| for (auto latency : latencies) { | ||
| total_latency += latency; | ||
| if (latency > max_latency) { | ||
| max_latency = latency; | ||
| } | ||
| if (latency < min_latency) { | ||
| min_latency = latency; | ||
| } | ||
| } | ||
|
|
||
| double avg_latency = latencies.empty() ? 0.0 : static_cast<double>(total_latency) / latencies.size(); | ||
|
|
||
| const char* strategy_name = ""; | ||
| switch (strategy) { | ||
| case wsp::WaitStrategy::LowLatencyMode: | ||
| strategy_name = "LowLatencyMode"; | ||
| break; | ||
| case wsp::WaitStrategy::BalancedMode: | ||
| strategy_name = "BalancedMode"; | ||
| break; | ||
| case wsp::WaitStrategy::SleepMode: | ||
| strategy_name = "SleepMode"; | ||
| break; | ||
| } | ||
|
|
||
| std::cout << "Strategy: " << std::left << std::setw(15) << strategy_name | ||
| << " | Threads: " << std::setw(2) << thread_nums << " | Tasks: " << std::setw(8) << task_nums | ||
| << " | Avg Latency: " << std::setw(8) << std::right << std::fixed << std::setprecision(2) | ||
| << avg_latency << " us" | ||
| << " | Min Latency: " << std::setw(4) << min_latency << " us" | ||
| << " | Max Latency: " << std::setw(8) << max_latency << " us" << std::endl; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在轻量的框架里面,获取高精度时间这个调用是相当重的,这样测试反映的耗时都在这个调用上而不是框架本身。只能是全部跑完统计。
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hanya@localhost:build$ ./bench4 4 1000000
Strategy: lowlatancy | Threads: 4 | Tasks: 1000000 | Avg Latency: 5.19 us | Min Latency: 0 us | Max Latency: 1027 us
Strategy: balance | Threads: 4 | Tasks: 1000000 | Avg Latency: 6.87 us | Min Latency: 0 us | Max Latency: 1256 us
Strategy: blocking | Threads: 4 | Tasks: 1000000 | Avg Latency: 6.14 us | Min Latency: 0 us | Max Latency: 145 us
hanya@localhost:build$ ./bench4 1 1000000
Strategy: lowlatancy | Threads: 1 | Tasks: 1000000 | Avg Latency: 56448.22 us | Min Latency: 28 us | Max Latency: 113657 us
Strategy: balance | Threads: 1 | Tasks: 1000000 | Avg Latency: 34161.14 us | Min Latency: 1 us | Max Latency: 71510 us
Strategy: blocking | Threads: 1 | Tasks: 1000000 | Avg Latency: 83461.47 us | Min Latency: 59 us | Max Latency: 152813 us
单个worker的总耗时是更低的,但是这里平均延迟高出了几个数量级。