fixed #468, Implement CCProfiler & CCProfilingTimer. And use them in PerformanceNodeChildrenTest.

This commit is contained in:
natural-law 2011-04-25 14:30:30 +08:00
parent 7337119501
commit 7231120b16
4 changed files with 148 additions and 36 deletions

View File

@ -26,9 +26,6 @@ THE SOFTWARE.
#if CC_ENABLE_PROFILERS #if CC_ENABLE_PROFILERS
#include <stdio.h>
#include <iostream>
namespace cocos2d namespace cocos2d
{ {
using namespace std; using namespace std;
@ -60,11 +57,17 @@ namespace cocos2d
{ {
CCProfiler *p = CCProfiler::sharedProfiler(); CCProfiler *p = CCProfiler::sharedProfiler();
p->m_pActiveTimers->removeObject(pTimer); p->m_pActiveTimers->removeObject(pTimer);
if (0 == (p->m_pActiveTimers->count()))
{
CC_SAFE_DELETE(g_sSharedProfiler);
}
} }
bool CCProfiler::init() bool CCProfiler::init()
{ {
m_pActiveTimers = new CCMutableArray<CCProfilingTimer*>(); m_pActiveTimers = CCArray::array();
m_pActiveTimers->retain();
return true; return true;
} }
@ -76,11 +79,13 @@ namespace cocos2d
void CCProfiler::displayTimers() void CCProfiler::displayTimers()
{ {
CCMutableArray<CCProfilingTimer*>::CCMutableArrayIterator it; CCObject* pObject = NULL;
for (it = m_pActiveTimers->begin(); it != m_pActiveTimers->end(); ++it) CCProfilingTimer* pTimer = NULL;
CCARRAY_FOREACH(m_pActiveTimers, pObject)
{ {
char *pszDescription = (*it)->description(); pTimer = (CCProfilingTimer*) pObject;
cout << pszDescription << endl; char *pszDescription = pTimer->description();
CCLog(pszDescription);
delete pszDescription; delete pszDescription;
} }
} }
@ -89,9 +94,10 @@ namespace cocos2d
bool CCProfilingTimer::initWithName(const char* pszTimerName, CCObject *pInstance) bool CCProfilingTimer::initWithName(const char* pszTimerName, CCObject *pInstance)
{ {
char tmp[100]; char tmp[160];
sprintf(tmp, "%s (0x%.8x)", pszTimerName, pInstance); sprintf(tmp, "%s (0x%.8x)", pszTimerName, pInstance);
m_NameStr = string(tmp); m_NameStr = string(tmp);
m_dAverageTime = 0.0;
return true; return true;
} }
@ -103,21 +109,21 @@ namespace cocos2d
char* CCProfilingTimer::description() char* CCProfilingTimer::description()
{ {
char *pszDes = new char[m_NameStr.length() + sizeof(double) + 20]; char *pszDes = new char[m_NameStr.length() + sizeof(double) + 32];
sprintf(pszDes, "%s: avg time, %fms", m_NameStr.c_str(), m_dAverageTime); sprintf(pszDes, "%s: avg time, %fms", m_NameStr.c_str(), m_dAverageTime);
return pszDes; return pszDes;
} }
void CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer) void CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer)
{ {
CCTime::gettimeofdayCocos2d(&pTimer->getStartTime(), NULL); CCTime::gettimeofdayCocos2d(pTimer->getStartTime(), NULL);
} }
void CCProfilingEndTimingBlock(CCProfilingTimer *pTimer) void CCProfilingEndTimingBlock(CCProfilingTimer *pTimer)
{ {
struct cc_timeval currentTime; struct cc_timeval currentTime;
CCTime::gettimeofdayCocos2d(&currentTime, NULL); CCTime::gettimeofdayCocos2d(&currentTime, NULL);
CCTime::timersubCocos2d(&currentTime, &pTimer->getStartTime(), &currentTime); CCTime::timersubCocos2d(&currentTime, pTimer->getStartTime(), &currentTime);
double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0; double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0;
// return in milliseconds // return in milliseconds

View File

@ -33,13 +33,13 @@ THE SOFTWARE.
#include "CCObject.h" #include "CCObject.h"
#include "platform/platform.h" #include "platform/platform.h"
#include "CCMutableArray.h" #include "CCArray.h"
namespace cocos2d namespace cocos2d
{ {
class CCProfilingTimer; class CCProfilingTimer;
class CCProfiler : public CCObject class CC_DLL CCProfiler : public CCObject
{ {
public: public:
~CCProfiler(void); ~CCProfiler(void);
@ -52,7 +52,7 @@ namespace cocos2d
static void releaseTimer(CCProfilingTimer *pTimer); static void releaseTimer(CCProfilingTimer *pTimer);
protected: protected:
CCMutableArray<CCProfilingTimer*> *m_pActiveTimers; CCArray *m_pActiveTimers;
}; };
class CCProfilingTimer : public CCObject class CCProfilingTimer : public CCObject
@ -61,7 +61,7 @@ namespace cocos2d
bool initWithName(const char* pszTimerName, CCObject *pInstance); bool initWithName(const char* pszTimerName, CCObject *pInstance);
~CCProfilingTimer(void); ~CCProfilingTimer(void);
char* description(void); char* description(void);
inline struct cc_timeval getStartTime(void) { return m_sStartTime; }; inline struct cc_timeval * getStartTime(void) { return &m_sStartTime; };
inline void setAverageTime(double value) { m_dAverageTime = value; } inline void setAverageTime(double value) { m_dAverageTime = value; }
inline double getAverageTime(void) { return m_dAverageTime; } inline double getAverageTime(void) { return m_dAverageTime; }
@ -71,8 +71,8 @@ namespace cocos2d
double m_dAverageTime; double m_dAverageTime;
}; };
void CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer); void CC_DLL CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer);
void CCProfilingEndTimingBlock(CCProfilingTimer *pTimer); void CC_DLL CCProfilingEndTimingBlock(CCProfilingTimer *pTimer);
} // end of namespace cocos2d } // end of namespace cocos2d

