Audacity  3.0.3
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Profiler Class Reference

A simple profiler to measure the average time lengths that a particular task/function takes. Currently not thread-safe and not thread-smart, but it will probably work fine if you use it on a high level. More...

#include <Profiler.h>

Collaboration diagram for Profiler:
[legend]

Public Member Functions

virtual ~Profiler ()
 write to a profile at the end of the test. More...
 
void Begin (const char *fileName, int lineNum, const char *taskDescription)
 start the task timer. More...
 
void End (const char *fileName, int lineNum, const char *taskDescription)
 end the task timer. More...
 

Static Public Member Functions

static ProfilerInstance ()
 Gets the singleton instance. More...
 

Protected Member Functions

 Profiler ()
 private constructor - Singleton. More...
 
TaskProfileGetOrCreateTaskProfile (const char *fileName, int lineNum)
 find a taskProfile for the given task, otherwise create More...
 
TaskProfileGetTaskProfileByDescription (const char *description)
 

Protected Attributes

std::vector< std::unique_ptr< TaskProfile > > mTasks
 
std::mutex mTasksMutex
 

Detailed Description

A simple profiler to measure the average time lengths that a particular task/function takes. Currently not thread-safe and not thread-smart, but it will probably work fine if you use it on a high level.

Definition at line 39 of file Profiler.h.

Constructor & Destructor Documentation

◆ ~Profiler()

Profiler::~Profiler ( )
virtual

write to a profile at the end of the test.

Definition at line 31 of file Profiler.cpp.

32 {
33  if(mTasks.size())
34  {
35  //print everything out. append to a log.
36  FILE* log = fopen("AudacityProfilerLog.txt", "a");
37  time_t now;
38 
39  time(&now);
40  wxFprintf(log,"Audacity Profiler Run, Ended at ");
41  wxFprintf(log,"%s",ctime(&now));
42  wxFprintf(log,"****************************************\n");
43  //print out the tasks
44  for(int i=0;i<(int)mTasks.size();i++)
45  {
46  if(mTasks[i]->mNumHits>0)
47  {
48  wxFprintf(log,"Task: %s\n(begins at line %d in %s)\n\n",mTasks[i]->mDescription.get(), mTasks[i]->mLine, mTasks[i]->mFileName.get());
49  wxFprintf(log,"Number of times run: %d\n",mTasks[i]->mNumHits);
50  wxFprintf(log,"Total run time (seconds): %f\n", (double)mTasks[i]->mCumTime/CLOCKS_PER_SEC);
51  wxFprintf(log,"Average run time (seconds): %f\n",mTasks[i]->ComputeAverageRunTime());
52 
53  if(i < ((int)mTasks.size()) -1)
54  wxFprintf(log,"----------------------------\n");
55  }
56  }
57  wxFprintf(log,"\n****************************************\n\n\n");
58 
59  fclose(log);
60  }
61 }

References mTasks.

◆ Profiler()

Profiler::Profiler ( )
inlineprotected

private constructor - Singleton.

Definition at line 56 of file Profiler.h.

56 {};

Member Function Documentation

◆ Begin()

void Profiler::Begin ( const char *  fileName,
int  lineNum,
const char *  taskDescription 
)

start the task timer.

Definition at line 64 of file Profiler.cpp.

65 {
66  std::lock_guard<std::mutex> guard{ mTasksMutex };
67  GetOrCreateTaskProfile(fileName,lineNum)->Begin(fileName,lineNum,taskDescription);
68 }

References TaskProfile::Begin(), GetOrCreateTaskProfile(), and mTasksMutex.

Here is the call graph for this function:

◆ End()

void Profiler::End ( const char *  fileName,
int  lineNum,
const char *  taskDescription 
)

end the task timer.

Definition at line 71 of file Profiler.cpp.

72 {
73  std::lock_guard<std::mutex> guard{ mTasksMutex };
74  TaskProfile* tp;
75  tp=GetTaskProfileByDescription(taskDescription);
76  if(tp)
77  tp->End(fileName,lineNum,taskDescription);
78 }

References TaskProfile::End(), GetTaskProfileByDescription(), and mTasksMutex.

Here is the call graph for this function:

◆ GetOrCreateTaskProfile()

TaskProfile * Profiler::GetOrCreateTaskProfile ( const char *  fileName,
int  lineNum 
)
protected

find a taskProfile for the given task, otherwise create

Definition at line 90 of file Profiler.cpp.

91 {
92  for(int i=0;i<(int)mTasks.size();i++)
93  {
94  if(strcmp(fileName, mTasks[i]->mFileName.get())==0 && lineNum == mTasks[i]->mLine)
95  return mTasks[i].get();
96  }
97 
98  auto tp = std::make_unique<TaskProfile>();
99  mTasks.push_back(std::move(tp));
100  return mTasks.back().get();
101 }

References mTasks.

Referenced by Begin().

Here is the caller graph for this function:

◆ GetTaskProfileByDescription()

TaskProfile * Profiler::GetTaskProfileByDescription ( const char *  description)
protected

Definition at line 103 of file Profiler.cpp.

104 {
105  for(int i=0;i<(int)mTasks.size();i++)
106  {
107  if(strcmp(description, mTasks[i]->mDescription.get())==0)
108  return mTasks[i].get();
109  }
110 
111  return NULL;
112 
113 }

References mTasks.

Referenced by End().

Here is the caller graph for this function:

◆ Instance()

Profiler * Profiler::Instance ( )
static

Gets the singleton instance.

Definition at line 81 of file Profiler.cpp.

82 {
83  static Profiler pro;
84  //this isn't 100% threadsafe but I think Okay for this purpose.
85 
86  return &pro;
87 }

Member Data Documentation

◆ mTasks

std::vector<std::unique_ptr<TaskProfile> > Profiler::mTasks
protected

Definition at line 63 of file Profiler.h.

Referenced by GetOrCreateTaskProfile(), GetTaskProfileByDescription(), and ~Profiler().

◆ mTasksMutex

std::mutex Profiler::mTasksMutex
protected

Definition at line 65 of file Profiler.h.

Referenced by Begin(), and End().


The documentation for this class was generated from the following files:
Profiler
A simple profiler to measure the average time lengths that a particular task/function takes....
Definition: Profiler.h:40
Profiler::mTasksMutex
std::mutex mTasksMutex
Definition: Profiler.h:65
TaskProfile::Begin
void Begin(const char *fileName, int lineNum, const char *taskDescription)
start the task timer.
Definition: Profiler.cpp:128
Profiler::GetTaskProfileByDescription
TaskProfile * GetTaskProfileByDescription(const char *description)
Definition: Profiler.cpp:103
TaskProfile::End
void End(const char *fileName, int lineNum, const char *taskDescription)
end the task timer.
Definition: Profiler.cpp:144
Profiler::mTasks
std::vector< std::unique_ptr< TaskProfile > > mTasks
Definition: Profiler.h:63
Profiler::GetOrCreateTaskProfile
TaskProfile * GetOrCreateTaskProfile(const char *fileName, int lineNum)
find a taskProfile for the given task, otherwise create
Definition: Profiler.cpp:90
TaskProfile
a simple class to keep track of one task that may be called multiple times.
Definition: Profiler.h:70