|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// @file benchmark_DPLRawPageSequencer.h |
| 13 | +/// @author Matthias Richter |
| 14 | +/// @since 2021-07-21 |
| 15 | +/// @brief Unit test for the DPL raw page sequencer utility |
| 16 | + |
| 17 | +#include <benchmark/benchmark.h> |
| 18 | + |
| 19 | +#include "DPLUtils/DPLRawPageSequencer.h" |
| 20 | +#include "RawPageTestData.h" |
| 21 | +#include <random> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +using namespace o2::framework; |
| 25 | +auto const PAGESIZE = test::PAGESIZE; |
| 26 | + |
| 27 | +auto createData(int nPages) |
| 28 | +{ |
| 29 | + const int nParts = 16; |
| 30 | + std::vector<InputSpec> inputspecs = { |
| 31 | + InputSpec{"tpc", "TPC", "RAWDATA", 0, Lifetime::Timeframe}}; |
| 32 | + |
| 33 | + std::vector<DataHeader> dataheaders; |
| 34 | + dataheaders.emplace_back("RAWDATA", "TPC", 0, nPages * PAGESIZE, 0, nParts); |
| 35 | + |
| 36 | + std::random_device rd; |
| 37 | + std::uniform_int_distribution<> lengthDist(1, nPages); |
| 38 | + auto randlength = [&rd, &lengthDist]() { |
| 39 | + return lengthDist(rd); |
| 40 | + }; |
| 41 | + |
| 42 | + int rdhCount = 0; |
| 43 | + // whenever a new id is created, it is done from the current counter |
| 44 | + // position, so we also have the possibility to calculate the length |
| 45 | + std::vector<uint16_t> fees; |
| 46 | + auto nextlength = randlength(); |
| 47 | + auto createFEEID = [&rdhCount, &fees, &nPages, &randlength, &nextlength]() { |
| 48 | + if (rdhCount % nPages == 0 || rdhCount - fees.back() > nextlength) { |
| 49 | + fees.emplace_back(rdhCount); |
| 50 | + nextlength = randlength(); |
| 51 | + } |
| 52 | + return fees.back(); |
| 53 | + }; |
| 54 | + auto amendRdh = [&rdhCount, createFEEID](test::RAWDataHeader& rdh) { |
| 55 | + rdh.feeId = createFEEID(); |
| 56 | + rdhCount++; |
| 57 | + }; |
| 58 | + |
| 59 | + return test::createData(inputspecs, dataheaders, amendRdh); |
| 60 | +} |
| 61 | + |
| 62 | +static void BM_DPLRawPageSequencerBinary(benchmark::State& state) |
| 63 | +{ |
| 64 | + auto isSameRdh = [](const char* left, const char* right) -> bool { |
| 65 | + if (left == right) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + return reinterpret_cast<test::RAWDataHeader const*>(left)->feeId == reinterpret_cast<test::RAWDataHeader const*>(right)->feeId; |
| 70 | + }; |
| 71 | + std::vector<std::pair<const char*, size_t>> pages; |
| 72 | + auto insertPages = [&pages](const char* ptr, size_t n) -> void { |
| 73 | + pages.emplace_back(ptr, n); |
| 74 | + }; |
| 75 | + auto dataset = createData(state.range(0)); |
| 76 | + for (auto _ : state) { |
| 77 | + DPLRawPageSequencer(dataset.record).binary(isSameRdh, insertPages); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +static void BM_DPLRawPageSequencerForward(benchmark::State& state) |
| 82 | +{ |
| 83 | + auto isSameRdh = [](const char* left, const char* right) -> bool { |
| 84 | + if (left == right) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + return reinterpret_cast<test::RAWDataHeader const*>(left)->feeId == reinterpret_cast<test::RAWDataHeader const*>(right)->feeId; |
| 89 | + }; |
| 90 | + std::vector<std::pair<const char*, size_t>> pages; |
| 91 | + auto insertPages = [&pages](const char* ptr, size_t n) -> void { |
| 92 | + pages.emplace_back(ptr, n); |
| 93 | + }; |
| 94 | + auto dataset = createData(state.range(0)); |
| 95 | + for (auto _ : state) { |
| 96 | + DPLRawPageSequencer(dataset.record).forward(isSameRdh, insertPages); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +BENCHMARK(BM_DPLRawPageSequencerBinary)->Arg(64)->Arg(512)->Arg(1024); |
| 101 | +BENCHMARK(BM_DPLRawPageSequencerForward)->Arg(64)->Arg(512)->Arg(1024); |
| 102 | + |
| 103 | +BENCHMARK_MAIN(); |
0 commit comments