1 #include "hmbdc/app/Base.hpp" 2 #include "hmbdc/numeric/StatHistogram.hpp" 3 #include "hmbdc/time/Rater.hpp" 4 #include "hmbdc/os/Signals.hpp" 12 namespace hmbdc {
namespace app {
namespace utils {
14 namespace pingpong_detail {
29 char padding[1013 - 16];
32 template <
typename Sender>
34 :
Client<Pinger<Sender>, Ball> {
35 Pinger(Sender* sender, uint16_t msgPerSec, uint16_t msgSize,
size_t skipFirst)
36 : rater_(Duration::seconds(1), msgPerSec, 1u)
38 , hist_(Duration::microseconds(0), Duration::microseconds(1000), 50000)
39 , periodicPingCount_(0)
42 , msgPerSec_(msgPerSec)
46 char const* hmbdcName()
const {
50 void messageDispatchingStartedCb(uint16_t threadSerialNumber)
override {
51 cout <<
"Started with the first " << skipped_ <<
" values ignored(x), press ctrl-c to get results" << endl;
54 void handleMessageCb(
Ball const& m) {
55 auto now = SysTime::now();
56 auto lat = now - m.ts;
64 void stoppedCb(exception
const& e)
override {
65 cerr << e.what() << endl;
68 void invokedCb(uint16_t)
override {
69 if (hmbdc_unlikely(rater_.check())) {
70 if (++periodicPingCount_ == msgPerSec_) {
71 cout << (skipped_?
'x':
'.') << flush;
72 periodicPingCount_ = 0;
75 sender_->send(p, msgSize_);
77 if (!skipped_) pingCount_++;
82 cout <<
"\nround trip latency:(" << hist_.sampleSize() <<
'/' << pingCount_ <<
"):";
83 hist_.display(cout, {0, 25, 50, 75, 90, 95, 99, 99.9, 99.99, 100});
90 size_t periodicPingCount_;
98 template <
typename Sender>
100 :
Client<Ponger<Sender>, Ball> {
105 char const* hmbdcName()
const {
109 void messageDispatchingStartedCb(uint16_t)
override {
110 cout <<
"Started, press ctrl-c to stop" << endl;
113 void handleMessageCb(
Ball const& m) {
114 sender_->send(m, m.size);
117 void stoppedCb(exception
const& e)
override {
118 cerr << e.what() << endl;
124 template <
typename NetContext>
126 pingpong(
Config const& config) {
127 uint16_t startCore = config.
getExt<uint16_t>(
"startCore");
128 size_t skipFirst = config.
getExt<
size_t>(
"skipFirst");
129 bool ping = config.
getExt<
bool>(
"ping");
130 auto runTime = config.
getExt<uint32_t>(
"runTime");
133 auto& net = NetContext::instance();
134 auto msgSize = config.
getExt<uint16_t>(
"msgSize");
135 msgSize = min<uint16_t>(msgSize, 512);
136 msgSize = max<uint16_t>(msgSize, 16);
142 Config pingCfg(config,
"ping");
143 Config pongCfg(config,
"pong");
144 cout <<
"initailizing..." << endl;
146 auto sengine = net.createSendTransportEngine(pingCfg, msgSize);
147 auto rengine = net.createRecvTransportEngine(pongCfg, ctx.buffer());
149 , min<uint16_t>(config.
getExt<uint16_t>(
"msgPerSec"), 1000u)
153 net.listenTo(
"pong");
155 uint64_t cpuMask = (1ul << startCore);
157 ctx.start(1, cpuMask,
true);
158 ctx.addToPool(*sengine);
159 ctx.addToPool(*rengine);
161 ctx.start(pinger, cpuMask);
173 pinger.finalReport();
176 auto sengine = net.createSendTransportEngine(pongCfg,
sizeof(
Ball));
177 auto rengine = net.createRecvTransportEngine(pingCfg, ctx.buffer());
180 net.listenTo(
"ping");
182 uint64_t cpuMask = (1ul << startCore);
184 ctx.start(1, cpuMask,
true);
185 ctx.addToPool(*sengine);
186 ctx.addToPool(*rengine);
189 ctx.start(ponger, cpuMask);
206 using pingpong_detail::pingpong;
class to hold an hmbdc configuration
Definition: Config.hpp:44
Definition: Pingpong.hpp:21
T getExt(const path_type ¶m) const
get a value from the config
Definition: Config.hpp:154
Definition: TypedString.hpp:74
Definition: BitMath.hpp:6
each message type has 16 bit tag
Definition: Message.hpp:30
RAII representing the lifespan of the underlying Singleton which also ganrantees the singularity of u...
Definition: GuardedSingleton.hpp:20
static void onTermIntDo(std::function< void()> doThis)
specfy what to do when SIGTERM or SIGINT is received
Definition: Signals.hpp:27
Definition: Pingpong.hpp:99
collect sample values and keep histogram for top percentages
Definition: StatHistogram.hpp:40
A Context is like a media object that facilitates the communications for the Clients that it is holdi...
Definition: Context.hpp:408
Definition: Pingpong.hpp:33
A Client represents a thread of execution/a task. The execution is managed by a Context. a Client object could participate in message dispatching as the receiver of specifed message types.
Definition: Client.hpp:47