issue #2792 : Performance Test: Sprite drawing

This commit is contained in:
samuelhu 2013-09-30 16:22:55 +08:00
parent 8989e5ce58
commit 6aa18423e6
3 changed files with 312 additions and 9 deletions

View File

@ -353,6 +353,11 @@ public:
void setActionManager(ActionManager* actionManager);
/* Gets delta time since last tick to main loop */
float getDeltaTime() const;
/**
* get Frame Rate
*/
float getFrameRate() const { return _frameRate; }
protected:
void purgeDirector();

View File

@ -10,11 +10,10 @@ enum {
enum {
kTagInfoLayer = 1,
kTagMainLayer = 2,
kTagAutoTestMenu = 3,
kTagMenuLayer = (kMaxNodes + 1000),
};
static int s_nSpriteCurCase = 0;
////////////////////////////////////////////////////////
//
// SubTest
@ -227,6 +226,37 @@ void SubTest::removeByTag(int tag)
// SpriteMenuLayer
//
////////////////////////////////////////////////////////
void SpriteMenuLayer::restartCallback(Object* sender)
{
if ( SpriteMainScene::_s_autoTest )
{
log("It's auto sprite performace testing,so this operation is invalid");
return;
}
PerformBasicLayer::restartCallback(sender);
}
void SpriteMenuLayer::nextCallback(Object* sender)
{
if ( SpriteMainScene::_s_autoTest )
{
log("It's auto sprite performace testing,so this operation is invalid");
return;
}
PerformBasicLayer::nextCallback(sender);
}
void SpriteMenuLayer::backCallback(Object* sender)
{
if ( SpriteMainScene::_s_autoTest )
{
log("It's auto sprite performace testing,so this operation is invalid");
return;
}
PerformBasicLayer::backCallback(sender);
}
void SpriteMenuLayer::showCurrentTest()
{
SpriteMainScene* scene = NULL;
@ -258,7 +288,8 @@ void SpriteMenuLayer::showCurrentTest()
scene = new SpritePerformTest7;
break;
}
s_nSpriteCurCase = _curCase;
SpriteMainScene::_s_nSpriteCurCase = _curCase;
if (scene)
{
@ -267,12 +298,15 @@ void SpriteMenuLayer::showCurrentTest()
scene->release();
}
}
////////////////////////////////////////////////////////
//
// SpriteMainScene
//
////////////////////////////////////////////////////////
bool SpriteMainScene::_s_autoTest = false;
int SpriteMainScene::_s_nSpriteCurCase = 0;
void SpriteMainScene::initWithSubTest(int asubtest, int nNodes)
{
//srandom(0);
@ -303,10 +337,33 @@ void SpriteMainScene::initWithSubTest(int asubtest, int nNodes)
addChild(infoLabel, 1, kTagInfoLayer);
// add menu
auto menuLayer = new SpriteMenuLayer(true, TEST_COUNT, s_nSpriteCurCase);
auto menuLayer = new SpriteMenuLayer(true, TEST_COUNT, SpriteMainScene::_s_nSpriteCurCase);
addChild(menuLayer, 1, kTagMenuLayer);
menuLayer->release();
/**
* auto test menu
*/
auto menuAutoTest = Menu::create();
menuAutoTest->setPosition( Point::ZERO );
MenuItemFont::setFontName("Arial");
MenuItemFont::setFontSize(24);
MenuItemFont* autoTestItem = NULL;
if (SpriteMainScene::_s_autoTest)
{
autoTestItem = MenuItemFont::create("Auto Test On",CC_CALLBACK_1(SpriteMainScene::onAutoTest, this));
}
else
{
autoTestItem = MenuItemFont::create("Auto Test Off",CC_CALLBACK_1(SpriteMainScene::onAutoTest, this));
}
autoTestItem->setTag(1);
autoTestItem->setPosition(Point( s.width - 90, s.height / 2));
menuAutoTest->addChild(autoTestItem);
addChild( menuAutoTest, 3, kTagAutoTestMenu );
// Sub Tests
MenuItemFont::setFontSize(32);
auto subMenu = Menu::create();
@ -356,6 +413,12 @@ SpriteMainScene::~SpriteMainScene()
void SpriteMainScene::testNCallback(Object* sender)
{
if (SpriteMainScene::_s_autoTest)
{
log("It's auto sprite performace testing,so this operation is invalid");
return;
}
subtestNumber = static_cast<MenuItemFont*>(sender)->getTag();
auto menu = static_cast<SpriteMenuLayer*>( getChildByTag(kTagMenuLayer) );
menu->restartCallback(sender);
@ -375,7 +438,7 @@ void SpriteMainScene::updateNodes()
}
void SpriteMainScene::onIncrease(Object* sender)
{
{
if( quantityNodes >= kMaxNodes)
return;
@ -391,6 +454,7 @@ void SpriteMainScene::onIncrease(Object* sender)
void SpriteMainScene::onDecrease(Object* sender)
{
if( quantityNodes <= 0 )
return;
@ -403,6 +467,216 @@ void SpriteMainScene::onDecrease(Object* sender)
updateNodes();
}
void SpriteMainScene::dumpProfilerFPS()
{
if (_vecFPS.empty())
{
log("Error: the FPS vector is empty");
return;
}
auto iter = _vecFPS.begin();
float minFPS = *iter;
float maxFPS = *iter;
float totalFPS = 0.0f;
float averagerFPS = 0.0f;
for (; iter != _vecFPS.end(); ++iter)
{
if (minFPS > *iter)
{
minFPS = *iter;
}
if (maxFPS < *iter)
{
maxFPS = *iter;
}
totalFPS += *iter;
}
averagerFPS = totalFPS / _vecFPS.size();
log("Cur test: %d, cur sub item :%d,cur sprite nums:%d, the min FPS value is %.1f,the max FPS value is %.1f,the averager FPS is %.1f", SpriteMainScene::_s_nSpriteCurCase, subtestNumber, quantityNodes, minFPS, maxFPS, averagerFPS);
}
void SpriteMainScene::updateAutoTest(float dt)
{
if (SpriteMainScene::_s_autoTest)
{
_executeTimes += 1;
_vecFPS.push_back(Director::getInstance()->getFrameRate());
if ( _executeTimes >= SpriteMainScene::MAX_AUTO_TEST_TIMES )
{
dumpProfilerFPS();
nextAutoTest();
}
}
}
void SpriteMainScene::onEnter()
{
Scene::onEnter();
if ( SpriteMainScene::_s_autoTest )
{
_vecFPS.clear();
_executeTimes = 0;
auto director = Director::getInstance();
auto sched = director->getScheduler();
sched->scheduleSelector(SEL_SCHEDULE(&SpriteMainScene::updateAutoTest), this, 0.2, false);
}
}
void SpriteMainScene::onExit()
{
if ( SpriteMainScene::_s_autoTest )
{
auto director = Director::getInstance();
auto sched = director->getScheduler();
sched->unscheduleSelector(SEL_SCHEDULE(&SpriteMainScene::updateAutoTest), this );
}
Scene::onExit();
}
void SpriteMainScene::autoShowSpriteTests(int curCase, int subTest,int nodes)
{
SpriteMainScene* scene = NULL;
switch (curCase)
{
case 0:
scene = new SpritePerformTest1;
break;
case 1:
scene = new SpritePerformTest2;
break;
case 2:
scene = new SpritePerformTest3;
break;
case 3:
scene = new SpritePerformTest4;
break;
case 4:
scene = new SpritePerformTest5;
break;
case 5:
scene = new SpritePerformTest6;
break;
case 6:
scene = new SpritePerformTest7;
break;
}
SpriteMainScene::_s_nSpriteCurCase = curCase;
if (scene)
{
scene->initWithSubTest(subTest, nodes);
Director::getInstance()->replaceScene(scene);
scene->release();
}
}
void SpriteMainScene::beginAutoTest()
{
if (0 != SpriteMainScene::_s_nSpriteCurCase)
{
SpriteMainScene::_s_nSpriteCurCase = 0;
}
auto scene = new SpritePerformTest1;
scene->initWithSubTest(1, 500);
Director::getInstance()->replaceScene(scene);
scene->release();
}
void SpriteMainScene::endAutoTest()
{
SpriteMainScene::_s_autoTest = false;
auto director = Director::getInstance();
auto sched = director->getScheduler();
sched->unscheduleSelector( SEL_SCHEDULE( &SpriteMainScene::updateAutoTest ), this );
}
void SpriteMainScene::nextAutoTest()
{
if ( SpriteMainScene::_s_nSpriteCurCase < SpriteMainScene::MAX_SPRITE_TEST_CASE )
{
if ( subtestNumber < SpriteMainScene::MAX_SUB_TEST_NUMS )
{
subtestNumber += 1;
autoShowSpriteTests(SpriteMainScene::_s_nSpriteCurCase, subtestNumber, quantityNodes);
}
else if ( subtestNumber == SpriteMainScene::MAX_SUB_TEST_NUMS )
{
if (quantityNodes == SpriteMainScene::AUTO_TEST_NODE_NUM1)
{
autoShowSpriteTests(SpriteMainScene::_s_nSpriteCurCase, 1, SpriteMainScene::AUTO_TEST_NODE_NUM2);
}
else
{
if (SpriteMainScene::_s_nSpriteCurCase + 1 < SpriteMainScene::MAX_SPRITE_TEST_CASE)
{
SpriteMainScene::_s_nSpriteCurCase += 1;
autoShowSpriteTests(SpriteMainScene::_s_nSpriteCurCase, 1, SpriteMainScene::AUTO_TEST_NODE_NUM1);
}
else
{
finishAutoTest();
}
}
}
}
}
void SpriteMainScene::finishAutoTest()
{
SpriteMainScene::_s_autoTest = false;
auto director = Director::getInstance();
auto sched = director->getScheduler();
sched->unscheduleSelector( SEL_SCHEDULE( &SpriteMainScene::updateAutoTest ), this);
auto autoTestMenu = dynamic_cast<Menu*>(getChildByTag(kTagAutoTestMenu));
if (nullptr != autoTestMenu)
{
auto menuItemFont = dynamic_cast<MenuItemFont*>(autoTestMenu->getChildByTag(1));
if (nullptr != menuItemFont)
{
menuItemFont->setString("Auto Test finish");
}
}
log("Sprite performance test is finish ");
}
void SpriteMainScene::onAutoTest(Object* sender)
{
SpriteMainScene::_s_autoTest = !SpriteMainScene::_s_autoTest;
MenuItemFont* menuItem = dynamic_cast<MenuItemFont*>(sender);
if (nullptr != menuItem)
{
if (SpriteMainScene::_s_autoTest)
{
menuItem->setString("Auto Test On");
beginAutoTest();
}
else
{
menuItem->setString("Auto Test Off");
endAutoTest();
}
}
}
////////////////////////////////////////////////////////
//
// For test functions
@ -498,7 +772,6 @@ void SpritePerformTest1::doTest(Sprite* sprite)
{
performancePosition(sprite);
}
////////////////////////////////////////////////////////
//
// SpritePerformTest2
@ -609,6 +882,7 @@ void SpritePerformTest7::doTest(Sprite* sprite)
void runSpriteTest()
{
SpriteMainScene::_s_autoTest = false;
auto scene = new SpritePerformTest1;
scene->initWithSubTest(1, 50);
Director::getInstance()->replaceScene(scene);

View File

@ -25,6 +25,9 @@ public:
{
}
virtual void restartCallback(Object* sender);
virtual void nextCallback(Object* sender);
virtual void backCallback(Object* sender);
virtual void showCurrentTest();
};
@ -45,12 +48,33 @@ public:
int getSubTestNum() { return subtestNumber; }
int getNodesNum() { return quantityNodes; }
virtual void onEnter();
virtual void onExit();
void updateAutoTest(float dt);
void onAutoTest(Object* sender);
private:
void dumpProfilerFPS();
void beginAutoTest();
void endAutoTest();
void nextAutoTest();
void finishAutoTest();
void autoShowSpriteTests(int curCase, int subTest,int nodes);
public:
static bool _s_autoTest;
static int _s_nSpriteCurCase;
protected:
int lastRenderedCount;
int quantityNodes;
SubTest *_subTest;
int subtestNumber;
std::vector<float> _vecFPS;
int _executeTimes;
const int MAX_AUTO_TEST_TIMES = 25;
const int MAX_SPRITE_TEST_CASE = 7;
const int MAX_SUB_TEST_NUMS = 9;
const int AUTO_TEST_NODE_NUM1 = 500;
const int AUTO_TEST_NODE_NUM2 = 1500;
};
class SpritePerformTest1 : public SpriteMainScene