forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneToOneRawThroughputTest.cpp
More file actions
106 lines (83 loc) · 2.95 KB
/
Copy pathOneToOneRawThroughputTest.cpp
File metadata and controls
106 lines (83 loc) · 2.95 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
#include "stdafx.h"
#include "OneToOneRawThroughputTest.h"
#include "Disruptor.TestTools/ScopeExitFunctor.h"
namespace Disruptor
{
namespace PerfTests
{
OneToOneRawThroughputTest::OneToOneRawThroughputTest()
{
m_myRunnable = std::make_shared< MyRunnable >(m_sequencer);
m_sequencer->addGatingSequences({ m_myRunnable->sequence });
m_taskScheduler = std::make_shared< RoundRobinThreadAffinedTaskScheduler >();
}
std::int64_t OneToOneRawThroughputTest::run(Stopwatch& stopwatch)
{
m_taskScheduler->start(requiredProcessorCount());
TestTools::ScopeExitFunctor atScopeExit([this] { m_taskScheduler->stop(); });
auto latch = std::make_shared< Tests::ManualResetEvent >(false);
const auto expectedCount = m_myRunnable->sequence->value() + m_iterations;
m_myRunnable->reset(latch, expectedCount);
auto consumerTask = m_taskScheduler->scheduleAndStart(std::packaged_task< void() >([this] { m_myRunnable->run(); }));
stopwatch.start();
auto sequencer = m_sequencer;
auto producerTask = m_taskScheduler->scheduleAndStart(std::packaged_task< void() >([this, sequencer, latch]
{
auto& s = *sequencer;
for (std::int64_t i = 0; i < m_iterations; ++i)
{
auto next = s.next();
s.publish(next);
}
latch->waitOne();
}));
producerTask.wait();
stopwatch.stop();
waitForEventProcessorSequence(expectedCount);
consumerTask.wait();
return m_iterations;
}
std::int32_t OneToOneRawThroughputTest::requiredProcessorCount() const
{
return 2;
}
void OneToOneRawThroughputTest::waitForEventProcessorSequence(std::int64_t expectedCount) const
{
while (m_myRunnable->sequence->value() != expectedCount)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
OneToOneRawThroughputTest::MyRunnable::MyRunnable(const std::shared_ptr< ISequencer< boost::any > >& sequencer)
{
m_barrier = sequencer->newBarrier({});
}
void OneToOneRawThroughputTest::MyRunnable::reset(const std::shared_ptr< Tests::ManualResetEvent >& latch, std::int64_t expectedCount)
{
m_latch = latch;
m_expectedCount = expectedCount;
}
void OneToOneRawThroughputTest::MyRunnable::run() const
{
auto expected = m_expectedCount;
auto& b = *m_barrier;
auto& s = *sequence;
try
{
std::int64_t processed;
do
{
processed = b.waitFor(s.value() + 1);
s.setValue(processed);
}
while (processed < expected);
m_latch->set();
s.setValue(processed);
}
catch (std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
}
} // namespace PerfTests
} // namespace Disruptor