11#include " ipm/Sender.hpp"
22#include " ipm/ZmqContext.hpp"
3-
3+ # include " logging/Logging.hpp "
44#include " boost/program_options.hpp"
55
66#include < memory>
77#include < chrono>
88#include < cstdlib>
99#include < cerrno>
10+ #include < unistd.h> // sleep
1011
1112int main (int argc, char * argv[]){
12- int npackets=1 ;
13- int packetSize=100 ;
13+ uint32_t npackets=1 ;
14+ size_t packetSize=100 ;
15+ size_t interval = 0 ;
1416 std::string conString=" tcp://127.0.0.1:12345" ;
1517 int nthreads=1 ;
18+ uint32_t id = 0 ;
1619
1720 namespace po = boost::program_options;
1821 po::options_description desc (" Simple test program for ZmqSender" );
1922 desc.add_options ()(
20- " connection,c" , po::value<std::string>(&conString), " Connection to listen on" )(
21- " threads,t" , po::value<int >(&nthreads), " Number of ZMQ threads" )(
22- " packets,p" , po::value<int >(&npackets), " Number of packets to send" )(
23- " packetSize,s" , po::value<int >(&packetSize), " Number of bytes per packet" );
23+ " connection,c" , po::value<std::string>(&conString)->default_value (conString), " Connection to listen on" )(
24+ " threads,t" , po::value<int >(&nthreads)->default_value (nthreads), " Number of ZMQ threads" )(
25+ " packets,p" , po::value<uint32_t >(&npackets)->default_value (npackets), " Number of packets to send" )(
26+ " packetSize,s" , po::value<size_t >(&packetSize)->default_value (packetSize), " Number of bytes per packet" )(
27+ " interval,i" , po::value<size_t >(&interval)->default_value (interval), " Microseconds to sleep between messages" )(
28+ " id" , po::value<uint32_t >(&id)->default_value (id), " Identifier for this Sender" );
2429 try {
2530 po::variables_map vm;
2631 po::store (po::parse_command_line (argc, argv, desc), vm);
@@ -31,25 +36,50 @@ int main(int argc, char* argv[]){
3136 return 0 ;
3237 }
3338
39+ dunedaq::logging::Logging::setup (" ZMQ Test" , " zmq_send" );
3440 if (nthreads > 1 ) {
3541 dunedaq::ipm::ZmqContext::instance ().set_context_threads (nthreads);
3642 }
43+
44+ // Set the minimum packet size to 16 bytes, 8 bytes for sequence number and 8 bytes for current time
45+ if (packetSize < 16 ) {
46+ packetSize = 16 ;
47+ }
3748
3849 std::shared_ptr<dunedaq::ipm::Sender> sender=dunedaq::ipm::make_ipm_sender (" ZmqSender" );
3950 sender->connect_for_sends ({ {" connection_string" , conString} });
4051
41- std::vector message (packetSize,0 );
52+ std::vector<char > message (packetSize,0 );
53+ *reinterpret_cast <uint32_t *>(message.data ()) = id;
4254
4355 auto start=std::chrono::steady_clock::now ();
44- for (int p=0 ; p<npackets;p++) {
45- // Last arg is send timeout
46- sender->send ((void *)message.data (), packetSize, std::chrono::milliseconds (100 ));
56+ for (uint64_t p=0 ; p<npackets;p++) {
57+ TLOG_DEBUG (3 ) << " Preparing Message " << p;
58+ // Last arg is send timeout
59+ int attempt=0 ;
60+ bool success;
61+
62+ *(reinterpret_cast <uint32_t *>(message.data ()) + 1 )= p;
63+ *(reinterpret_cast <uint64_t *>(message.data ()) + 1 ) = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now ().time_since_epoch ()).count ();
64+
65+ TLOG_DEBUG (4 ) << " Sending Message " << p;
66+ do {
67+ success = sender->send ((void *)message.data (), packetSize, std::chrono::milliseconds (2 )," " ,true );
68+ if (success == false && ++attempt == 1 )
69+ TLOG () << " bad omen" ;
70+ } while (success == false );
71+
72+ TLOG_DEBUG (5 ) << " Message sent " << p;
73+ if (interval > 0 ) {
74+ usleep (interval);
75+ }
4776 }
4877
4978 auto elapsed=std::chrono::steady_clock::now ()-start;
5079 auto nano=std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count ();
5180 float bw=((float )packetSize*npackets)/nano;
52- std::cout << " Sent " << packetSize*npackets << " bytes in "
53- << nano << " ns " << bw << " GB/s" << std::endl;
54-
81+ TLOG () << " Sent " << packetSize*npackets << " bytes in "
82+ << nano << " ns " << bw << " GB/s" ;
83+ TLOG () << " Sleep 2 to allow finish???" ;
84+ sleep (2 ); // Sleep for 2
5585}
0 commit comments