-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdistbench_threadpool_test.cc
108 lines (93 loc) · 3.38 KB
/
distbench_threadpool_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "distbench_threadpool.h"
#include <atomic>
#include <string>
#include "absl/base/internal/sysinfo.h"
#include "absl/log/log.h"
#include "absl/synchronization/notification.h"
#include "distbench_thread_support.h"
#include "gtest_utils.h"
namespace {
using distbench::CreateThreadpool;
class OverloadTest : public testing::TestWithParam<std::string> {};
TEST_P(OverloadTest, Overload) {
absl::Notification overload_waiter;
{
auto atp = CreateThreadpool(GetParam(), absl::base_internal::NumCPUs());
ASSERT_TRUE(atp.ok());
int overload_threshhold = absl::base_internal::NumCPUs();
distbench::SetOverloadAbortThreshhold(overload_threshhold);
distbench::SetOverloadAbortCallback([&]() {
LOG(INFO) << "detected overload";
overload_waiter.Notify();
LOG(INFO) << "and notified " << &overload_waiter;
});
for (int i = 0; i < 2 * overload_threshhold; ++i) {
atp.value()->AddTask([&]() {
overload_waiter.WaitForNotificationWithTimeout(absl::Seconds(1));
});
}
}
ASSERT_TRUE(overload_waiter.HasBeenNotified()) << &overload_waiter;
}
INSTANTIATE_TEST_SUITE_P(OverloadTests, OverloadTest,
testing::Values("null", "elastic"));
class ThreadpoolTest : public testing::TestWithParam<std::string> {};
TEST(ThreadpoolTest, BadType) {
auto atp = CreateThreadpool("bad_type", 4);
ASSERT_FALSE(atp.ok());
}
TEST_P(ThreadpoolTest, Constructor) {
auto atp = CreateThreadpool(GetParam(), 4);
ASSERT_TRUE(atp.ok());
}
TEST_P(ThreadpoolTest, PerformSimpleWork) {
const int iterations = 1000;
std::atomic<int> work_counter = 0;
{
auto atp = CreateThreadpool(GetParam(), 4);
ASSERT_TRUE(atp.ok());
for (int i = 0; i < iterations; i++) {
atp.value()->AddTask([&]() { ++work_counter; });
}
} // Complete the work of atp.
ASSERT_EQ(work_counter, iterations);
}
TEST_P(ThreadpoolTest, ParallelAddTest) {
const int iterations = 1000;
std::atomic<int> work_counter = 0;
{
auto atp_work_performer = CreateThreadpool(GetParam(), 4);
ASSERT_TRUE(atp_work_performer.ok());
{
auto atp_work_generator = CreateThreadpool(GetParam(), 4);
ASSERT_TRUE(atp_work_generator.ok());
for (int i = 0; i < iterations; i++) {
atp_work_generator.value()->AddTask([&]() {
atp_work_performer.value()->AddTask([&]() { ++work_counter; });
});
}
} // Complete the work of atp_work_generator.
} // Complete the work of atp_work_performer.
ASSERT_EQ(work_counter, iterations);
}
INSTANTIATE_TEST_SUITE_P(ThreadpoolTests, ThreadpoolTest,
testing::Values("", "null", "simple", "elastic"
#ifdef WITH_MERCURY
,
"mercury"
#endif
));
} // namespace