forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeToOneSequencedThroughputTest.cpp
More file actions
87 lines (70 loc) · 3.04 KB
/
Copy pathThreeToOneSequencedThroughputTest.cpp
File metadata and controls
87 lines (70 loc) · 3.04 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
#include "stdafx.h"
#include "ThreeToOneSequencedThroughputTest.h"
#include "Disruptor.TestTools/ScopeExitFunctor.h"
namespace Disruptor
{
namespace PerfTests
{
ThreeToOneSequencedThroughputTest::ThreeToOneSequencedThroughputTest()
{
m_sequenceBarrier = m_ringBuffer->newBarrier();
m_batchEventProcessor = std::make_shared< BatchEventProcessor< ValueEvent > >(m_ringBuffer, m_sequenceBarrier, m_handler);
for (auto i = 0; i < m_numPublishers; ++i)
{
m_valuePublishers[i] = [this](const std::shared_ptr< Tests::CountdownEvent >& countdownEvent,
const std::shared_ptr< RingBuffer< ValueEvent > >& ringBuffer,
std::int64_t iterations)
{
this->valuePublisher(countdownEvent, ringBuffer, iterations);
};
}
m_ringBuffer->addGatingSequences({ m_batchEventProcessor->sequence() });
}
std::int64_t ThreeToOneSequencedThroughputTest::run(Stopwatch& stopwatch)
{
m_scheduler->start(5);
TestTools::ScopeExitFunctor atScopeExit([this] { m_scheduler->stop(); });
m_cyclicBarrier->reset();
auto latch = std::make_shared< Tests::ManualResetEvent >(false);
m_handler->reset(latch, m_batchEventProcessor->sequence()->value() + ((m_iterations / m_numPublishers) * m_numPublishers));
std::vector< std::future< void > > futures(m_numPublishers);
for (auto i = 0; i < m_numPublishers; ++i)
{
auto index = i;
futures[i] = m_scheduler->scheduleAndStart(std::packaged_task< void() >([this, index] { m_valuePublishers[index](m_cyclicBarrier, m_ringBuffer, m_iterations); }));
}
auto processorTask = m_scheduler->scheduleAndStart(std::packaged_task< void() >([this] { m_batchEventProcessor->run(); }));
stopwatch.start();
m_cyclicBarrier->signal();
m_cyclicBarrier->wait();
for (auto i = 0; i < m_numPublishers; ++i)
{
futures[i].wait();
}
latch->waitOne();
stopwatch.stop();
m_batchEventProcessor->halt();
processorTask.wait_for(std::chrono::milliseconds(2000));
return m_iterations;
}
std::int32_t ThreeToOneSequencedThroughputTest::requiredProcessorCount() const
{
return 4;
}
void ThreeToOneSequencedThroughputTest::valuePublisher(const std::shared_ptr< Tests::CountdownEvent >& countdownEvent,
const std::shared_ptr< RingBuffer< ValueEvent > >& ringBuffer,
std::int64_t iterations)
{
auto& rb = *ringBuffer;
countdownEvent->signal();
countdownEvent->wait();
for (std::int64_t i = 0; i < iterations; ++i)
{
auto sequence = rb.next();
auto& eventData = rb[sequence];
eventData.value = i;
rb.publish(sequence);
}
}
} // namespace PerfTests
} // namespace Disruptor