00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004
00005 #include "TVector3.h"
00006 #include "TStopwatch.h"
00007
00008 #include "BField/BfldValidate.h"
00009 #include "BField/BfldLoanPool.h"
00010 #include "MessageService/MsgService.h"
00011
00012 #include "TestConstants.h"
00013
00014 CVSID("$Id: TestPerf.cc,v 1.3 2001/08/21 19:42:54 agoldst Exp $");
00015
00016 const int kDefaultNumPerfTestPts = 1224;
00017
00018 class PerfTestFile {
00019 public:
00020 PerfTestFile(const char *name);
00021 ~PerfTestFile(void);
00022
00023 bool IsGood(void);
00024 void Store(int numPoints,float timeTaken,int algorithm,
00025 BfldInterpMethod::InterpMethod_t interp);
00026
00027 private:
00028 ofstream *outFile;
00029 static const char kHeader[40] = "BField Performance Test Results";
00030
00031 };
00032
00033 void BenchmarkPerformance(const char *outFileName,const char *rectMapFileName,
00034 const char *vorMapFileName,
00035 const char *vorMeshFileName,
00036 const char *vorBFldFileName,
00037 const char *testPtsFileName);
00038 double RectPerformanceTest(const char *mapFileName,
00039 BfldInterpMethod::InterpMethod_t interpMethod,
00040 vector<TVector3> &testPts);
00041 double VorPerformanceTest(const char *vorMapFileName,const char *meshFileName,
00042 const char *vorBFieldFileName,
00043 BfldInterpMethod::InterpMethod_t interpMethod,
00044 vector<TVector3> &testPts);
00045
00046 int GetNumPointsRect(const char *fileName);
00047 int GetNumPointsVor(const char *fileName);
00048 BField *NewBFieldRect(const char *mapFileName);
00049 BField *NewBFieldVor(const char *vorMapFileName,const char *meshFileName,
00050 const char *vorBFieldFileName);
00051
00052 void GetPerfTestPts(vector<TVector3> &pts,const char *fileName);
00053
00054 void BenchmarkPerformance(const char *outFileName,const char *rectMapFileName,
00055 const char *vorMapFileName,
00056 const char *vorMeshFileName,
00057 const char *vorBFldFileName,
00058 const char *testPtsFileName) {
00059
00060
00061
00062
00063
00064 PerfTestFile outFile(outFileName);
00065
00066 int numPoints;
00067 double timeTaken;
00068
00069
00070
00071 vector<TVector3> testPts(kDefaultNumPerfTestPts);
00072 GetPerfTestPts(testPts,testPtsFileName);
00073
00074 MSG("Bfldtest",Msg::kDebug) << "Testing rect2d algorithm..." << endl;
00075 MSG("Bfldtest",Msg::kDebug)
00076 << "At the moment, does not use file specified but instead" << endl;
00077 MSG("Bfldtest",Msg::kDebug)
00078 << "reverts to default file $BMAPPATH/bfld_142.dat" << endl;
00079
00080 numPoints = GetNumPointsRect(rectMapFileName);
00081
00082
00083 int j;
00084 for(j = 0;j < kNumRectInterpMethods;j++) {
00085 MSG("Bfldtest",Msg::kDebug) << "Testing interpolation method: "
00086 << BfldInterpMethod::AsString(kRectInterpMethods[j]) << endl;
00087 timeTaken = RectPerformanceTest(rectMapFileName,kRectInterpMethods[j],
00088 testPts);
00089
00090 outFile.Store(numPoints,timeTaken,kAlgRect,kRectInterpMethods[j]);
00091 }
00092
00093
00094
00095 MSG("Bfldtest",Msg::kDebug) << "Testing Voronoi algorithm..." << endl;
00096 MSG("Bfldtest",Msg::kDebug)
00097 << "At the moment does not use files specified but instead" << endl;
00098 MSG("Bfldtest",Msg::kDebug)
00099 << "reverts to default files BField/*.default" << endl;
00100
00101 MSG("Bfldtest",Msg::kDebug) << "Using the following files:" << endl;
00102 MSG("Bfldtest",Msg::kDebug)
00103 << "Voronoi diagram file: " << vorMapFileName << endl;
00104 MSG("Bfldtest",Msg::kDebug)
00105 << "Voronoi mesh file: " << vorMeshFileName << endl;
00106 MSG("Bfldtest",Msg::kDebug)
00107 << "Generator BField data file: " << vorBFldFileName << endl;
00108
00109 numPoints = GetNumPointsVor(vorMapFileName);
00110
00111 for(j = 0;j < kNumVorInterpMethods;j++) {
00112 MSG("Bfldtest",Msg::kDebug) << "Testing interpolation method: "
00113 << BfldInterpMethod::AsString(kVorInterpMethods[j]) << endl;
00114
00115 timeTaken = VorPerformanceTest(vorMapFileName,vorMeshFileName,
00116 vorBFldFileName,kVorInterpMethods[j],
00117 testPts);
00118
00119 outFile.Store(numPoints,timeTaken,kAlgVor,kVorInterpMethods[j]);
00120 }
00121
00122 MSG("Bfldtest",Msg::kInfo) << "Done with performance test." << endl;
00123 MSG("Bfldtest",Msg::kInfo) << "Output written to " << outFileName << endl;
00124 }
00125
00126 double RectPerformanceTest(const char *mapFileName,
00127 BfldInterpMethod::InterpMethod_t interpMethod,
00128 vector<TVector3> &testPts) {
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 MSG("Bfldtest",Msg::kDebug) << "Beginning performance test..." << endl;
00139
00140 BField *theField = NewBFieldRect(mapFileName);
00141 theField->SetInterpMethod(interpMethod);
00142
00143 TVector3 b;
00144 int i;
00145 TStopwatch timer;
00146
00147 theField->GetBField(testPts[0]);
00148
00149
00150
00151 timer.Start();
00152 for(i = 0;i < testPts.size();++i)
00153 b = theField->GetBField(testPts[i]);
00154 timer.Stop();
00155
00156 delete theField;
00157
00158 MSG("Bfldtest",Msg::kDebug) << "Test done." << endl;
00159
00160 return timer.CpuTime();
00161 }
00162
00163 double VorPerformanceTest(const char *vorMapFileName,const char *meshFileName,
00164 const char *vorBFieldFileName,
00165 BfldInterpMethod::InterpMethod_t interpMethod,
00166 vector<TVector3> &testPts) {
00167
00168
00169
00170
00171 BField *theField = NewBFieldVor(vorMapFileName,meshFileName,
00172 vorBFieldFileName);
00173 theField->SetInterpMethod(interpMethod);
00174 TVector3 v,b;
00175 int i;
00176
00177 theField->GetBField(testPts[0]);
00178
00179
00180
00181 MSG("Bfldtest",Msg::kDebug) << "Beginning performance test..." << endl;
00182
00183 TStopwatch timer;
00184 timer.Start();
00185 for(i = 0;i < testPts.size();++i)
00186 b = theField->GetBField(testPts[i]);
00187 timer.Stop();
00188
00189 delete theField;
00190
00191 MSG("Bfldtest",Msg::kDebug) << "Test done." << endl;
00192
00193 return timer.CpuTime();
00194 }
00195
00196 void GetPerfTestPts(vector<TVector3> &pts,const char *fileName) {
00197
00198
00199
00200
00201
00202
00203 MSG("Bfldtest",Msg::kDebug) << "Reading in performance test points file "
00204 << fileName << endl;
00205
00206 ifstream inFile(fileName,ios::in);
00207 if(!inFile.good()) {
00208 MSG("Bfldtest",Msg::kWarning)
00209 << "Error opening file " << fileName << endl;
00210 return;
00211 }
00212
00213 int n = pts.size();
00214 int i = 0;
00215
00216 int id_ignored;
00217 float x,y,z;
00218
00219 while(!inFile.eof()) {
00220 inFile >> id_ignored >> x >> y >> z;
00221 if(i >= n) {
00222 n *= 2;
00223 pts.resize(n);
00224 }
00225
00226 pts[i].SetXYZ(x / 100.0,y / 100.0,z / 100.0);
00227 i++;
00228 }
00229
00230 pts.resize(i);
00231 }
00232
00233 PerfTestFile::PerfTestFile(const char*fileName) {
00234 outFile = 0;
00235 outFile = new ofstream(fileName,ios::out);
00236 if(outFile->good())
00237 (*outFile) << kHeader << endl << endl;
00238 }
00239
00240 PerfTestFile::~PerfTestFile(void) {
00241 if(outFile)
00242 delete outFile;
00243 }
00244
00245 bool PerfTestFile::IsGood(void) {
00246 return (!outFile) ? false : outFile->good();
00247 }
00248
00249 void PerfTestFile::Store(int numPoints,float timeTaken,int algorithm,
00250 BfldInterpMethod::InterpMethod_t interp) {
00251
00252 if(algorithm == kAlgRect)
00253 (*outFile) << "Algorithm: Rect2d" << endl;
00254 else
00255 (*outFile) << "Algorithm: Voronoi" << endl;
00256
00257 (*outFile) << "Interpolation: " << BfldInterpMethod::AsString(interp)
00258 << endl;
00259 (*outFile) << "CPU time taken: " << timeTaken << endl;
00260
00261
00262 (*outFile) << " --- " << endl;
00263 }