1 #include "hmbdc/Copyright.hpp" 3 #include "hmbdc/app/Base.hpp" 4 #include "hmbdc/numeric/Stat.hpp" 5 #include "hmbdc/time/Rater.hpp" 6 #include "hmbdc/os/Signals.hpp" 14 namespace hmbdc {
namespace app {
namespace utils {
16 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 , periodicPingCount_(0)
41 , msgPerSec_(msgPerSec)
45 char const* hmbdcName()
const {
50 cout <<
"Started with the first " << skipped_ <<
" values ignored(x), press ctrl-c to get results" << endl;
53 void handleMessageCb(
Ball const& m) {
54 auto now = SysTime::now();
55 auto lat = now - m.ts;
64 cerr << e.what() << endl;
68 if (hmbdc_unlikely(rater_.check())) {
69 if (++periodicPingCount_ == msgPerSec_) {
70 cout << (skipped_?
'x':
'.') << flush;
71 periodicPingCount_ = 0;
74 sender_->send(p, msgSize_);
76 if (!skipped_) pingCount_++;
81 cout <<
"\nround trip time (sec):(" << stat_.sampleSize() <<
'/' << pingCount_ <<
"):";
89 size_t periodicPingCount_;
97 template <
typename Sender>
99 :
Client<Ponger<Sender>, Ball> {
104 char const* hmbdcName()
const {
109 cout <<
"Started, press ctrl-c to stop" << endl;
112 void handleMessageCb(
Ball const& m) {
113 sender_->send(m, m.size);
117 cerr << e.what() << endl;
123 template <
typename NetContext>
125 pingpong(
Config const& config, vector<uint16_t> cpus) {
126 size_t skipFirst = config.
getExt<
size_t>(
"skipFirst");
127 bool ping = config.
getExt<
bool>(
"ping");
128 auto runTime = config.
getExt<uint32_t>(
"runTime");
131 auto& net = NetContext::instance();
132 auto msgSize = config.
getExt<uint16_t>(
"msgSize");
133 msgSize = min<uint16_t>(msgSize, 512);
134 msgSize = max<uint16_t>(msgSize, 16);
140 Config pingCfg(config,
"ping");
141 Config pongCfg(config,
"pong");
142 cout <<
"initailizing..." << endl;
144 auto sengine = net.createSendTransportEngine(pingCfg, msgSize);
145 auto rengine = net.createRecvTransportEngine(pongCfg, ctx.buffer());
147 , config.
getExt<uint16_t>(
"msgPerSec")
151 net.listenTo(
"pong");
153 ctx.start(*rengine, 1ul << cpus[0]
154 , *sengine, 1ul << cpus[1]
155 , pinger, 1ul << cpus[2]);
167 pinger.finalReport();
169 auto sengine = net.createSendTransportEngine(pongCfg, msgSize);
170 auto rengine = net.createRecvTransportEngine(pingCfg, ctx.buffer());
173 net.listenTo(
"ping");
174 ctx.start(*rengine, 1ul << cpus[0]
175 , *sengine, 1ul << cpus[1]
176 , ponger, 1ul << cpus[2]);
193 using pingpong_detail::pingpong;
T getExt(const path_type ¶m, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:226
void stoppedCb(exception const &e) override
callback called when this Client is taken out of message dispatching
Definition: Pingpong.hpp:63
class to hold an hmbdc configuration
Definition: Config.hpp:46
Definition: Pingpong.hpp:21
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
Definition: BitMath.hpp:6
each message type has 16 bit tag
Definition: Message.hpp:60
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
void messageDispatchingStartedCb(uint16_t threadSerialNumber) override
called before any messages got dispatched - only once
Definition: Pingpong.hpp:49
Definition: Pingpong.hpp:98
void stoppedCb(exception const &e) override
callback called when this Client is taken out of message dispatching
Definition: Pingpong.hpp:116
A Context is like a media object that facilitates the communications for the Clients that it is holdi...
Definition: Context.hpp:477
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:57
void messageDispatchingStartedCb(uint16_t) override
called before any messages got dispatched - only once
Definition: Pingpong.hpp:108
void invokedCb(size_t) override
this callback is called all the time (frequently) - the exact timing is after a batch of messages are...
Definition: Pingpong.hpp:67