explicit std namespace
This commit is contained in:
parent
fa1d70e6a5
commit
a868e34fa1
1 changed files with 21 additions and 20 deletions
|
@ -5,6 +5,7 @@
|
|||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include "ofMain.h"
|
||||
|
||||
|
@ -12,34 +13,34 @@
|
|||
|
||||
namespace ofxProfiler {
|
||||
|
||||
using FloatingPointMicroseconds = chrono::duration <double, micro>;
|
||||
using FloatingPointMicroseconds = std::chrono::duration <double, std::micro>;
|
||||
|
||||
struct ProfileResult {
|
||||
string Name;
|
||||
std::string Name;
|
||||
|
||||
FloatingPointMicroseconds Start;
|
||||
chrono::microseconds ElapsedTime;
|
||||
thread::id ThreadID;
|
||||
std::chrono::microseconds ElapsedTime;
|
||||
std::thread::id ThreadID;
|
||||
};
|
||||
|
||||
struct InstrumentationSession {
|
||||
string Name;
|
||||
std::string Name;
|
||||
};
|
||||
|
||||
class Instrumentor {
|
||||
private:
|
||||
mutex m_Mutex;
|
||||
std::mutex m_Mutex;
|
||||
InstrumentationSession * m_CurrentSession;
|
||||
ofstream m_OutputStream;
|
||||
std::ofstream m_OutputStream;
|
||||
public:
|
||||
Instrumentor()
|
||||
: m_CurrentSession(nullptr){
|
||||
}
|
||||
|
||||
void BeginSession(const string & name = "I do not want to name my profiling sessions.",
|
||||
const string & filepath = "results.json",
|
||||
void BeginSession(const std::string & name = "I do not want to name my profiling sessions.",
|
||||
const std::string & filepath = "results.json",
|
||||
const bool continueSession = false){
|
||||
lock_guard <mutex> lock(m_Mutex);
|
||||
std::lock_guard <std::mutex> lock(m_Mutex);
|
||||
if(!m_CurrentSession || !continueSession){
|
||||
// if there is no session, or it should not be continued
|
||||
if(m_CurrentSession){
|
||||
|
@ -62,17 +63,17 @@ class Instrumentor {
|
|||
}
|
||||
|
||||
void EndSession(){
|
||||
lock_guard <mutex> lock(m_Mutex);
|
||||
std::lock_guard <std::mutex> lock(m_Mutex);
|
||||
InternalEndSession();
|
||||
}
|
||||
|
||||
void WriteProfile(const ProfileResult & result){
|
||||
stringstream json;
|
||||
std::stringstream json;
|
||||
|
||||
string name = result.Name;
|
||||
std::string name = result.Name;
|
||||
replace(name.begin(), name.end(), '"', '\'');
|
||||
|
||||
json << setprecision(3) << fixed;
|
||||
json << std::setprecision(3) << std::fixed;
|
||||
json << ",{";
|
||||
json << "\"cat\":\"function\",";
|
||||
json << "\"dur\":" << (result.ElapsedTime.count()) << ',';
|
||||
|
@ -83,7 +84,7 @@ class Instrumentor {
|
|||
json << "\"ts\":" << result.Start.count();
|
||||
json << "}";
|
||||
|
||||
lock_guard <mutex> lock(m_Mutex);
|
||||
std::lock_guard <std::mutex> lock(m_Mutex);
|
||||
if(m_CurrentSession){
|
||||
m_OutputStream << json.str();
|
||||
m_OutputStream.flush();
|
||||
|
@ -123,7 +124,7 @@ class InstrumentationTimer {
|
|||
public:
|
||||
InstrumentationTimer(const char * name)
|
||||
: m_Name(name), m_Stopped(false){
|
||||
m_StartTimepoint = chrono::steady_clock::now();
|
||||
m_StartTimepoint = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
~InstrumentationTimer(){
|
||||
|
@ -133,17 +134,17 @@ class InstrumentationTimer {
|
|||
}
|
||||
|
||||
void Stop(){
|
||||
auto endTimepoint = chrono::steady_clock::now();
|
||||
auto endTimepoint = std::chrono::steady_clock::now();
|
||||
auto highResStart = FloatingPointMicroseconds{m_StartTimepoint.time_since_epoch()};
|
||||
auto elapsedTime = chrono::time_point_cast <chrono::microseconds>(endTimepoint).time_since_epoch() - chrono::time_point_cast <chrono::microseconds>(m_StartTimepoint).time_since_epoch();
|
||||
auto elapsedTime = std::chrono::time_point_cast <std::chrono::microseconds>(endTimepoint).time_since_epoch() - std::chrono::time_point_cast <std::chrono::microseconds>(m_StartTimepoint).time_since_epoch();
|
||||
|
||||
Instrumentor::Get().WriteProfile({m_Name, highResStart, elapsedTime, this_thread::get_id()});
|
||||
Instrumentor::Get().WriteProfile({m_Name, highResStart, elapsedTime, std::this_thread::get_id()});
|
||||
|
||||
m_Stopped = true;
|
||||
}
|
||||
private:
|
||||
const char * m_Name;
|
||||
chrono::time_point <chrono::steady_clock> m_StartTimepoint;
|
||||
std::chrono::time_point <std::chrono::steady_clock> m_StartTimepoint;
|
||||
bool m_Stopped;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue