LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lcio_io_benchmark.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCReader.h"
4 #include "IO/LCEventListener.h"
5 #include "IO/LCRunListener.h"
6 #include "IMPL/LCEventImpl.h"
7 #include "Exceptions.h"
8 
9 #include <cstdlib>
10 #include <memory>
11 #include <sstream>
12 
13 #include <TSystem.h>
14 #include <TFile.h>
15 #include <TTree.h>
16 
17 using namespace std ;
18 using namespace lcio ;
19 
25 public:
26  BenchmarkListener(const BenchmarkListener&) = delete ;
27  BenchmarkListener& operator=(const BenchmarkListener&) = delete ;
28 
35  m_pTFile = std::make_shared<TFile>( fname.c_str() , "RECREATE" ) ;
36  m_pTTree = new TTree( "LCIOBenchmark", "LCIOBenchmark" ) ;
37  m_pTTree->SetDirectory( m_pTFile.get() ) ;
38  // global benchmarking
39  m_pTTree->Branch( "ProcRSS", &m_procRSS, "ProcRSS/Long_t" ) ;
40  // read benchmarks
41  m_pTTree->Branch( "ReadTime", &m_readTime ) ;
42  m_pTTree->Branch( "ReadRSS", &m_readResidentMemory, "ReadRSS/Long_t" ) ;
43  // write benchmarks
44  m_pTTree->Branch( "WriteTime", &m_writeTime ) ;
45  m_pTTree->Branch( "WriteRSS", &m_writeRSS, "WriteRSS/Long_t" ) ;
46  // the entry id for easy plotting
47  m_pTTree->Branch( "Entry", &m_entry ) ;
48  // init benchamark vars
49  ProcInfo_t procInfo ;
50  gSystem->GetProcInfo(&procInfo);
51  m_residentMemoryReference = procInfo.fMemResident;
52  m_firstClock = m_lastClock = clock();
53  // initialize the LCWriter instance
54  m_lcWriter = LCFactory::getInstance()->createLCWriter();
55  m_lcioOutputFileName << "LCIO_benchmarking_" << gSystem->GetPid() << ".slcio";
56  std::cout << "Opening output LCIO file: " << m_lcioOutputFileName.str() << std::endl;
57  m_lcWriter->open( m_lcioOutputFileName.str(), LCIO::WRITE_NEW );
58  }
59 
64  std::cout << "Closing and deleting LCIO file " << m_lcioOutputFileName.str() << std::endl;
65  m_lcWriter->close();
66  delete m_lcWriter;
67  std::remove( m_lcioOutputFileName.str().c_str() );
68  std::cout << "Total processing time was " << float(m_lastClock - m_firstClock) / CLOCKS_PER_SEC << " secs" << std::endl;
69  }
70 
77  getVariables(evt);
78  m_pTTree->Fill();
79  }
80 
87  // Get benchmarks and fill with the tree
88  // "Read event" variables
89  ProcInfo_t procInfo, procInfo2 ;
90  clock_t currentClock = clock();
91  m_readTime = float( currentClock - m_lastClock ) / CLOCKS_PER_SEC ;
92  gSystem->GetProcInfo(&procInfo);
93  m_procRSS = procInfo.fMemResident;
94  m_readResidentMemory = ( procInfo.fMemResident - m_residentMemoryReference ) ;
95  // "Write event" variables
96  clock_t writeStart = clock() ;
97  m_lcWriter->writeEvent(event) ;
98  clock_t writeEnd = clock() ;
99  gSystem->GetProcInfo(&procInfo2) ;
100  m_writeTime = float( writeEnd - writeStart ) / CLOCKS_PER_SEC ;
101  m_writeRSS = ( procInfo2.fMemResident - procInfo.fMemResident ) ;
102  m_residentMemoryReference = procInfo2.fMemResident ;
103  m_lastClock = clock();
104  m_entry++;
105  }
106 
110  void write() {
111  m_pTFile->cd();
112  m_pTTree->Write();
113  m_pTFile->Close();
114  m_pTFile = nullptr;
115  }
116 
117 private:
118  void modifyEvent(EVENT::LCEvent * /*evt*/) { /* nop */ }
119  void processRunHeader(EVENT::LCRunHeader * /*rh*/) { /* nop */ }
120  void modifyRunHeader(EVENT::LCRunHeader * /*rh*/) { /* nop */ }
121 
122 private:
124  std::shared_ptr<TFile> m_pTFile {nullptr} ;
126  TTree *m_pTTree {nullptr} ;
128  IO::LCWriter *m_lcWriter {nullptr} ;
130  std::stringstream m_lcioOutputFileName {} ;
132  Float_t m_readTime {0} ;
134  clock_t m_firstClock {0} ;
136  clock_t m_lastClock {0} ;
138  Long_t m_residentMemoryReference {0} ;
140  Long_t m_readResidentMemory {0} ;
142  Int_t m_entry {0} ;
144  Float_t m_writeTime {0} ;
146  Long_t m_writeRSS {0} ;
148  Long_t m_procRSS {0} ;
149 };
150 
153 int main(int argc, char** argv ){
154 
155  // read file names from command line (only argument)
156  if( argc < 3) {
157  cout << " read/write LCIO file and output io benchmarks in a ROOT file" << endl << endl;
158  cout << " usage: lcio_io_benchmark <input-file> <output-file> [<nevents>]" << endl ;
159  exit(1) ;
160  }
161  std::string infilen, outfilen ;
162  int nevents = -1;
163  infilen = argv[1] ;
164  outfilen = argv[2] ;
165  if( argc > 3 ) {
166  nevents = atoi( argv[3] );
167  }
168 
169  auto lcReader = LCFactory::getInstance()->createLCReader() ;
170  lcReader->open( infilen ) ;
171 
172  BenchmarkListener *benchmark = new BenchmarkListener( outfilen );
173  lcReader->registerLCEventListener(benchmark);
174  lcReader->registerLCRunListener(benchmark);
175 
176  try {
177  // read the stream, filling benchmark variables
178  if( nevents > 0 ) {
179  lcReader->readStream( nevents );
180  }
181  else {
182  lcReader->readStream();
183  }
184  }
185  catch(IO::EndOfDataException) {
186 
187  }
188 
189  lcReader->close() ;
190  benchmark->write();
191  delete benchmark;
192  delete lcReader ;
193 
194  return 0 ;
195 }
196 
197 
T atoi(T...args)
void processEvent(EVENT::LCEvent *evt)
Extract and fill benchmark variables.
BenchmarkListener(const std::string &fname)
Constructor.
~BenchmarkListener()
Destructor.
void modifyRunHeader(EVENT::LCRunHeader *)
Call back for modifying an existing LCRunHeader.
Interface for the run header.
Definition: LCRunHeader.h:23
T endl(T...args)
T clock(T...args)
LCEvent * event
Definition: lsh.cc:80
BenchmarkListener class Simple event/run listener getting memory/proc statistics on callback...
Listener for the occurence of LCEvents when reading a stream.
STL class.
T exit(T...args)
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
Listener for the occurence of LCRunHeader when reading a stream.
Definition: LCRunListener.h:24
void write()
Write benchmarks to ROOT file.
void processRunHeader(EVENT::LCRunHeader *)
Call back for processing an LCRunHeader.
The main event interface.
Definition: LCEvent.h:31
T c_str(T...args)
LCReader * lcReader
Definition: lsh.cc:78
void getVariables(EVENT::LCEvent *event)
Extract benchmark variables.
void modifyEvent(EVENT::LCEvent *)
Call back for updating an LCEvent.
Interface for writing data with LCIO.
Definition: LCWriter.h:27
EndOfDataException for signaling the end of a data stream.
Definition: Exceptions.h:108