PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
TimedReader.h
Go to the documentation of this file.
1#ifndef PODIO_TIMEDREADER_H
2#define PODIO_TIMEDREADER_H
3
6
8#include "podio/IReader.h"
9
10#include <map>
11
12namespace podio {
13
14template <class WrappedReader>
15class TimedReader : public IReader {
16 using ClockT = benchmark::ClockT;
17
18public:
19 template <typename... Args>
20 TimedReader(benchmark::BenchmarkRecorder& recorder, Args&&... args) :
21 m_start(ClockT::now()),
22 m_reader(WrappedReader(std::forward<Args>(args)...)),
23 m_end(ClockT::now()),
24 m_recorder(recorder),
25 m_perEventTree(m_recorder.addTree(
26 "event_times",
27 {"read_collections", "read_ev_md", "read_run_md", "read_coll_md", "end_of_event", "read_event"})) {
28 m_recorder.addTree("setup_times", {"constructor", "open_file", "close_file", "read_collection_ids", "get_entries"});
29 m_recorder.recordTime("setup_times", "constructor", m_end - m_start);
30 }
31
33 // Timing deconstructors is not straight forward when wrapping a value.
34 // Since nothing is usually happening in them in any case, we simply don't
35 // do it. We still have to fill the setup_times tree here though.
36 m_recorder.Fill("setup_times");
37 }
38
39 /// Read Collection of given name
40 /// Does not set references yet.
41 CollectionBase* readCollection(const std::string& name) override {
42 const auto [result, duration] = benchmark::run_member_timed(m_reader, &IReader::readCollection, name);
43 // since we cannot in general know how many collections there will be read
44 // we simply sum up all the requests in an event and record that
45 m_totalCollectionReadTime += duration;
46 return result;
47 }
48
49 /// Get CollectionIDTable of read-in data
50 std::shared_ptr<CollectionIDTable> getCollectionIDTable() override {
51 return runTimed(false, "read_collection_ids", &IReader::getCollectionIDTable);
52 }
53
54 /// read event meta data from file
56 return runTimed(true, "read_ev_md", &IReader::readEventMetaData);
57 }
58
59 std::map<int, GenericParameters>* readCollectionMetaData() override {
60 return runTimed(true, "read_coll_md", &IReader::readCollectionMetaData);
61 }
62
63 std::map<int, GenericParameters>* readRunMetaData() override {
64 return runTimed(true, "read_run_md", &IReader::readRunMetaData);
65 }
66
67 /// get the number of events available from this reader
68 unsigned getEntries() const override {
69 return runTimed(false, "get_entries", &IReader::getEntries);
70 }
71
72 /// Prepare the reader to read the next event
73 void endOfEvent() override {
74 runVoidTimed(true, "end_of_event", &IReader::endOfEvent);
75
76 m_perEventTree.recordTime("read_collections", m_totalCollectionReadTime);
77 m_perEventTree.Fill();
78 m_totalCollectionReadTime = std::chrono::nanoseconds{0};
79 }
80
81 // not benchmarking this one
82 bool isValid() const override {
83 return m_reader.isValid();
84 }
85
86 void openFile(const std::string& filename) override {
87 runVoidTimed(false, "open_file", &IReader::openFile, filename);
88 }
89
90 void closeFile() override {
91 runVoidTimed(false, "close_file", &IReader::closeFile);
92 }
93
94 void readEvent() override {
95 runVoidTimed(true, "read_event", &IReader::readEvent);
96 }
97
98 void goToEvent(unsigned ev) override {
99 // TODO: Do we need to time this? Not really used at the moment
100 m_reader.goToEvent(ev);
101 }
102
104 // no need to time this as it is really just a very simple get
105 return m_reader.currentFileVersion();
106 }
107
108private:
109 void recordTime(bool perEvent, const std::string& step, ClockT::duration duration) const {
110 if (perEvent) {
111 m_perEventTree.recordTime(step, duration);
112 } else {
113 m_recorder.recordTime("setup_times", step, duration);
114 }
115 }
116
117 template <typename FuncT, typename... Args>
118 inline std::invoke_result_t<FuncT, WrappedReader, Args...> runTimed(bool perEvent, const std::string& step,
119 FuncT func, Args&&... args) {
120 const auto [result, duration] = benchmark::run_member_timed(m_reader, func, std::forward<Args>(args)...);
121
122 recordTime(perEvent, step, duration);
123
124 return result;
125 }
126
127 template <typename FuncT, typename... Args>
128 inline std::invoke_result_t<FuncT, WrappedReader, Args...> runTimed(bool perEvent, const std::string& step,
129 FuncT func, Args&&... args) const {
130 const auto [result, duration] = benchmark::run_member_timed(m_reader, func, std::forward<Args>(args)...);
131
132 recordTime(perEvent, step, duration);
133
134 return result;
135 }
136
137 template <typename FuncT, typename... Args>
138 inline void runVoidTimed(bool perEvent, const std::string& step, FuncT func, Args&&... args) {
139 const auto duration = benchmark::run_void_member_timed(m_reader, func, std::forward<Args>(args)...);
140
141 recordTime(perEvent, step, duration);
142 }
143
144 // NOTE: c++ initializes its class members in the order they are defined not
145 // in the order in which they appear in the initializer list!
146 ClockT::time_point m_start; // to time the construction
147 WrappedReader m_reader; // the decorated reader that does the actual work
148 ClockT::time_point m_end; // to time the constructor call
149
150 benchmark::BenchmarkRecorder& m_recorder;
151 // Keep a reference to this one around, to save the look-up in each event
152 benchmark::BenchmarkRecorderTree& m_perEventTree;
153 ClockT::duration m_totalCollectionReadTime{std::chrono::nanoseconds{0}};
154};
155
156} // namespace podio
157
158#endif
virtual CollectionBase * readCollection(const std::string &name)=0
virtual std::shared_ptr< CollectionIDTable > getCollectionIDTable()=0
Get CollectionIDTable of read-in data.
virtual void endOfEvent()=0
Prepare the reader to read the next event.
virtual void openFile(const std::string &filename)=0
virtual std::map< int, GenericParameters > * readRunMetaData()=0
virtual GenericParameters * readEventMetaData()=0
read event meta data from file
virtual unsigned getEntries() const =0
get the number of events available from this reader
virtual void closeFile()=0
virtual void readEvent()=0
virtual std::map< int, GenericParameters > * readCollectionMetaData()=0
bool isValid() const override
Check if reader is valid.
Definition: TimedReader.h:82
void closeFile() override
Definition: TimedReader.h:90
std::map< int, GenericParameters > * readRunMetaData() override
Definition: TimedReader.h:63
void endOfEvent() override
Prepare the reader to read the next event.
Definition: TimedReader.h:73
std::map< int, GenericParameters > * readCollectionMetaData() override
Definition: TimedReader.h:59
podio::version::Version currentFileVersion() const override
Get the podio version with which the current file has been written.
Definition: TimedReader.h:103
CollectionBase * readCollection(const std::string &name) override
Definition: TimedReader.h:41
unsigned getEntries() const override
get the number of events available from this reader
Definition: TimedReader.h:68
void goToEvent(unsigned ev) override
Definition: TimedReader.h:98
TimedReader(benchmark::BenchmarkRecorder &recorder, Args &&... args)
Definition: TimedReader.h:20
void openFile(const std::string &filename) override
Definition: TimedReader.h:86
void readEvent() override
Definition: TimedReader.h:94
GenericParameters * readEventMetaData() override
read event meta data from file
Definition: TimedReader.h:55
std::shared_ptr< CollectionIDTable > getCollectionIDTable() override
Get CollectionIDTable of read-in data.
Definition: TimedReader.h:50
void recordTime(const std::string &stepName, const ClockT::duration time)
BenchmarkRecorderTree & addTree(const std::string &name, const std::vector< std::string > &steps)
void Fill(const std::string &treeName)
void recordTime(const std::string &treeName, const std::string &stepName, const ClockT::duration time)
ClockT::duration run_void_member_timed(Obj &obj, MemberFunc func, Args &&... args)
Definition: BenchmarkUtil.h:32
std::chrono::high_resolution_clock ClockT
Definition: BenchmarkUtil.h:9
std::pair< std::invoke_result_t< MemberFunc, Obj, Args... >, ClockT::duration > run_member_timed(Obj &obj, MemberFunc func, Args &&... args)
Definition: BenchmarkUtil.h:17