View File

@ -163,6 +163,14 @@ void NodeChildrenMainScene::updateQuantityLabel()
// IterateSpriteSheet // IterateSpriteSheet
// //
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
IterateSpriteSheet::~IterateSpriteSheet()
{
#if CC_ENABLE_PROFILERS
CCProfiler::releaseTimer(_profilingTimer);
_profilingTimer = NULL;
#endif
}
void IterateSpriteSheet::updateQuantityOfNodes() void IterateSpriteSheet::updateQuantityOfNodes()
{ {
CCSize s = CCDirector::sharedDirector()->getWinSize(); CCSize s = CCDirector::sharedDirector()->getWinSize();
@ -197,9 +205,19 @@ void IterateSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes)
addChild(batchNode); addChild(batchNode);
NodeChildrenMainScene::initWithQuantityOfNodes(nNodes); NodeChildrenMainScene::initWithQuantityOfNodes(nNodes);
#if CC_ENABLE_PROFILERS
_profilingTimer = CCProfiler::timerWithName(profilerName().c_str(), this);
#endif
scheduleUpdate(); scheduleUpdate();
} }
std::string IterateSpriteSheet::profilerName()
{
return "none";
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
// //
// IterateSpriteSheetFastEnum // IterateSpriteSheetFastEnum
@ -207,18 +225,23 @@ void IterateSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes)
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
void IterateSpriteSheetFastEnum::update(ccTime dt) void IterateSpriteSheetFastEnum::update(ccTime dt)
{ {
//CCProfilingBeginTimingBlock(_profilingTimer);
// iterate using fast enumeration protocol // iterate using fast enumeration protocol
CCArray* pChildren = batchNode->getChildren(); CCArray* pChildren = batchNode->getChildren();
CCObject* pObject = NULL; CCObject* pObject = NULL;
#if CC_ENABLE_PROFILERS
CCProfilingBeginTimingBlock(_profilingTimer);
#endif
CCARRAY_FOREACH(pChildren, pObject) CCARRAY_FOREACH(pChildren, pObject)
{ {
CCSprite* pSprite = (CCSprite*) pObject; CCSprite* pSprite = (CCSprite*) pObject;
pSprite->setIsVisible(false); pSprite->setIsVisible(false);
} }
//CCProfilingEndTimingBlock(_profilingTimer); #if CC_ENABLE_PROFILERS
CCProfilingEndTimingBlock(_profilingTimer);
#endif
} }
std::string IterateSpriteSheetFastEnum::title() std::string IterateSpriteSheetFastEnum::title()
@ -227,6 +250,11 @@ std::string IterateSpriteSheetFastEnum::title()
} }
std::string IterateSpriteSheetFastEnum::subtitle() std::string IterateSpriteSheetFastEnum::subtitle()
{
return "Iterate children using Fast Enum API. See console";
}
std::string IterateSpriteSheetFastEnum::profilerName()
{ {
return "iter fast enum"; return "iter fast enum";
} }
@ -241,11 +269,20 @@ void IterateSpriteSheetCArray::update(ccTime dt)
// iterate using fast enumeration protocol // iterate using fast enumeration protocol
CCArray* pChildren = batchNode->getChildren(); CCArray* pChildren = batchNode->getChildren();
CCObject* pObject = NULL; CCObject* pObject = NULL;
#if CC_ENABLE_PROFILERS
CCProfilingBeginTimingBlock(_profilingTimer);
#endif
CCARRAY_FOREACH(pChildren, pObject) CCARRAY_FOREACH(pChildren, pObject)
{ {
CCSprite* pSprite = (CCSprite*)pObject; CCSprite* pSprite = (CCSprite*)pObject;
pSprite->setIsVisible(false); pSprite->setIsVisible(false); }
} }
#if CC_ENABLE_PROFILERS
CCProfilingEndTimingBlock(_profilingTimer);
#endif
}
std::string IterateSpriteSheetCArray::title() std::string IterateSpriteSheetCArray::title()
{ {
@ -257,17 +294,35 @@ std::string IterateSpriteSheetCArray::subtitle()
return "Iterate children using C Array API. See console"; return "Iterate children using C Array API. See console";
} }
std::string IterateSpriteSheetCArray::profilerName()
{
return "iter c-array";
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
// //
// AddRemoveSpriteSheet // AddRemoveSpriteSheet
// //
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
AddRemoveSpriteSheet::~AddRemoveSpriteSheet()
{
#if CC_ENABLE_PROFILERS
CCProfiler::releaseTimer(_profilingTimer);
_profilingTimer = NULL;
#endif
}
void AddRemoveSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes) void AddRemoveSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes)
{ {
batchNode = CCSpriteBatchNode::batchNodeWithFile("Images/spritesheet1.png"); batchNode = CCSpriteBatchNode::batchNodeWithFile("Images/spritesheet1.png");
addChild(batchNode); addChild(batchNode);
NodeChildrenMainScene::initWithQuantityOfNodes(nNodes); NodeChildrenMainScene::initWithQuantityOfNodes(nNodes);
#if CC_ENABLE_PROFILERS
_profilingTimer = CCProfiler::timerWithName(profilerName().c_str(), this);
#endif
scheduleUpdate(); scheduleUpdate();
} }
@ -299,6 +354,11 @@ void AddRemoveSpriteSheet::updateQuantityOfNodes()
currentQuantityOfNodes = quantityOfNodes; currentQuantityOfNodes = quantityOfNodes;
} }
std::string AddRemoveSpriteSheet::profilerName()
{
return "none";
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
// //
// AddSpriteSheet // AddSpriteSheet
@ -326,13 +386,18 @@ void AddSpriteSheet::update(ccTime dt)
} }
// add them with random Z (very important!) // add them with random Z (very important!)
// CCProfilingBeginTimingBlock(_profilingTimer); #if CC_ENABLE_PROFILERS
CCProfilingBeginTimingBlock(_profilingTimer);
#endif
for( int i=0; i < totalToAdd;i++ ) for( int i=0; i < totalToAdd;i++ )
{ {
batchNode->addChild((CCNode*) (sprites->objectAtIndex(i)), zs[i], kTagBase+i); batchNode->addChild((CCNode*) (sprites->objectAtIndex(i)), zs[i], kTagBase+i);
} }
// [batchNode sortAllChildren];
// CCProfilingEndTimingBlock(_profilingTimer); #if CC_ENABLE_PROFILERS
CCProfilingEndTimingBlock(_profilingTimer);
#endif
// remove them // remove them
for( int i=0;i < totalToAdd;i++) for( int i=0;i < totalToAdd;i++)
@ -354,6 +419,10 @@ std::string AddSpriteSheet::subtitle()
return "Adds %10 of total sprites with random z. See console"; return "Adds %10 of total sprites with random z. See console";
} }
std::string AddSpriteSheet::profilerName()
{
return "add sprites";
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
// //
@ -385,12 +454,18 @@ void RemoveSpriteSheet::update(ccTime dt)
} }
// remove them // remove them
// CCProfilingBeginTimingBlock(_profilingTimer); #if CC_ENABLE_PROFILERS
CCProfilingBeginTimingBlock(_profilingTimer);
#endif
for( int i=0;i < totalToAdd;i++) for( int i=0;i < totalToAdd;i++)
{ {
batchNode->removeChildByTag(kTagBase+i, true); batchNode->removeChildByTag(kTagBase+i, true);
} }
// CCProfilingEndTimingBlock(_profilingTimer);
#if CC_ENABLE_PROFILERS
CCProfilingEndTimingBlock(_profilingTimer);
#endif
} }
} }
@ -404,6 +479,11 @@ std::string RemoveSpriteSheet::subtitle()
return "Remove %10 of total sprites placed randomly. See console"; return "Remove %10 of total sprites placed randomly. See console";
} }
std::string RemoveSpriteSheet::profilerName()
{
return "remove sprites";
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
// //
// ReorderSpriteSheet // ReorderSpriteSheet
@ -436,14 +516,19 @@ void ReorderSpriteSheet::update(ccTime dt)
// [batchNode sortAllChildren]; // [batchNode sortAllChildren];
// reorder them // reorder them
//CCProfilingBeginTimingBlock(_profilingTimer); #if CC_ENABLE_PROFILERS
CCProfilingBeginTimingBlock(_profilingTimer);
#endif
for( int i=0;i < totalToAdd;i++) for( int i=0;i < totalToAdd;i++)
{ {
CCNode* pNode = (CCNode*) (batchNode->getChildren()->objectAtIndex(i)); CCNode* pNode = (CCNode*) (batchNode->getChildren()->objectAtIndex(i));
batchNode->reorderChild(pNode, CCRANDOM_MINUS1_1() * 50); batchNode->reorderChild(pNode, CCRANDOM_MINUS1_1() * 50);
} }
// [batchNode sortAllChildren];
//CCProfilingEndTimingBlock(_profilingTimer); #if CC_ENABLE_PROFILERS
CCProfilingEndTimingBlock(_profilingTimer);
#endif
// remove them // remove them
for( int i=0;i < totalToAdd;i++) for( int i=0;i < totalToAdd;i++)
@ -463,6 +548,11 @@ std::string ReorderSpriteSheet::subtitle()
return "Reorder %10 of total sprites placed randomly. See console"; return "Reorder %10 of total sprites placed randomly. See console";
} }
std::string ReorderSpriteSheet::profilerName()
{
return "reorder sprites";
}
void runNodeChildrenTest() void runNodeChildrenTest()
{ {
IterateSpriteSheet* pScene = new IterateSpriteSheetCArray(); IterateSpriteSheet* pScene = new IterateSpriteSheetCArray();

View File

@ -2,6 +2,7 @@
#define __PERFORMANCE_NODE_CHILDREN_TEST_H__ #define __PERFORMANCE_NODE_CHILDREN_TEST_H__
#include "PerformanceTest.h" #include "PerformanceTest.h"
#include "support/CCProfiling.h"
class NodeChildrenMenuLayer : public PerformBasicLayer class NodeChildrenMenuLayer : public PerformBasicLayer
{ {
@ -33,13 +34,18 @@ protected:
class IterateSpriteSheet : public NodeChildrenMainScene class IterateSpriteSheet : public NodeChildrenMainScene
{ {
public: public:
~IterateSpriteSheet();
virtual void updateQuantityOfNodes(); virtual void updateQuantityOfNodes();
virtual void initWithQuantityOfNodes(unsigned int nNodes); virtual void initWithQuantityOfNodes(unsigned int nNodes);
virtual void update(ccTime dt) = 0; virtual void update(ccTime dt) = 0;
virtual std::string profilerName();
protected: protected:
CCSpriteBatchNode *batchNode; CCSpriteBatchNode *batchNode;
//CCProfilingTimer *_profilingTimer;
#if CC_ENABLE_PROFILERS
CCProfilingTimer *_profilingTimer;
#endif
}; };
class IterateSpriteSheetFastEnum : public IterateSpriteSheet class IterateSpriteSheetFastEnum : public IterateSpriteSheet
@ -49,6 +55,7 @@ public:
virtual std::string title(); virtual std::string title();
virtual std::string subtitle(); virtual std::string subtitle();
virtual std::string profilerName();
}; };
class IterateSpriteSheetCArray : public IterateSpriteSheet class IterateSpriteSheetCArray : public IterateSpriteSheet
@ -58,18 +65,24 @@ public:
virtual std::string title(); virtual std::string title();
virtual std::string subtitle(); virtual std::string subtitle();
virtual std::string profilerName();
}; };
class AddRemoveSpriteSheet : public NodeChildrenMainScene class AddRemoveSpriteSheet : public NodeChildrenMainScene
{ {
public: public:
~AddRemoveSpriteSheet();
virtual void updateQuantityOfNodes(); virtual void updateQuantityOfNodes();
virtual void initWithQuantityOfNodes(unsigned int nNodes); virtual void initWithQuantityOfNodes(unsigned int nNodes);
virtual void update(ccTime dt) = 0; virtual void update(ccTime dt) = 0;
virtual std::string profilerName();
protected: protected:
CCSpriteBatchNode *batchNode; CCSpriteBatchNode *batchNode;
//CCProfilingTimer* _profilingTimer;
#if CC_ENABLE_PROFILERS
CCProfilingTimer* _profilingTimer;
#endif
}; };
class AddSpriteSheet : public AddRemoveSpriteSheet class AddSpriteSheet : public AddRemoveSpriteSheet
@ -79,6 +92,7 @@ public:
virtual std::string title(); virtual std::string title();
virtual std::string subtitle(); virtual std::string subtitle();
virtual std::string profilerName();
}; };
class RemoveSpriteSheet : public AddRemoveSpriteSheet class RemoveSpriteSheet : public AddRemoveSpriteSheet
@ -88,6 +102,7 @@ public:
virtual std::string title(); virtual std::string title();
virtual std::string subtitle(); virtual std::string subtitle();
virtual std::string profilerName();
}; };
class ReorderSpriteSheet : public AddRemoveSpriteSheet class ReorderSpriteSheet : public AddRemoveSpriteSheet
@ -97,6 +112,7 @@ public:
virtual std::string title(); virtual std::string title();
virtual std::string subtitle(); virtual std::string subtitle();
virtual std::string profilerName();
}; };
void runNodeChildrenTest(); void runNodeChildrenTest();