1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkResourceProbe_h
19 #define itkResourceProbe_h
20 
21 #include "itkMacro.h"
22 #include "itkIntTypes.h"
23 
24 #include <string>
25 #include <vector>
26 
27 #include "ITKCommonExport.h"
28 
29 namespace itk
30 {
31 /** \class ResourceProbe
32   *  \brief Computes the change of a value between two points in code.
33   *
34   *   This class is the base class of all the probes (time, memory, etc.)
35   *   between the execution of two pieces of code. It can be started and
36   *   stopped in order to evaluate the execution over multiple passes.
37   *
38   *   \sa TimeResourceProbe, MemoryResourceProbe
39   *
40   * \ingroup ITKCommon
41   */
42 template< typename ValueType, typename MeanType >
43 class ITK_TEMPLATE_EXPORT ResourceProbe
44 {
45 public:
46 
47   /** Type for counting how many times the probe has been started and stopped.
48     */
49   using CountType = SizeValueType;
50 
51 public:
52 
53   /** Constructor */
54   ResourceProbe(std::string  type, std::string  unit);
55 
56   /** Destructor */
57   virtual ~ResourceProbe() = default;
58 
59   /** Returns the type probed value */
60   std::string GetType() const;
61 
62   /** Returns the unit probed value */
63   std::string GetUnit() const;
64 
65   /** Start counting the change of value */
66   virtual void Start();
67 
68   /** Stop counting the change of value.
69    *
70    * If a matching Start() has not been called before, there is no
71    * effect.
72    **/
73   virtual void Stop();
74 
75   /** Returns the number of times that the probe has been started */
76   CountType   GetNumberOfStarts() const;
77 
78   /** Returns the number of times that the probe has been stopped */
79   CountType   GetNumberOfStops() const;
80 
81   /** Returns the number of iteration of the probe */
82   CountType   GetNumberOfIteration() const;
83 
84   /** Returns the instant value of the probed system.
85    */
86   virtual ValueType   GetInstantValue() const = 0;
87 
88   /** Returns the accumulated value changes between the starts and stops
89    *  of the probe */
90   virtual ValueType    GetTotal() const;
91 
92   /** Returns the average value changes between the starts and stops
93    *  of the probe. Stop() has to be called at least once, returns 0 otherwise.
94    */
95   virtual MeanType     GetMean() const;
96 
97   /** Reset the probe */
98   virtual void Reset();
99 
100   /** Returns the min value changes between the starts and stops
101    *  of the probe */
102   virtual ValueType GetMinimum() const;
103 
104   /** Returns the max value changes between the starts and stops
105    *  of the probe */
106   virtual ValueType GetMaximum() const;
107 
108   /** Returns the standard deviation value changes between the starts and stops
109    *  of the probe. */
110   virtual ValueType GetStandardDeviation();
111 
112   /** Returns the standard deviation value changes between the starts and stops
113    *  of the probe. */
114   virtual ValueType GetStandardError();
115 
116   /** Set name of probe */
117   virtual void SetNameOfProbe(const char* nameOfProbe);
118 
119   /** Set name of probe */
120   virtual std::string GetNameOfProbe() const;
121 
122   /** Print System information */
123   virtual void PrintSystemInformation(std::ostream & os = std::cout);
124 
125   /** Print Probe Results. */
126   virtual void Report(std::ostream & os = std::cout, bool printSystemInfo = true,
127                       bool printReportHead = true, bool useTabs = false);
128 
129   /** Print Probe Results. */
130   virtual void ExpandedReport(std::ostream & os = std::cout, bool printSystemInfo = true,
131                               bool printReportHead = true, bool useTabs = false);
132 
133   /** Print Probe Results. */
134   virtual void JSONReport(std::ostream & os = std::cout);
135 
136   /** Print Probe Results. */
137   virtual void PrintJSONSystemInformation(std::ostream & os = std::cout);
138 
139 protected:
140   /** Update the Min and Max values with an input value */
141   virtual void UpdateMinimumMaximumMeasuredValue(ValueType value);
142 
143   /** Print Probe Results. */
144   virtual void PrintReportHead(std::ostream & os = std::cout, bool useTabs = false);
145 
146   /** Print Probe Results. */
147   virtual void PrintExpandedReportHead(std::ostream & os = std::cout, bool useTabs = false);
148 
149   /** Prints a varName: varValue pair. */
150   template<typename T>
151   void PrintJSONvar(std::ostream & os, const char* varName, T varValue,
152       unsigned indent = 4, bool comma = true);
153 
154   /** Obsolete member function from ITK 4.8 - 4.13. Does not do anything anymore. */
155   itkLegacyMacro(virtual void GetSystemInformation());
156 
157 private:
158 
159   ValueType                  m_StartValue;
160   ValueType                  m_TotalValue;
161   ValueType                  m_MinimumValue;
162   ValueType                  m_MaximumValue;
163   ValueType                  m_StandardDeviation;
164   ValueType                  m_StandardError;
165 
166   CountType                  m_NumberOfStarts;
167   CountType                  m_NumberOfStops;
168   CountType                  m_NumberOfIteration;
169 
170   std::vector<ValueType>     m_ProbeValueList;
171 
172   std::string                m_NameOfProbe;
173   std::string                m_TypeString;
174   std::string                m_UnitString;
175 
176   static constexpr unsigned int  tabwidth  = 15;
177 };
178 } // end namespace itk
179 
180 #ifndef ITK_MANUAL_INSTANTIATION
181 #include "itkResourceProbe.hxx"
182 #endif
183 
184 #endif //itkResourceProbe_h
185