forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti_producer_sequencer_tests.cpp
More file actions
205 lines (157 loc) · 6.02 KB
/
Copy pathmulti_producer_sequencer_tests.cpp
File metadata and controls
205 lines (157 loc) · 6.02 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/multi_producer_sequencer.hpp>
#include <cppcoro/config.hpp>
#include <cppcoro/sequence_barrier.hpp>
#include <cppcoro/sequence_traits.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/static_thread_pool.hpp>
#include <thread>
#include <chrono>
#include <ostream>
#include "doctest/cppcoro_doctest.h"
DOCTEST_TEST_SUITE_BEGIN("multi_producer_sequencer");
using namespace cppcoro;
namespace
{
task<> one_at_a_time_producer(
static_thread_pool& tp,
multi_producer_sequencer<std::size_t>& sequencer,
std::uint64_t buffer[],
std::uint64_t iterationCount)
{
if (iterationCount == 0) co_return;
co_await tp.schedule();
const std::size_t bufferSize = sequencer.buffer_size();
const std::size_t mask = bufferSize - 1;
std::uint64_t i = 0;
while (i < iterationCount)
{
auto seq = co_await sequencer.claim_one(tp);
buffer[seq & mask] = ++i;
sequencer.publish(seq);
}
auto finalSeq = co_await sequencer.claim_one(tp);
buffer[finalSeq & mask] = 0;
sequencer.publish(finalSeq);
}
task<> batch_producer(
static_thread_pool& tp,
multi_producer_sequencer<std::size_t>& sequencer,
std::uint64_t buffer[],
std::uint64_t iterationCount,
std::size_t maxBatchSize)
{
const std::size_t bufferSize = sequencer.buffer_size();
std::uint64_t i = 0;
while (i < iterationCount)
{
const std::size_t batchSize = static_cast<std::size_t>(
std::min<std::uint64_t>(maxBatchSize, iterationCount - i));
auto sequences = co_await sequencer.claim_up_to(batchSize, tp);
for (auto seq : sequences)
{
buffer[seq % bufferSize] = ++i;
}
sequencer.publish(sequences);
}
auto finalSeq = co_await sequencer.claim_one(tp);
buffer[finalSeq % bufferSize] = 0;
sequencer.publish(finalSeq);
}
task<std::uint64_t> consumer(
static_thread_pool& tp,
const multi_producer_sequencer<std::size_t>& sequencer,
sequence_barrier<std::size_t>& readBarrier,
const std::uint64_t buffer[],
std::uint32_t producerCount)
{
co_await tp.schedule();
const std::size_t mask = sequencer.buffer_size() - 1;
std::uint64_t sum = 0;
std::uint32_t endCount = 0;
std::size_t nextToRead = 0;
do
{
std::size_t available = co_await sequencer.wait_until_published(nextToRead, nextToRead - 1, tp);
do
{
const auto& value = buffer[nextToRead & mask];
sum += value;
// Zero value is sentinel that indicates the end of one of the streams.
const bool isEndOfStream = value == 0;
endCount += isEndOfStream ? 1 : 0;
} while (nextToRead++ != available);
// Notify that we've finished processing up to 'available'.
readBarrier.publish(available);
} while (endCount < producerCount);
co_return sum;
}
}
DOCTEST_TEST_CASE("two producers (batch) / single consumer")
{
static_thread_pool tp{ 3 };
// Allow time for threads to start up.
using namespace std::chrono_literals;
std::this_thread::sleep_for(1ms);
constexpr std::size_t batchSize = 10;
constexpr std::size_t bufferSize = 16384;
sequence_barrier<std::size_t> readBarrier;
multi_producer_sequencer<std::size_t> sequencer(readBarrier, bufferSize);
constexpr std::uint64_t iterationCount = 1'000'000;
std::uint64_t buffer[bufferSize];
auto startTime = std::chrono::high_resolution_clock::now();
constexpr std::uint32_t producerCount = 2;
auto result = std::get<0>(sync_wait(when_all(
consumer(tp, sequencer, readBarrier, buffer, producerCount),
batch_producer(tp, sequencer, buffer, iterationCount, batchSize),
batch_producer(tp, sequencer, buffer, iterationCount, batchSize))));
auto endTime = std::chrono::high_resolution_clock::now();
auto totalTimeInNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count();
MESSAGE(
"Producers = " << producerCount
<< ", BatchSize = " << batchSize
<< ", MessagesPerProducer = " << iterationCount
<< ", TotalTime = " << totalTimeInNs/1000 << "us"
<< ", TimePerMessage = " << totalTimeInNs/double(iterationCount * producerCount) << "ns"
<< ", MessagesPerSecond = " << 1'000'000'000 * (producerCount * iterationCount) / totalTimeInNs);
constexpr std::uint64_t expectedResult =
producerCount * std::uint64_t(iterationCount) * std::uint64_t(iterationCount + 1) / 2;
CHECK(result == expectedResult);
}
DOCTEST_TEST_CASE("two producers (single) / single consumer")
{
static_thread_pool tp{ 3 };
// Allow time for threads to start up.
using namespace std::chrono_literals;
std::this_thread::sleep_for(1ms);
constexpr std::size_t bufferSize = 16384;
sequence_barrier<std::size_t> readBarrier;
multi_producer_sequencer<std::size_t> sequencer(readBarrier, bufferSize);
constexpr std::uint64_t iterationCount = 1'000'000;
std::uint64_t buffer[bufferSize];
auto startTime = std::chrono::high_resolution_clock::now();
constexpr std::uint32_t producerCount = 2;
auto result = std::get<0>(sync_wait(when_all(
consumer(tp, sequencer, readBarrier, buffer, producerCount),
one_at_a_time_producer(tp, sequencer, buffer, iterationCount),
one_at_a_time_producer(tp, sequencer, buffer, iterationCount))));
auto endTime = std::chrono::high_resolution_clock::now();
auto totalTimeInNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count();
MESSAGE(
"Producers = " << producerCount
<< ", NoBatch"
<< ", MessagesPerProducer = " << iterationCount
<< ", TotalTime = " << totalTimeInNs / 1000 << "us"
<< ", TimePerMessage = " << totalTimeInNs / double(iterationCount * producerCount) << "ns"
<< ", MessagesPerSecond = " << 1'000'000'000 * (producerCount * iterationCount) / totalTimeInNs);
constexpr std::uint64_t expectedResult =
producerCount * std::uint64_t(iterationCount) * std::uint64_t(iterationCount + 1) / 2;
CHECK(result == expectedResult);
}
DOCTEST_TEST_SUITE_END();