Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-08 23:11:21

0001 #include "PerfTools/Callgrind/interface/ProfilerService.h"
0002 
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0005 
0006 #include <boost/ref.hpp>
0007 
0008 #include <limits>
0009 #include <vector>
0010 #include <string>
0011 #include <cmath>
0012 
0013 #include <cppunit/extensions/HelperMacros.h>
0014 
0015 namespace {
0016   std::string gS;
0017   double gD;
0018   void doSomething(std::string const& name) {
0019     static std::string const local("p1");
0020     if (name == local)
0021       gS = name;
0022     if (!name.empty())
0023       gD += std::sqrt(double(name[0]));
0024   }
0025   void doSomethingElse(std::string const& name) {
0026     static std::string const local("p1");
0027     if (name == local)
0028       gS = name;
0029     if (!name.empty())
0030       gD += std::sqrt(double(name[0]));
0031   }
0032 }  // namespace
0033 
0034 namespace test {
0035   class TestProfilerService : public CppUnit::TestFixture {
0036     CPPUNIT_TEST_SUITE(TestProfilerService);
0037     CPPUNIT_TEST(check_constr);
0038     CPPUNIT_TEST(check_config);
0039     CPPUNIT_TEST(check_Instrumentation);
0040     CPPUNIT_TEST(check_FullEvent);
0041     CPPUNIT_TEST(check_Event);
0042     CPPUNIT_TEST(check_Path);
0043     CPPUNIT_TEST(check_ExcludedPath);
0044     // CPPUNIT_TEST(check_AllPaths);
0045     CPPUNIT_TEST(check_Nesting);
0046     CPPUNIT_TEST_SUITE_END();
0047 
0048   public:
0049     TestProfilerService();
0050     void setUp();
0051     void tearDown();
0052     void check_constr();
0053     void check_config();
0054     void check_Instrumentation();
0055     void check_FullEvent();
0056     void check_Event();
0057     void check_Path();
0058     void check_ExcludedPath();
0059     void check_AllPaths();
0060     void check_Nesting();
0061   };
0062 
0063   CPPUNIT_TEST_SUITE_REGISTRATION(TestProfilerService);
0064 
0065   TestProfilerService::TestProfilerService() {}
0066 
0067   void TestProfilerService::setUp() {}
0068 
0069   void TestProfilerService::tearDown() {}
0070 
0071   void TestProfilerService::check_constr() {
0072     edm::ParameterSet pset;
0073     edm::ActivityRegistry activity;
0074     ProfilerService ps(pset, activity);
0075     CPPUNIT_ASSERT(ps.m_firstEvent == 0);
0076     CPPUNIT_ASSERT(ps.m_lastEvent == std::numeric_limits<int>::max());
0077     CPPUNIT_ASSERT(ps.m_dumpInterval == 100);
0078     CPPUNIT_ASSERT(ps.m_excludedPaths.empty());
0079     CPPUNIT_ASSERT(ps.m_paths.empty());
0080   }
0081 
0082   void TestProfilerService::check_config() {
0083     int fe = 2;
0084     int le = 10;
0085     int di = 5;
0086     std::vector<std::string> paths = {"p1", "p2", "p3"};
0087     std::vector<std::string> ep = {"e1", "e2", "e3"};
0088     edm::ParameterSet pset;
0089     pset.addUntrackedParameter<int>("firstEvent", fe);
0090     pset.addUntrackedParameter<int>("lastEvent", le);
0091     pset.addUntrackedParameter<int>("dumpInterval", di);
0092     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0093     pset.addUntrackedParameter<std::vector<std::string> >("excludePaths", ep);
0094     edm::ActivityRegistry activity;
0095     {
0096       ProfilerService ps(pset, activity);
0097       CPPUNIT_ASSERT(ps.m_firstEvent == fe);
0098       CPPUNIT_ASSERT(ps.m_lastEvent == le);
0099       CPPUNIT_ASSERT(ps.m_dumpInterval == di);
0100       CPPUNIT_ASSERT(ps.m_excludedPaths == ep);
0101       CPPUNIT_ASSERT(ps.m_paths == paths);
0102       CPPUNIT_ASSERT(!ps.m_allPaths);
0103     }
0104     paths.push_back("ALL");
0105     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0106     {
0107       ProfilerService ps(pset, activity);
0108       CPPUNIT_ASSERT(ps.m_allPaths);
0109     }
0110   }
0111 
0112   void TestProfilerService::check_Instrumentation() {
0113     int fe = 2;
0114     int le = 10;
0115     int di = 5;
0116     edm::ParameterSet pset;
0117     pset.addUntrackedParameter<int>("firstEvent", fe);
0118     pset.addUntrackedParameter<int>("lastEvent", le);
0119     pset.addUntrackedParameter<int>("dumpInterval", di);
0120     edm::ActivityRegistry activity;
0121     ProfilerService ps(pset, activity);
0122     ps.beginEvent();
0123     CPPUNIT_ASSERT(ps.m_active == 0);
0124     CPPUNIT_ASSERT(!ps.doEvent());
0125     CPPUNIT_ASSERT(ps.m_counts == 0);
0126     CPPUNIT_ASSERT(!ps.startInstrumentation());
0127     doSomethingElse("bha");
0128     CPPUNIT_ASSERT(!ps.stopInstrumentation());
0129     CPPUNIT_ASSERT(!ps.forceStopInstrumentation());
0130 
0131     ps.beginEvent();
0132     CPPUNIT_ASSERT(ps.m_active == 0);
0133     CPPUNIT_ASSERT(ps.m_counts == 0);
0134     CPPUNIT_ASSERT(ps.doEvent());
0135     CPPUNIT_ASSERT(ps.startInstrumentation());
0136     CPPUNIT_ASSERT(ps.m_counts == 1);
0137     doSomething("bha");
0138     CPPUNIT_ASSERT(ps.stopInstrumentation());
0139     CPPUNIT_ASSERT(!ps.forceStopInstrumentation());
0140 
0141     CPPUNIT_ASSERT(ps.startInstrumentation());
0142     CPPUNIT_ASSERT(!ps.startInstrumentation());
0143     doSomething("bha");
0144     CPPUNIT_ASSERT(!ps.stopInstrumentation());
0145     CPPUNIT_ASSERT(ps.stopInstrumentation());
0146     CPPUNIT_ASSERT(!ps.stopInstrumentation());
0147 
0148     for (int i = 2; i < 10; i++) {
0149       ps.beginEvent();
0150       doSomething("bha");
0151     }
0152     CPPUNIT_ASSERT(ps.m_evtCount == 10);
0153     CPPUNIT_ASSERT(ps.doEvent());
0154     CPPUNIT_ASSERT(ps.startInstrumentation());
0155     CPPUNIT_ASSERT(ps.m_counts == 3);
0156     doSomething("bha");
0157     CPPUNIT_ASSERT(ps.stopInstrumentation());
0158     ps.beginEvent();
0159     CPPUNIT_ASSERT(ps.m_evtCount == 11);
0160     CPPUNIT_ASSERT(!ps.doEvent());
0161     CPPUNIT_ASSERT(!ps.startInstrumentation());
0162     doSomethingElse("bha");
0163     CPPUNIT_ASSERT(!ps.stopInstrumentation());
0164     CPPUNIT_ASSERT(ps.m_counts == 3);
0165   }
0166 
0167   void TestProfilerService::check_FullEvent() {
0168     int fe = 2;
0169     int le = 10;
0170     std::vector<std::string> paths = {"FullEvent"};
0171     edm::ParameterSet pset;
0172     pset.addUntrackedParameter<int>("firstEvent", fe);
0173     pset.addUntrackedParameter<int>("lastEvent", le);
0174     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0175     edm::ActivityRegistry activity;
0176     ProfilerService ps(pset, activity);
0177 
0178     ps.fullEvent();
0179     CPPUNIT_ASSERT(ps.m_active == 0);
0180     CPPUNIT_ASSERT(!ps.doEvent());
0181     doSomethingElse("bha");
0182 
0183     ps.fullEvent();
0184     CPPUNIT_ASSERT(ps.m_active == 1);
0185     CPPUNIT_ASSERT(ps.doEvent());
0186     doSomething("bha");
0187     for (int i = 2; i < 10; i++) {
0188       ps.fullEvent();
0189       doSomething("bha");
0190     }
0191     CPPUNIT_ASSERT(ps.m_evtCount == 10);
0192     CPPUNIT_ASSERT(ps.doEvent());  // who cares?
0193 
0194     ps.fullEvent();
0195     CPPUNIT_ASSERT(ps.m_active == 0);
0196     CPPUNIT_ASSERT(!ps.doEvent());
0197     doSomethingElse("bha");
0198     ps.fullEvent();
0199     CPPUNIT_ASSERT(ps.m_active == 0);
0200   }
0201 
0202   // now used ALL paths....
0203   void TestProfilerService::check_Event() {
0204     int fe = 2;
0205     int le = 10;
0206     std::vector<std::string> paths = {"ALL"};
0207     edm::ParameterSet pset;
0208     pset.addUntrackedParameter<int>("firstEvent", fe);
0209     pset.addUntrackedParameter<int>("lastEvent", le);
0210     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0211     edm::ActivityRegistry activity;
0212     ProfilerService ps(pset, activity);
0213 
0214     ps.beginEvent();
0215     CPPUNIT_ASSERT(ps.m_active == 0);
0216     CPPUNIT_ASSERT(!ps.doEvent());
0217     doSomethingElse("bha");
0218     ps.endEvent();
0219 
0220     ps.beginEvent();
0221     CPPUNIT_ASSERT(ps.m_active == 1);
0222     CPPUNIT_ASSERT(ps.doEvent());
0223     doSomething("bha");
0224     ps.endEvent();
0225     CPPUNIT_ASSERT(ps.m_active == 0);
0226     for (int i = 2; i < 10; i++) {
0227       ps.beginEvent();
0228       doSomething("bha");
0229       ps.endEvent();
0230     }
0231     CPPUNIT_ASSERT(ps.m_evtCount == 10);
0232     CPPUNIT_ASSERT(ps.doEvent());  // who cares?
0233 
0234     ps.beginEvent();
0235     CPPUNIT_ASSERT(ps.m_active == 0);
0236     CPPUNIT_ASSERT(!ps.doEvent());
0237     doSomethingElse("bha");
0238     ps.endEvent();
0239     CPPUNIT_ASSERT(ps.m_active == 0);
0240   }
0241 
0242   struct CheckPaths {
0243     CheckPaths(ProfilerService& ips, std::vector<std::string> const& iselpaths, int ibase = 0, bool iexc = false)
0244         : ps(ips), selpaths(iselpaths), base(ibase), exc(iexc), done(0) {}
0245     ProfilerService& ps;
0246     std::vector<std::string> const& selpaths;
0247     int base;
0248     bool exc;
0249 
0250     mutable int done;
0251 
0252     void operator()(std::string const& path) const {
0253       bool found = std::find(selpaths.begin(), selpaths.end(), path) != selpaths.end();
0254       bool ok = ps.doEvent() && (exc ? !found : found);
0255       noselPath(true);
0256       ps.beginPath(path);
0257       if (ok)
0258         selPath(path);
0259       else
0260         noselPath(!ps.doEvent());
0261       ps.endPath(path);
0262       noselPath(true);
0263     }
0264 
0265     void selPath(const std::string& path) const {
0266       CPPUNIT_ASSERT(ps.m_active == (base + (exc ? 0 : 1)));
0267       CPPUNIT_ASSERT(exc || ps.m_activePath == path);
0268       CPPUNIT_ASSERT(!ps.m_paused);
0269       ++done;
0270       doSomething(path);
0271     }
0272 
0273     void noselPath(bool f = false) const {
0274       CPPUNIT_ASSERT(ps.m_active == base);
0275       CPPUNIT_ASSERT(ps.m_activePath.empty());
0276       CPPUNIT_ASSERT(f || (!exc) || ps.m_paused);
0277       CPPUNIT_ASSERT(!(f && ps.m_paused));
0278       doSomethingElse("else");
0279     }
0280   };
0281 
0282   void TestProfilerService::check_Path() {
0283     int fe = 2;
0284     int le = 10;
0285     std::vector<std::string> paths = {"p1", "p2", "p3"};
0286     edm::ParameterSet pset;
0287     pset.addUntrackedParameter<int>("firstEvent", fe);
0288     pset.addUntrackedParameter<int>("lastEvent", le);
0289     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0290     edm::ActivityRegistry activity;
0291     ProfilerService ps(pset, activity);
0292 
0293     std::vector<std::string> allPaths = {"p1", "p21", "p22", "p3"};
0294     CheckPaths cp(ps, paths);
0295     CPPUNIT_ASSERT(std::find(paths.begin(), paths.end(), allPaths[0]) != paths.end());
0296     CPPUNIT_ASSERT(std::find(paths.begin(), paths.end(), allPaths[1]) == paths.end());
0297     CPPUNIT_ASSERT(std::find(paths.begin(), paths.end(), allPaths[2]) == paths.end());
0298     CPPUNIT_ASSERT(std::find(paths.begin(), paths.end(), allPaths[3]) != paths.end());
0299 
0300     ps.beginEvent();
0301     CPPUNIT_ASSERT(ps.m_active == 0);
0302     CPPUNIT_ASSERT(!ps.doEvent());
0303     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp).done == 0);
0304     ps.endEvent();
0305 
0306     ps.beginEvent();
0307     CPPUNIT_ASSERT(ps.m_active == 0);
0308     CPPUNIT_ASSERT(ps.doEvent());
0309     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp).done == 2);
0310     ps.endEvent();
0311   }
0312   void TestProfilerService::check_ExcludedPath() {
0313     int fe = 2;
0314     int le = 10;
0315     std::vector<std::string> paths = {"ALL"};
0316     std::vector<std::string> expaths = {"p21"};
0317     edm::ParameterSet pset;
0318     pset.addUntrackedParameter<int>("firstEvent", fe);
0319     pset.addUntrackedParameter<int>("lastEvent", le);
0320     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0321     pset.addUntrackedParameter<std::vector<std::string> >("excludePaths", expaths);
0322     edm::ActivityRegistry activity;
0323     ProfilerService ps(pset, activity);
0324 
0325     std::vector<std::string> allPaths = {"p1", "p21", "p22", "p3"};
0326     CheckPaths cp0(ps, expaths, 0, true);
0327     CheckPaths cp1(ps, expaths, 1, true);
0328     CPPUNIT_ASSERT(std::find(expaths.begin(), expaths.end(), allPaths[0]) == expaths.end());
0329     CPPUNIT_ASSERT(std::find(expaths.begin(), expaths.end(), allPaths[1]) != expaths.end());
0330     CPPUNIT_ASSERT(std::find(expaths.begin(), expaths.end(), allPaths[2]) == expaths.end());
0331     CPPUNIT_ASSERT(std::find(expaths.begin(), expaths.end(), allPaths[3]) == expaths.end());
0332 
0333     ps.beginEvent();
0334     CPPUNIT_ASSERT(ps.m_active == 0);
0335     CPPUNIT_ASSERT(!ps.doEvent());
0336     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp0).done == 0);
0337     ps.endEvent();
0338 
0339     ps.beginEvent();
0340     CPPUNIT_ASSERT(ps.m_active != 0);
0341     CPPUNIT_ASSERT(ps.doEvent());
0342     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp1).done == 3);
0343     ps.endEvent();
0344   }
0345 
0346   // same as nesting, removed....
0347   void TestProfilerService::check_AllPaths() {
0348     int fe = 2;
0349     int le = 10;
0350     std::vector<std::string> paths = {"ALL", "p2", "p3"};
0351     edm::ParameterSet pset;
0352     pset.addUntrackedParameter<int>("firstEvent", fe);
0353     pset.addUntrackedParameter<int>("lastEvent", le);
0354     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0355     edm::ActivityRegistry activity;
0356     ProfilerService ps(pset, activity);
0357 
0358     std::vector<std::string> allPaths = {"p1", "p21", "p22", "p3"};
0359     CheckPaths cp(ps, paths);
0360 
0361     ps.beginEvent();
0362     CPPUNIT_ASSERT(ps.m_active == 0);
0363     CPPUNIT_ASSERT(!ps.doEvent());
0364     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp).done == 0);
0365     ps.endEvent();
0366 
0367     ps.beginEvent();
0368     CPPUNIT_ASSERT(ps.m_active == 1);
0369     CPPUNIT_ASSERT(ps.doEvent());
0370     //  CPPUNIT_ASSERT(std::for_each(allPaths.begin(),allPaths.end(),cp).done==4);
0371     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp).done == 1);
0372     ps.endEvent();
0373   }
0374 
0375   void TestProfilerService::check_Nesting() {
0376     int fe = 2;
0377     int le = 10;
0378     std::vector<std::string> paths = {"ALL", "p1", "p2", "p3"};
0379     edm::ParameterSet pset;
0380     pset.addUntrackedParameter<int>("firstEvent", fe);
0381     pset.addUntrackedParameter<int>("lastEvent", le);
0382     pset.addUntrackedParameter<std::vector<std::string> >("paths", paths);
0383     edm::ActivityRegistry activity;
0384     ProfilerService ps(pset, activity);
0385 
0386     std::vector<std::string> allPaths = {"p1", "p21", "p22", "p3"};
0387 
0388     CheckPaths cp0(ps, paths, 0);
0389     CheckPaths cp1(ps, paths, 1);
0390 
0391     ps.beginEvent();
0392     CPPUNIT_ASSERT(ps.m_active == 0);
0393     CPPUNIT_ASSERT(!ps.doEvent());
0394     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp0).done == 0);
0395     ps.endEvent();
0396     CPPUNIT_ASSERT(ps.m_active == 0);
0397 
0398     ps.beginEvent();
0399     CPPUNIT_ASSERT(ps.m_active == 1);
0400     CPPUNIT_ASSERT(ps.doEvent());
0401     CPPUNIT_ASSERT(std::for_each(allPaths.begin(), allPaths.end(), cp1).done == 2);
0402     ps.endEvent();
0403     CPPUNIT_ASSERT(ps.m_active == 0);
0404   }
0405 }  // namespace test