PerformanceProfiler
PerformanceProfiler.h

为穆棱等地区用户提供了全套网页设计制作服务,及穆棱网站建设行业解决方案。主营业务为成都做网站、网站设计、穆棱网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
#include#include #include
PerformanceProfiler.cpp
using namespace std;
#include "PerformanceProfiler.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//配置管理
int ConifgManager::SetOption(int flag)
{
int old = _flag;
_flag = flag;
return old;
}
int ConifgManager::GetOption()
{
return _flag;
}
void ConifgManager::AddOption(int flag)
{
_flag |= flag;
}
void ConifgManager::DelOption(int flag)
{
_flag &= (~flag);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//剖析节点
inline bool PPNode::operator<(const PPNode& node) const
{
return (_line < node._line) || (_filename < node._filename) || (_function < node._function);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//剖析段
void PPSection::Begin(int id)
{
lock_guard lock(_mtx);
//id用来分辨不同线程
if (_refCountMap[id]++ == 0)
{
_beginTimeMap[id] = clock();
}
_callCountMap[id]++;
}
void PPSection::End(int id)
{
lock_guard lock(_mtx);
if (--_refCountMap[id] == 0)
{
LongType costTime = clock() - _beginTimeMap[id];
_costTimeMap[id] += costTime;
_totalCostTime += costTime;
}
}
PPSection* PerformanceProfiler::CreateSection(const char* filename, const char* function, size_t line, const char* desc)
{
PPNode node(filename, function, line, desc);
PPSection*& section = _ppMap[node];
if (section == NULL)
{
section = new PPSection;
}
return section;
}
void PerformanceProfiler::OutPut()
{
int flag = ConifgManager::GetInstance()->GetOption();
if (flag&SAVE_TO_CONSOLE)
{
ConsoleSaveAdapter csa;
_OutPut(csa);
}
if (flag&SAVE_TO_FILE)
{
FileSaveAdapter fsa("PerformanceProfilerReport.txt");
_OutPut(fsa);
}
}
void PerformanceProfiler::_OutPut(SaveAdapter& sa)
{
vector vInfos;
int num = 1;
PPMap::iterator ppIt = _ppMap.begin();
while (ppIt != _ppMap.end())
{
vInfos.push_back(ppIt);
++ppIt;
}
struct SortByCostTime
{
bool operator()(PPMap::iterator left, PPMap::iterator right)
{
return left->second->_totalCostTime > right->second->_totalCostTime;
}
};
sort(vInfos.begin(), vInfos.end(), SortByCostTime());
vector::iterator it = vInfos.begin();
while (it != vInfos.end())
{
const PPNode& node = (*it)->first;
PPSection* section = (*it)->second;
sa.Save("NO.%d,Desc:%s\n", num++, node._desc.c_str());
sa.Save("Filename:%s,Function:%s,Line:%u\n", node._filename.c_str(), node._function.c_str(), node._line);
LongType totalCostTime = 0;
LongType totalCallCount = 0;
int id = 0;
LongType costTime = 0;
LongType callCount = 0;
StatisticsMap::iterator timeIt = section->_costTimeMap.begin();
while(timeIt!=section->_costTimeMap.end())
{
id = timeIt->first;
costTime = timeIt->second;
callCount = section->_callCountMap[id];
totalCostTime += costTime;
totalCallCount += callCount;
sa.Save("ThreadId:%d,CostTime:%.2f,callCount:%lld\n", id, (double)costTime, totalCallCount);
++it;
}
}
} test.cpp
#include "PerformanceProfiler.h"
//测试用例
//普通情况
///////////////////////////////////////////////////////////////////////////////////
//void Test1()
//{
// PPSection* section = PerformanceProfiler::GetInstance() \
// ->CreateSection(__FILE__, __FUNCTION__, __LINE__, "第一段代码");
//
// section->Begin();
// Sleep(5000);
// section->End();
//}
void Test2()
{
PERFORMANCE_PROFILER_EE_BEGIN(sql, "数据库");
Sleep(1000);
PERFORMANCE_PROFILER_EE_END(sql);
PERFORMANCE_PROFILER_EE_BEGIN(network, "网络");
Sleep(2000);
PERFORMANCE_PROFILER_EE_END(network);
}
//Test2调用3次
void TestN2()
{
for (int i = 3; i > 0; i--)
{
Test2();
}
}
///////////////////////////////////////////////////////////////////////////////////
//测试递归
LongType Fib(size_t n)
{
PERFORMANCE_PROFILER_EE_BEGIN(fib, "递归");
LongType ret = 0;
if (n < 2)
{
ret = n;
}
else
{
ret = Fib(n - 1) + Fib(n - 2);
}
PERFORMANCE_PROFILER_EE_END(fib);
return ret;
}
void TestN3()
{
PERFORMANCE_PROFILER_EE_BEGIN(fib, "FIB");
Fib(20);
PERFORMANCE_PROFILER_EE_END(fib);
}
//测试多线程
void ThreadRun(int count)
{
cout << this_thread::get_id() << endl;
while (count--)
{
PERFORMANCE_PROFILER_EE_BEGIN(ThreadRun, "ThreadRun");
this_thread::sleep_for(std::chrono::milliseconds(100));
PERFORMANCE_PROFILER_EE_END(ThreadRun);
}
}
void TestMhread()
{
cout << this_thread::get_id() << endl;
thread t1(ThreadRun, 15);
thread t2(ThreadRun, 10);
thread t3(ThreadRun, 5);
t1.join();
t2.join();
t3.join();
}
int main()
{
SET_CONFIG_OPTION(PERFORMANCE_PROFILER_EE | SAVE_TO_CONSOLE);
//Test2();
//TestN3();
TestMhread();
system("pause");
return 0;
}
网站栏目:PerformanceProfiler
URL分享:http://www.jxjierui.cn/article/ghodge.html


咨询
建站咨询
