Line data Source code
1 : /**
2 : * @file set_serialization_speed.cxx Test the amount of time it takes to serialize a TASet
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "logging/Logging.hpp"
10 : #include "serialization/Serialization.hpp"
11 : #include "trigger/TASet.hpp"
12 : #include "trigger/TPSet.hpp"
13 : #include "triggeralgs/TriggerPrimitive.hpp"
14 : #include "trgdataformats/Types.hpp"
15 :
16 : #include <chrono>
17 : #include <iostream>
18 : #include <random>
19 : #include <vector>
20 :
21 : // Return the current steady clock in microseconds
22 : inline uint64_t // NOLINT(build/unsigned)
23 0 : now_us()
24 : {
25 0 : using namespace std::chrono;
26 : // std::chrono is the worst
27 0 : return duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count();
28 : }
29 :
30 : void
31 0 : time_serialization(int tps_per_set)
32 : {
33 0 : const int N = 100000;
34 0 : int total = 0;
35 :
36 0 : std::default_random_engine generator;
37 0 : std::uniform_int_distribution<int> uniform(0, 1000);
38 :
39 0 : std::vector<dunedaq::trigger::TPSet> sets;
40 0 : for (int i = 0; i < N; ++i) {
41 0 : dunedaq::trigger::TPSet set;
42 0 : set.seqno = i + 1;
43 0 : set.start_time = (i + 1) * 5000;
44 0 : set.end_time = (i + 2) * 5000 - 1;
45 0 : for (int j = 0; j < tps_per_set; ++j) {
46 0 : triggeralgs::TriggerPrimitive tp;
47 0 : tp.time_start = 1234963454;
48 0 : tp.samples_to_peak = 1000;
49 0 : tp.samples_over_threshold = uniform(generator);
50 0 : tp.channel = uniform(generator);
51 0 : tp.adc_integral = uniform(generator);
52 0 : tp.adc_peak = uniform(generator);
53 0 : tp.detid = 1;
54 0 : tp.flag = 1;
55 :
56 0 : set.objects.push_back(tp);
57 : }
58 0 : sets.push_back(set);
59 0 : }
60 :
61 0 : uint64_t start_time = now_us(); // NOLINT(build/unsigned)
62 :
63 0 : for (int i = 0; i < N; ++i) {
64 : // NOLINTNEXTLINE(build/unsigned)
65 0 : std::vector<uint8_t> bytes = dunedaq::serialization::serialize(sets[i], dunedaq::serialization::kMsgPack);
66 0 : dunedaq::trigger::TPSet set_recv = dunedaq::serialization::deserialize<dunedaq::trigger::TPSet>(bytes);
67 0 : total += set_recv.seqno;
68 0 : }
69 0 : uint64_t end_time = now_us(); // NOLINT(build/unsigned)
70 :
71 0 : double time_taken_s = 1e-6 * (end_time - start_time);
72 0 : double msg_kHz = 1e-3 * N / time_taken_s;
73 0 : double tp_kHz = 1e-3 * tps_per_set * N / time_taken_s;
74 0 : TLOG() << "Sent " << N << " messages in " << time_taken_s << " (" << msg_kHz << " kHz of msgs, " << tp_kHz
75 0 : << " kHz of TPs) " << total;
76 0 : }
77 :
78 : int
79 0 : main()
80 : {
81 0 : std::vector<int> n_tps{ 0, 1, 10, 100, 1000 };
82 0 : for (auto n : n_tps) {
83 0 : TLOG() << n << " TPs per set: " << std::flush;
84 0 : time_serialization(n);
85 : }
86 :
87 0 : dunedaq::trigger::TASet taset;
88 : // NOLINTNEXTLINE(build/unsigned)
89 0 : std::vector<uint8_t> bytes = dunedaq::serialization::serialize(taset, dunedaq::serialization::kMsgPack);
90 0 : dunedaq::trigger::TASet set_recv = dunedaq::serialization::deserialize<dunedaq::trigger::TASet>(bytes);
91 0 : }
|