Class Profiler


  • public class Profiler
    extends java.lang.Object
    Allow one to profile performance in JCMS.

    While admin:
    • 1. create a new profiler session :
      Profiler profiler = Profiler.getProfiler("test1", true);
    • 2. enable measure recording :
      profiler.startProfiling();
    Then for each request:
    • 3. Retrieve the already created profiler
      Profiler profiler = Profiler.getProfiler("test1", false);
    • 4. Retrieve the RequestProfiler for the current request
      Profiler.RequestProfiler rprofiler = profiler.getRequestProfiler(request);
    • 5. Start measures for this request
      rprofiler.startMeasures();
      it will force a full garbage collection, waiting for some time, so be sure not to profile on production systems!
    • 6. Add individual measures
            rprofiler.startMeasures("something");
            doSomething(...);
            rprofiler.endMeasures("something");
          
    • 7. End measures for this request
      rprofile.endMeasures();
    While admin:
    • 8. End measures recording:
      profiler.endProfiling();

    In the end, retrieve the measure and work with them.

    Note: this API is NOT thread safe, as doing memory measures simultaneously on different thread is not possible.
    Since:
    jcms-5.5.0
    Author:
    Olivier Jaquemet
    • Method Detail

      • getProfiler

        public static Profiler getProfiler​(java.lang.String name,
                                           boolean create)
        Return an existing Profiler with the name name, or null if it does not exist
        Parameters:
        name - the name of the profiler to retrieve
        create - if true, will create the profiler if it does not exist (be sure to create only when admin)
        Returns:
        the Profiler or null if does not exist
      • getAllProfilers

        public static java.util.Collection<Profiler> getAllProfilers()
        Returns:
        a collection containing all profilers currently available.
      • removeProfiler

        public static void removeProfiler​(java.lang.String name)
        Remove the profiler named "name".
        Parameters:
        name - the name of the profiler to remove
      • removeAllProfiler

        public static void removeAllProfiler()
        Remove all profilers currently availables.
      • getName

        public java.lang.String getName()
      • startProfiling

        public void startProfiling()
        Start profiling (clean older result if existing)
      • endProfiling

        public void endProfiling()
        End profiling and compute average of RequestProfiler measures.
      • isProfiling

        public boolean isProfiling()
      • addMeasuredId

        protected void addMeasuredId​(java.lang.String id)
        Method called by request profiler to add measured id;
      • computeAverage

        protected void computeAverage()
      • getRequestProfiler

        public Profiler.RequestProfiler getRequestProfiler​(javax.servlet.http.HttpServletRequest request)
        Return the request profiler associated with the given request and create a new one if none is available.
        Parameters:
        request - the request of which to find RequestProfiler
        Returns:
        a RequestProfiler
      • getAverageConsumedMemory

        public long getAverageConsumedMemory()
        Returns:
        the total average memory used between call to startMeasures() and endMeasures() of each request
      • getAverageElapsedTime

        public long getAverageElapsedTime()
        Returns:
        the total average time elapsed between call to startMeasures() and endMeasures() of each request
      • getAverageOutputSize

        public long getAverageOutputSize()
        Returns:
        the total average output size between call to startMeasures() and endMeasures() of each request
      • getAverageMeasuresMap

        public java.util.HashMap<java.lang.String,​long[]> getAverageMeasuresMap()
        Returns:
        the HashMap (id ==> long[3] {averageConsumedMemory, averagedTimeElapsed, averageOutputSize})
      • getProfiledRequestNumber

        public long getProfiledRequestNumber()
        Returns:
        the total number of request that have been used to do this profiling.
      • getStartDate

        public java.util.Date getStartDate()
        Returns:
        the Date on which the profiling was started.
      • getEndDate

        public java.util.Date getEndDate()
        Returns:
        the Date on which the profiling was ended.
      • getSortedAverageMeasuresMap

        public java.util.TreeMap<java.lang.String,​long[]> getSortedAverageMeasuresMap()
        Returns:
        the TreeMap (id ==> long[3] {averageConsumedMemory, averagedTimeElapsed, averageOutputSize }) sorted from slowest to fastest.