From 7231120b16564a1ed673d940ec7c3fc1c0444624 Mon Sep 17 00:00:00 2001 From: natural-law Date: Mon, 25 Apr 2011 14:30:30 +0800 Subject: [PATCH] fixed #468, Implement CCProfiler & CCProfilingTimer. And use them in PerformanceNodeChildrenTest. --- cocos2dx/support/CCProfiling.cpp | 30 +++-- cocos2dx/support/CCProfiling.h | 12 +- .../PerformanceNodeChildrenTest.cpp | 122 +++++++++++++++--- .../PerformanceNodeChildrenTest.h | 20 ++- 4 files changed, 148 insertions(+), 36 deletions(-) diff --git a/cocos2dx/support/CCProfiling.cpp b/cocos2dx/support/CCProfiling.cpp index a496f65030..e3931f9516 100644 --- a/cocos2dx/support/CCProfiling.cpp +++ b/cocos2dx/support/CCProfiling.cpp @@ -26,9 +26,6 @@ THE SOFTWARE. #if CC_ENABLE_PROFILERS -#include -#include - namespace cocos2d { using namespace std; @@ -60,11 +57,17 @@ namespace cocos2d { CCProfiler *p = CCProfiler::sharedProfiler(); p->m_pActiveTimers->removeObject(pTimer); + + if (0 == (p->m_pActiveTimers->count())) + { + CC_SAFE_DELETE(g_sSharedProfiler); + } } bool CCProfiler::init() { - m_pActiveTimers = new CCMutableArray(); + m_pActiveTimers = CCArray::array(); + m_pActiveTimers->retain(); return true; } @@ -76,11 +79,13 @@ namespace cocos2d void CCProfiler::displayTimers() { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pActiveTimers->begin(); it != m_pActiveTimers->end(); ++it) + CCObject* pObject = NULL; + CCProfilingTimer* pTimer = NULL; + CCARRAY_FOREACH(m_pActiveTimers, pObject) { - char *pszDescription = (*it)->description(); - cout << pszDescription << endl; + pTimer = (CCProfilingTimer*) pObject; + char *pszDescription = pTimer->description(); + CCLog(pszDescription); delete pszDescription; } } @@ -89,9 +94,10 @@ namespace cocos2d bool CCProfilingTimer::initWithName(const char* pszTimerName, CCObject *pInstance) { - char tmp[100]; + char tmp[160]; sprintf(tmp, "%s (0x%.8x)", pszTimerName, pInstance); m_NameStr = string(tmp); + m_dAverageTime = 0.0; return true; } @@ -103,21 +109,21 @@ namespace cocos2d 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); return pszDes; } void CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer) { - CCTime::gettimeofdayCocos2d(&pTimer->getStartTime(), NULL); + CCTime::gettimeofdayCocos2d(pTimer->getStartTime(), NULL); } void CCProfilingEndTimingBlock(CCProfilingTimer *pTimer) { struct cc_timeval currentTime; CCTime::gettimeofdayCocos2d(¤tTime, NULL); - CCTime::timersubCocos2d(¤tTime, &pTimer->getStartTime(), ¤tTime); + CCTime::timersubCocos2d(¤tTime, pTimer->getStartTime(), ¤tTime); double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0; // return in milliseconds diff --git a/cocos2dx/support/CCProfiling.h b/cocos2dx/support/CCProfiling.h index f1be7b7e0b..66b7c619ea 100644 --- a/cocos2dx/support/CCProfiling.h +++ b/cocos2dx/support/CCProfiling.h @@ -33,13 +33,13 @@ THE SOFTWARE. #include "CCObject.h" #include "platform/platform.h" -#include "CCMutableArray.h" +#include "CCArray.h" namespace cocos2d { class CCProfilingTimer; - class CCProfiler : public CCObject + class CC_DLL CCProfiler : public CCObject { public: ~CCProfiler(void); @@ -52,7 +52,7 @@ namespace cocos2d static void releaseTimer(CCProfilingTimer *pTimer); protected: - CCMutableArray *m_pActiveTimers; + CCArray *m_pActiveTimers; }; class CCProfilingTimer : public CCObject @@ -61,7 +61,7 @@ namespace cocos2d bool initWithName(const char* pszTimerName, CCObject *pInstance); ~CCProfilingTimer(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 double getAverageTime(void) { return m_dAverageTime; } @@ -71,8 +71,8 @@ namespace cocos2d double m_dAverageTime; }; - void CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer); - void CCProfilingEndTimingBlock(CCProfilingTimer *pTimer); + void CC_DLL CCProfilingBeginTimingBlock(CCProfilingTimer *pTimer); + void CC_DLL CCProfilingEndTimingBlock(CCProfilingTimer *pTimer); } // end of namespace cocos2d diff --git a/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp b/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp index f5643bca15..852b4ffe4f 100644 --- a/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp +++ b/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp @@ -163,6 +163,14 @@ void NodeChildrenMainScene::updateQuantityLabel() // IterateSpriteSheet // //////////////////////////////////////////////////////// +IterateSpriteSheet::~IterateSpriteSheet() +{ +#if CC_ENABLE_PROFILERS + CCProfiler::releaseTimer(_profilingTimer); + _profilingTimer = NULL; +#endif +} + void IterateSpriteSheet::updateQuantityOfNodes() { CCSize s = CCDirector::sharedDirector()->getWinSize(); @@ -197,9 +205,19 @@ void IterateSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes) addChild(batchNode); NodeChildrenMainScene::initWithQuantityOfNodes(nNodes); + +#if CC_ENABLE_PROFILERS + _profilingTimer = CCProfiler::timerWithName(profilerName().c_str(), this); +#endif + scheduleUpdate(); } +std::string IterateSpriteSheet::profilerName() +{ + return "none"; +} + //////////////////////////////////////////////////////// // // IterateSpriteSheetFastEnum @@ -207,18 +225,23 @@ void IterateSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes) //////////////////////////////////////////////////////// void IterateSpriteSheetFastEnum::update(ccTime dt) { - //CCProfilingBeginTimingBlock(_profilingTimer); - // iterate using fast enumeration protocol CCArray* pChildren = batchNode->getChildren(); CCObject* pObject = NULL; + +#if CC_ENABLE_PROFILERS + CCProfilingBeginTimingBlock(_profilingTimer); +#endif + CCARRAY_FOREACH(pChildren, pObject) { CCSprite* pSprite = (CCSprite*) pObject; pSprite->setIsVisible(false); } - //CCProfilingEndTimingBlock(_profilingTimer); +#if CC_ENABLE_PROFILERS + CCProfilingEndTimingBlock(_profilingTimer); +#endif } std::string IterateSpriteSheetFastEnum::title() @@ -227,6 +250,11 @@ std::string IterateSpriteSheetFastEnum::title() } std::string IterateSpriteSheetFastEnum::subtitle() +{ + return "Iterate children using Fast Enum API. See console"; +} + +std::string IterateSpriteSheetFastEnum::profilerName() { return "iter fast enum"; } @@ -241,11 +269,20 @@ void IterateSpriteSheetCArray::update(ccTime dt) // iterate using fast enumeration protocol CCArray* pChildren = batchNode->getChildren(); CCObject* pObject = NULL; - CCARRAY_FOREACH(pChildren, pObject) - { - CCSprite* pSprite = (CCSprite*)pObject; - pSprite->setIsVisible(false); - } } + +#if CC_ENABLE_PROFILERS + CCProfilingBeginTimingBlock(_profilingTimer); +#endif + + CCARRAY_FOREACH(pChildren, pObject) + { + CCSprite* pSprite = (CCSprite*)pObject; + pSprite->setIsVisible(false); } + +#if CC_ENABLE_PROFILERS + CCProfilingEndTimingBlock(_profilingTimer); +#endif +} std::string IterateSpriteSheetCArray::title() { @@ -257,17 +294,35 @@ std::string IterateSpriteSheetCArray::subtitle() return "Iterate children using C Array API. See console"; } +std::string IterateSpriteSheetCArray::profilerName() +{ + return "iter c-array"; +} + //////////////////////////////////////////////////////// // // AddRemoveSpriteSheet // //////////////////////////////////////////////////////// +AddRemoveSpriteSheet::~AddRemoveSpriteSheet() +{ +#if CC_ENABLE_PROFILERS + CCProfiler::releaseTimer(_profilingTimer); + _profilingTimer = NULL; +#endif +} + void AddRemoveSpriteSheet::initWithQuantityOfNodes(unsigned int nNodes) { batchNode = CCSpriteBatchNode::batchNodeWithFile("Images/spritesheet1.png"); addChild(batchNode); NodeChildrenMainScene::initWithQuantityOfNodes(nNodes); + +#if CC_ENABLE_PROFILERS + _profilingTimer = CCProfiler::timerWithName(profilerName().c_str(), this); +#endif + scheduleUpdate(); } @@ -299,6 +354,11 @@ void AddRemoveSpriteSheet::updateQuantityOfNodes() currentQuantityOfNodes = quantityOfNodes; } +std::string AddRemoveSpriteSheet::profilerName() +{ + return "none"; +} + //////////////////////////////////////////////////////// // // AddSpriteSheet @@ -326,13 +386,18 @@ void AddSpriteSheet::update(ccTime dt) } // add them with random Z (very important!) - // CCProfilingBeginTimingBlock(_profilingTimer); +#if CC_ENABLE_PROFILERS + CCProfilingBeginTimingBlock(_profilingTimer); +#endif + for( int i=0; i < totalToAdd;i++ ) { batchNode->addChild((CCNode*) (sprites->objectAtIndex(i)), zs[i], kTagBase+i); } - // [batchNode sortAllChildren]; - // CCProfilingEndTimingBlock(_profilingTimer); + +#if CC_ENABLE_PROFILERS + CCProfilingEndTimingBlock(_profilingTimer); +#endif // remove them 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"; } +std::string AddSpriteSheet::profilerName() +{ + return "add sprites"; +} //////////////////////////////////////////////////////// // @@ -385,12 +454,18 @@ void RemoveSpriteSheet::update(ccTime dt) } // remove them - // CCProfilingBeginTimingBlock(_profilingTimer); +#if CC_ENABLE_PROFILERS + CCProfilingBeginTimingBlock(_profilingTimer); +#endif + for( int i=0;i < totalToAdd;i++) { 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"; } +std::string RemoveSpriteSheet::profilerName() +{ + return "remove sprites"; +} + //////////////////////////////////////////////////////// // // ReorderSpriteSheet @@ -436,14 +516,19 @@ void ReorderSpriteSheet::update(ccTime dt) // [batchNode sortAllChildren]; // reorder them - //CCProfilingBeginTimingBlock(_profilingTimer); +#if CC_ENABLE_PROFILERS + CCProfilingBeginTimingBlock(_profilingTimer); +#endif + for( int i=0;i < totalToAdd;i++) { CCNode* pNode = (CCNode*) (batchNode->getChildren()->objectAtIndex(i)); batchNode->reorderChild(pNode, CCRANDOM_MINUS1_1() * 50); } - // [batchNode sortAllChildren]; - //CCProfilingEndTimingBlock(_profilingTimer); + +#if CC_ENABLE_PROFILERS + CCProfilingEndTimingBlock(_profilingTimer); +#endif // remove them 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"; } +std::string ReorderSpriteSheet::profilerName() +{ + return "reorder sprites"; +} + void runNodeChildrenTest() { IterateSpriteSheet* pScene = new IterateSpriteSheetCArray(); diff --git a/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.h b/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.h index f9917cd316..d34912ce1a 100644 --- a/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.h +++ b/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.h @@ -2,6 +2,7 @@ #define __PERFORMANCE_NODE_CHILDREN_TEST_H__ #include "PerformanceTest.h" +#include "support/CCProfiling.h" class NodeChildrenMenuLayer : public PerformBasicLayer { @@ -33,13 +34,18 @@ protected: class IterateSpriteSheet : public NodeChildrenMainScene { public: + ~IterateSpriteSheet(); virtual void updateQuantityOfNodes(); virtual void initWithQuantityOfNodes(unsigned int nNodes); virtual void update(ccTime dt) = 0; + virtual std::string profilerName(); protected: CCSpriteBatchNode *batchNode; - //CCProfilingTimer *_profilingTimer; + +#if CC_ENABLE_PROFILERS + CCProfilingTimer *_profilingTimer; +#endif }; class IterateSpriteSheetFastEnum : public IterateSpriteSheet @@ -49,6 +55,7 @@ public: virtual std::string title(); virtual std::string subtitle(); + virtual std::string profilerName(); }; class IterateSpriteSheetCArray : public IterateSpriteSheet @@ -58,18 +65,24 @@ public: virtual std::string title(); virtual std::string subtitle(); + virtual std::string profilerName(); }; class AddRemoveSpriteSheet : public NodeChildrenMainScene { public: + ~AddRemoveSpriteSheet(); virtual void updateQuantityOfNodes(); virtual void initWithQuantityOfNodes(unsigned int nNodes); virtual void update(ccTime dt) = 0; + virtual std::string profilerName(); protected: CCSpriteBatchNode *batchNode; - //CCProfilingTimer* _profilingTimer; + +#if CC_ENABLE_PROFILERS + CCProfilingTimer* _profilingTimer; +#endif }; class AddSpriteSheet : public AddRemoveSpriteSheet @@ -79,6 +92,7 @@ public: virtual std::string title(); virtual std::string subtitle(); + virtual std::string profilerName(); }; class RemoveSpriteSheet : public AddRemoveSpriteSheet @@ -88,6 +102,7 @@ public: virtual std::string title(); virtual std::string subtitle(); + virtual std::string profilerName(); }; class ReorderSpriteSheet : public AddRemoveSpriteSheet @@ -97,6 +112,7 @@ public: virtual std::string title(); virtual std::string subtitle(); + virtual std::string profilerName(); }; void runNodeChildrenTest();