2013-12-13 12:42:15 +08:00
# include "PerformanceLabelTest.h"
enum {
kMaxNodes = 200 ,
kNodesIncrease = 2 ,
TEST_COUNT = 5 ,
} ;
enum {
kTagInfoLayer = 1 ,
kTagMainLayer ,
kTagAutoTestMenu ,
kTagMenuLayer = ( kMaxNodes + 1000 ) ,
} ;
enum {
kCaseLabelTTFUpdate = 0 ,
kCaseLabelBMFontUpdate ,
kCaseLabelUpdate ,
kCaseLabelBMFontBigLabels ,
kCaseLabelBigLabels
} ;
# define LongSentencesExample "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\
Lorem ipsum dolor sit amet , consectetur adipisicing elit , sed do eiusmod tempor incididunt ut labore et dolore magna aliqua . \
Lorem ipsum dolor sit amet , consectetur adipisicing elit , sed do eiusmod tempor incididunt ut labore et dolore magna aliqua . "
////////////////////////////////////////////////////////
//
// LabelMenuLayer
//
////////////////////////////////////////////////////////
void LabelMenuLayer : : restartCallback ( Object * sender )
{
if ( LabelMainScene : : _s_autoTest )
{
log ( " It's auto label performance testing,so this operation is invalid " ) ;
return ;
}
PerformBasicLayer : : restartCallback ( sender ) ;
}
void LabelMenuLayer : : nextCallback ( Object * sender )
{
if ( LabelMainScene : : _s_autoTest )
{
log ( " It's auto label performance testing,so this operation is invalid " ) ;
return ;
}
PerformBasicLayer : : nextCallback ( sender ) ;
}
void LabelMenuLayer : : backCallback ( Object * sender )
{
if ( LabelMainScene : : _s_autoTest )
{
log ( " It's auto label performance testing,so this operation is invalid " ) ;
return ;
}
PerformBasicLayer : : backCallback ( sender ) ;
}
void LabelMenuLayer : : showCurrentTest ( )
{
auto scene = ( LabelMainScene * ) getParent ( ) ;
scene - > autoShowLabelTests ( _curCase , LabelMainScene : : AUTO_TEST_NODE_NUM ) ;
}
////////////////////////////////////////////////////////
//
// LabelMainScene
//
////////////////////////////////////////////////////////
bool LabelMainScene : : _s_autoTest = false ;
int LabelMainScene : : _s_labelCurCase = 0 ;
void LabelMainScene : : initWithSubTest ( int nodes )
{
//srandom(0);
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
_lastRenderedCount = 0 ;
_quantityNodes = 0 ;
_labelContainer = Layer : : create ( ) ;
addChild ( _labelContainer ) ;
MenuItemFont : : setFontSize ( 65 ) ;
auto decrease = MenuItemFont : : create ( " - " , CC_CALLBACK_1 ( LabelMainScene : : onDecrease , this ) ) ;
decrease - > setColor ( Color3B ( 0 , 200 , 20 ) ) ;
auto increase = MenuItemFont : : create ( " + " , CC_CALLBACK_1 ( LabelMainScene : : onIncrease , this ) ) ;
increase - > setColor ( Color3B ( 0 , 200 , 20 ) ) ;
auto menu = Menu : : create ( decrease , increase , NULL ) ;
menu - > alignItemsHorizontally ( ) ;
menu - > setPosition ( Point ( s . width / 2 , s . height - 65 ) ) ;
addChild ( menu , 1 ) ;
auto infoLabel = LabelTTF : : create ( " 0 nodes " , " Marker Felt " , 30 ) ;
infoLabel - > setColor ( Color3B ( 0 , 200 , 20 ) ) ;
infoLabel - > setPosition ( Point ( s . width / 2 , s . height - 90 ) ) ;
addChild ( infoLabel , 1 , kTagInfoLayer ) ;
// add menu
auto menuLayer = new LabelMenuLayer ( true , TEST_COUNT , LabelMainScene : : _s_labelCurCase ) ;
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 ( LabelMainScene : : _s_autoTest )
{
autoTestItem = MenuItemFont : : create ( " Auto Test On " , CC_CALLBACK_1 ( LabelMainScene : : onAutoTest , this ) ) ;
}
else
{
autoTestItem = MenuItemFont : : create ( " Auto Test Off " , CC_CALLBACK_1 ( LabelMainScene : : onAutoTest , this ) ) ;
}
autoTestItem - > setTag ( 1 ) ;
autoTestItem - > setPosition ( Point ( s . width - 90 , s . height / 2 ) ) ;
autoTestItem - > setColor ( Color3B : : RED ) ;
menuAutoTest - > addChild ( autoTestItem ) ;
addChild ( menuAutoTest , 3 , kTagAutoTestMenu ) ;
_title = LabelTTF : : create ( title ( ) . c_str ( ) , " Arial " , 32 ) ;
addChild ( _title , 1 ) ;
_title - > setPosition ( Point ( s . width / 2 , s . height - 32 ) ) ;
_title - > setColor ( Color3B ( 255 , 255 , 40 ) ) ;
while ( _quantityNodes < nodes )
onIncrease ( this ) ;
}
2013-12-19 05:52:10 +08:00
std : : string LabelMainScene : : title ( ) const
2013-12-13 12:42:15 +08:00
{
switch ( _s_labelCurCase )
{
case kCaseLabelTTFUpdate :
return " Testing LabelTTF Update " ;
case kCaseLabelBMFontUpdate :
return " Testing LabelBMFont Update " ;
case kCaseLabelUpdate :
return " Testing Label Update " ;
case kCaseLabelBMFontBigLabels :
return " Testing LabelBMFont Big Labels " ;
case kCaseLabelBigLabels :
return " Testing Label Big Labels " ;
default :
break ;
}
return " No title " ;
}
LabelMainScene : : ~ LabelMainScene ( )
{
}
void LabelMainScene : : updateNodes ( )
{
if ( _quantityNodes ! = _lastRenderedCount )
{
auto infoLabel = ( LabelTTF * ) getChildByTag ( kTagInfoLayer ) ;
char str [ 16 ] = { 0 } ;
sprintf ( str , " %u nodes " , _quantityNodes ) ;
infoLabel - > setString ( str ) ;
_lastRenderedCount = _quantityNodes ;
}
}
void LabelMainScene : : onIncrease ( Object * sender )
{
if ( _quantityNodes > = kMaxNodes )
return ;
auto size = Director : : getInstance ( ) - > getWinSize ( ) ;
switch ( _s_labelCurCase )
{
case kCaseLabelTTFUpdate :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
auto label = LabelTTF : : create ( " LabelTTF " , " Marker Felt " , 30 ) ;
2013-12-16 11:04:52 +08:00
label - > setPosition ( Point ( ( size . width / 2 + rand ( ) % 50 ) , ( ( int ) size . height / 2 + rand ( ) % 50 ) ) ) ;
2013-12-13 12:42:15 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
case kCaseLabelBMFontUpdate :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
auto label = LabelBMFont : : create ( " LabelBMFont " , " fonts/bitmapFontTest3.fnt " ) ;
2013-12-16 11:04:52 +08:00
label - > setPosition ( Point ( ( size . width / 2 + rand ( ) % 50 ) , ( ( int ) size . height / 2 + rand ( ) % 50 ) ) ) ;
2013-12-13 12:42:15 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
case kCaseLabelUpdate :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
auto label = Label : : createWithTTF ( " Label " , " fonts/arial.ttf " , 60 , size . width , TextHAlignment : : CENTER , GlyphCollection : : DYNAMIC , nullptr , true ) ;
2013-12-16 11:04:52 +08:00
label - > setPosition ( Point ( ( size . width / 2 + rand ( ) % 50 ) , ( ( int ) size . height / 2 + rand ( ) % 50 ) ) ) ;
2013-12-13 12:42:15 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
case kCaseLabelBMFontBigLabels :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
auto label = LabelBMFont : : create ( LongSentencesExample , " fonts/bitmapFontTest3.fnt " ) ;
2013-12-16 11:04:52 +08:00
label - > setPosition ( Point ( ( size . width / 2 + rand ( ) % 50 ) , ( ( int ) size . height / 2 + rand ( ) % 50 ) ) ) ;
2013-12-13 12:42:15 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
case kCaseLabelBigLabels :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
auto label = Label : : createWithTTF ( LongSentencesExample , " fonts/arial.ttf " , 60 , size . width , TextHAlignment : : CENTER , GlyphCollection : : DYNAMIC , nullptr ) ;
2013-12-16 11:04:52 +08:00
label - > setPosition ( Point ( ( rand ( ) % 50 ) , rand ( ) % ( ( int ) size . height / 3 ) ) ) ;
2013-12-13 12:42:15 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
default :
break ;
}
updateNodes ( ) ;
}
void LabelMainScene : : onDecrease ( Object * sender )
{
if ( _quantityNodes < = 0 )
return ;
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
_quantityNodes - - ;
_labelContainer - > removeChildByTag ( _quantityNodes ) ;
}
updateNodes ( ) ;
}
void LabelMainScene : : 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 ( auto fps : _vecFPS )
{
CCLOG ( " fps is :%f \n " , fps ) ;
minFPS = std : : min ( minFPS , fps ) ;
maxFPS = std : : max ( maxFPS , fps ) ;
totalFPS + = fps ;
}
averagerFPS = totalFPS / _vecFPS . size ( ) ;
log ( " Cur test: %d, cur label nums:%d, the min FPS value is %.1f,the max FPS value is %.1f,the averager FPS is %.1f " , LabelMainScene : : _s_labelCurCase , _quantityNodes , minFPS , maxFPS , averagerFPS ) ;
}
void LabelMainScene : : updateAutoTest ( float dt )
{
if ( LabelMainScene : : _s_autoTest )
{
_executeTimes + = 1 ;
_vecFPS . push_back ( Director : : getInstance ( ) - > getFrameRate ( ) ) ;
if ( _executeTimes > = LabelMainScene : : MAX_AUTO_TEST_TIMES )
{
dumpProfilerFPS ( ) ;
nextAutoTest ( ) ;
}
}
}
void LabelMainScene : : updateText ( float dt )
{
if ( _s_labelCurCase > kCaseLabelUpdate )
return ;
_accumulativeTime + = dt ;
char text [ 20 ] ;
2013-12-13 12:58:16 +08:00
sprintf ( text , " %.2f " , _accumulativeTime ) ;
2013-12-13 12:42:15 +08:00
2013-12-17 17:45:29 +08:00
auto & children = _labelContainer - > getChildren ( ) ;
2013-12-13 12:42:15 +08:00
switch ( _s_labelCurCase )
{
case kCaseLabelTTFUpdate :
2013-12-17 17:45:29 +08:00
std : : for_each ( children . begin ( ) , children . end ( ) , [ & text ] ( Node * child ) {
2013-12-13 12:42:15 +08:00
if ( child )
{
LabelTTF * label = ( LabelTTF * ) child ;
label - > setString ( text ) ;
}
} ) ;
break ;
case kCaseLabelBMFontUpdate :
2013-12-17 17:45:29 +08:00
std : : for_each ( children . begin ( ) , children . end ( ) , [ & text ] ( Node * child ) {
2013-12-13 12:42:15 +08:00
if ( child )
{
LabelBMFont * label = ( LabelBMFont * ) child ;
label - > setString ( text ) ;
}
} ) ;
break ;
case kCaseLabelUpdate :
2013-12-17 17:45:29 +08:00
std : : for_each ( children . begin ( ) , children . end ( ) , [ & text ] ( Node * child ) {
2013-12-13 12:42:15 +08:00
if ( child )
{
Label * label = ( Label * ) child ;
label - > setString ( text , false ) ;
}
} ) ;
break ;
default :
break ;
}
}
void LabelMainScene : : onEnter ( )
{
Scene : : onEnter ( ) ;
auto director = Director : : getInstance ( ) ;
auto sched = director - > getScheduler ( ) ;
sched - > scheduleSelector ( SEL_SCHEDULE ( & LabelMainScene : : updateText ) , this , 0.0f , false ) ;
_vecFPS . clear ( ) ;
_executeTimes = 0 ;
sched - > scheduleSelector ( SEL_SCHEDULE ( & LabelMainScene : : updateAutoTest ) , this , 0.2f , false ) ;
}
void LabelMainScene : : onExit ( )
{
auto director = Director : : getInstance ( ) ;
auto sched = director - > getScheduler ( ) ;
sched - > unscheduleSelector ( SEL_SCHEDULE ( & LabelMainScene : : updateText ) , this ) ;
sched - > unscheduleSelector ( SEL_SCHEDULE ( & LabelMainScene : : updateAutoTest ) , this ) ;
Scene : : onExit ( ) ;
}
void LabelMainScene : : autoShowLabelTests ( int curCase , int nodes )
{
LabelMainScene : : _s_labelCurCase = curCase ;
_title - > setString ( title ( ) ) ;
_vecFPS . clear ( ) ;
_executeTimes = 0 ;
_labelContainer - > removeAllChildren ( ) ;
_lastRenderedCount = 0 ;
_quantityNodes = 0 ;
_accumulativeTime = 0.0f ;
while ( _quantityNodes < nodes )
onIncrease ( this ) ;
}
void LabelMainScene : : endAutoTest ( )
{
LabelMainScene : : _s_autoTest = false ;
_vecFPS . clear ( ) ;
_executeTimes = 0 ;
}
void LabelMainScene : : nextAutoTest ( )
{
if ( LabelMainScene : : _s_labelCurCase + 1 < LabelMainScene : : MAX_SUB_TEST_NUMS )
{
LabelMainScene : : _s_labelCurCase + = 1 ;
autoShowLabelTests ( LabelMainScene : : _s_labelCurCase , LabelMainScene : : AUTO_TEST_NODE_NUM ) ;
}
else
{
finishAutoTest ( ) ;
}
}
void LabelMainScene : : finishAutoTest ( )
{
LabelMainScene : : _s_autoTest = false ;
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 " ) ;
}
}
2013-12-16 11:04:52 +08:00
log ( " Label performance test is finish " ) ;
2013-12-13 12:42:15 +08:00
}
void LabelMainScene : : onAutoTest ( Object * sender )
{
LabelMainScene : : _s_autoTest = ! LabelMainScene : : _s_autoTest ;
MenuItemFont * menuItem = dynamic_cast < MenuItemFont * > ( sender ) ;
if ( nullptr ! = menuItem )
{
if ( LabelMainScene : : _s_autoTest )
{
menuItem - > setString ( " Auto Test On " ) ;
autoShowLabelTests ( 0 , AUTO_TEST_NODE_NUM ) ;
}
else
{
menuItem - > setString ( " Auto Test Off " ) ;
endAutoTest ( ) ;
}
}
}
void runLabelTest ( )
{
LabelMainScene : : _s_autoTest = false ;
auto scene = new LabelMainScene ;
scene - > initWithSubTest ( LabelMainScene : : AUTO_TEST_NODE_NUM ) ;
Director : : getInstance ( ) - > replaceScene ( scene ) ;
scene - > release ( ) ;
}