|
| 1 | +#include <string> |
| 2 | +#include <zmq.hpp> |
| 3 | +#include <zmq_addon.hpp> |
| 4 | +#include <vector> |
| 5 | +#include <iostream> |
| 6 | +#include <fstream> |
| 7 | +#include <chrono> |
| 8 | + |
| 9 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 10 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 11 | +// All rights not expressly granted are reserved. |
| 12 | +// |
| 13 | +// This software is distributed under the terms of the GNU General Public |
| 14 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 15 | +// |
| 16 | +// In applying this license CERN does not waive the privileges and immunities |
| 17 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 18 | +// or submit itself to any jurisdiction. |
| 19 | + |
| 20 | +#include <thread> |
| 21 | +#include <boost/program_options.hpp> |
| 22 | + |
| 23 | +#include "DetectorsDCS/DataPointIdentifier.h" |
| 24 | +#include "DetectorsDCS/DataPointValue.h" |
| 25 | +#include "DetectorsDCS/DataPointCompositeObject.h" |
| 26 | +#include "DetectorsDCS/DataPointCreator.h" |
| 27 | + |
| 28 | +namespace bpo = boost::program_options; |
| 29 | + |
| 30 | +int main(int argc, char** argv) |
| 31 | +{ |
| 32 | + using DPCOM = o2::dcs::DataPointCompositeObject; |
| 33 | + |
| 34 | + bpo::variables_map vm; |
| 35 | + bpo::options_description opt_general("Usage:\n " + std::string(argv[0])); |
| 36 | + bpo::options_description opt_hidden(""); |
| 37 | + bpo::options_description opt_all; |
| 38 | + bpo::positional_options_description opt_pos; |
| 39 | + |
| 40 | + try { |
| 41 | + auto add_option = opt_general.add_options(); |
| 42 | + add_option("help,h", "Print this help message"); |
| 43 | + add_option("rate,r", bpo::value<float>()->default_value(25.0f), "messages per second"); |
| 44 | + add_option("size,s", bpo::value<int>()->default_value(1024 * 1024), "size per message"); |
| 45 | + add_option("port,p", bpo::value<int>()->default_value(5556), "port to send the file"); |
| 46 | + opt_all.add(opt_general).add(opt_hidden); |
| 47 | + bpo::store(bpo::command_line_parser(argc, argv).options(opt_all).positional(opt_pos).run(), vm); |
| 48 | + |
| 49 | + if (vm.count("help")) { |
| 50 | + std::cout << opt_general << std::endl; |
| 51 | + exit(0); |
| 52 | + } |
| 53 | + bpo::notify(vm); |
| 54 | + } catch (bpo::error& e) { |
| 55 | + std::cerr << "ERROR: " << e.what() << std::endl; |
| 56 | + std::cerr << opt_general << std::endl; |
| 57 | + exit(1); |
| 58 | + } catch (std::exception& e) { |
| 59 | + std::cerr << e.what() << ", application will now exit" << std::endl; |
| 60 | + exit(2); |
| 61 | + } |
| 62 | + |
| 63 | + // init -------------------- |
| 64 | + zmq::context_t context(1); |
| 65 | + zmq::socket_t publisher(context, zmq::socket_type::push); |
| 66 | + |
| 67 | + int pub_port = vm["port"].as<int>(); |
| 68 | + size_t sz = vm["size"].as<int>(); |
| 69 | + |
| 70 | + publisher.bind("tcp://127.0.0.1:" + std::to_string(pub_port)); |
| 71 | + |
| 72 | + std::chrono::duration<float> wait{1. / std::max(0.01f, vm["rate"].as<float>())}; |
| 73 | + // end of init ------------- |
| 74 | + |
| 75 | + auto timerStart = std::chrono::system_clock::now(); |
| 76 | + auto timer0 = std::chrono::system_clock::now(); |
| 77 | + // std::cout << "will wait for " << wait << " s between injections\n" |
| 78 | + |
| 79 | + size_t trial = 0, datasize = 0; |
| 80 | + while (1) { |
| 81 | + std::this_thread::sleep_for(wait); |
| 82 | + |
| 83 | + // send ------------------------- |
| 84 | + std::vector<zmq::message_t> data; |
| 85 | + |
| 86 | + std::vector<o2::dcs::DataPointCompositeObject> myVector; |
| 87 | + auto timeNow = std::chrono::high_resolution_clock::now(); |
| 88 | + auto timeNowMs = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow.time_since_epoch()).count(); // in ms |
| 89 | + auto timeNowS = std::chrono::duration_cast<std::chrono::seconds>(timeNow.time_since_epoch()).count(); // in ms |
| 90 | + |
| 91 | + size_t locSize = 0; |
| 92 | + do { |
| 93 | + myVector.emplace_back(o2::dcs::createDataPointCompositeObject("ADAPOS_LG/TEST_000100", 12345.6789, timeNowS, std::abs(timeNowMs - timeNowS * 1000))); |
| 94 | + locSize += myVector.size() * sizeof(DPCOM); |
| 95 | + } while (locSize < sz); |
| 96 | + |
| 97 | + // fill in filename |
| 98 | + data.push_back(zmq::message_t((void*)myVector.data(), myVector.size() * sizeof(DPCOM))); |
| 99 | + datasize += locSize; |
| 100 | + zmq::send_multipart(publisher, data); |
| 101 | + // end of send ------------------ |
| 102 | + |
| 103 | + trial++; |
| 104 | + auto timerNow = std::chrono::system_clock::now(); |
| 105 | + std::chrono::duration<float, std::ratio<1>> dur0 = timerNow - timer0; |
| 106 | + if (dur0.count() > 1) { |
| 107 | + timer0 = timerNow; |
| 108 | + std::chrono::duration<float, std::ratio<1>> durationStart = timerNow - timerStart; |
| 109 | + std::cout << trial << " messages sent in " << durationStart.count() << " s, total size: " << datasize << " bytes\n"; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments