Ocular Engine
Ocular::Core::Profiler Class Reference

#include <Profiler.hpp>

Public Member Functions

void beginBlock (std::string const &name, std::string const &segment)
 
void endBlock ()
 
void printToConsole (bool prettify=true) const
 
void printToTXT (std::string const &path) const
 
void printToHTML (std::string const &path) const
 

Protected Member Functions

void destroy ()
 
void destroyNode (ProfilerNode *node) const
 
void createLogBuffer (std::stringstream &stream, bool prettify) const
 
void createLogForNode (ProfilerNode *node, std::stringstream &stream, bool prettify) const
 
std::string prettifyName (std::string const &name) const
 

Detailed Description

A simple CPU performance profiler. Records the amount of time it takes to execute a given segment of code. The profiler may be invoked directly, or the PROFILE family of macros can be used instead.

Performance readings are done in a series of nested blocks, and the results can either be queried at runtime or saved to an external log.

In order for profiling to occur, the OCULAR_PROFILE flag must be enabled. By default, all debug builds of the Ocular Engine have this flag enabled.

It should be noted that while the act of profiling may cause a slowdown in program performance, the recording results of the profiling are of the true execution time of the code segment. So, deeply nested instances of profiling will not affect the performance readings.

There are two different types of profiling: per scope or per designated block. These can be seen in the following example:

void ParentMethod()
{
    PROFILE()
    ChildMethod();
}

void ChildMethod()
{
    PROFILE()

    // ...

    SubchildMethod();
}

void SubchildMethod()
{
    PROFILE_START("Segment A")

    // ...

    PROFILE_END()
    PROFILE_START("Segment B")

    // ...

    PROFILE_END()
}

Which results in the following output (when printed to console or as plain-text):

- ParentMethod [1] [100.0ms] [100.0ms]
  - ChildMethod [1] [100.0ms] [100.0ms]
    - SubchildMethod "Segment A" [1] [50.0ms] [50.0ms]
    - SubchildMethod "Segment B" [1] [50.0ms] [50.0ms]

The output lines are formatted as follows:

Name Segment Calls Elapsed Average 

Where,

     Name: Name of the profiled function/method
  Segment: Name of the block segment (optional)
    Calls: Total amount of times this segment of code had been called
  Elapsed: Total amount of elapsed time spent in the code segment
  Average: Average amount of time spent executing the code segment

Member Function Documentation

void Ocular::Core::Profiler::beginBlock ( std::string const &  name,
std::string const &  segment 
)

Begins a new block of code to profile.

Parameters
[in]nameName of the code block.
[in]segmentOptional segment name if part of a larger block of profiled code. Use this if, for example, you are profiling multiple individual segments of a single function.
void Ocular::Core::Profiler::endBlock ( )

Ends the current profile block.

std::string Ocular::Core::Profiler::prettifyName ( std::string const &  name) const
protected

Regex to search for match inbetween (but not including) ' ' and '(' Breaking the regex down as I will most likely forget what is happening:

      \b : Selection starts on a word boundary
([^\s])+ : Selection contains 1 or more non-space characters
 ?(?=\() : Selection ends with a '(' but does not include it (using lookahead)
void Ocular::Core::Profiler::printToConsole ( bool  prettify = true) const

Prints the current profiler data to the console (standard out).

Parameters
[in]prettifyIf TRUE, function signatures are trimmed to just their names (namespace::class:method).
void Ocular::Core::Profiler::printToHTML ( std::string const &  path) const

Prints the current profiler data as a pre-formatted HTML/JS tree table to the specified location.

Parameters
[in]pathFile path to output the data to (expects a .html)
Todo:
Profiler output to HTML
void Ocular::Core::Profiler::printToTXT ( std::string const &  path) const

Prints the current profiler data to the specified .txt file. Output is formatted in the same manner as the prettified console output.

Parameters
[in]pathFile path to output the data to (expects a .txt)

The documentation for this class was generated from the following files: