2013-12-13 12:42:15 +08:00
# include "PerformanceLabelTest.h"
enum {
kMaxNodes = 200 ,
2013-12-18 10:46:02 +08:00
kNodesIncrease = 10 ,
2013-12-13 12:42:15 +08:00
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
//
////////////////////////////////////////////////////////
2014-02-20 10:53:49 +08:00
void LabelMenuLayer : : restartCallback ( Ref * sender )
2013-12-13 12:42:15 +08:00
{
if ( LabelMainScene : : _s_autoTest )
{
log ( " It's auto label performance testing,so this operation is invalid " ) ;
return ;
}
PerformBasicLayer : : restartCallback ( sender ) ;
}
2014-02-20 10:53:49 +08:00
void LabelMenuLayer : : nextCallback ( Ref * sender )
2013-12-13 12:42:15 +08:00
{
if ( LabelMainScene : : _s_autoTest )
{
log ( " It's auto label performance testing,so this operation is invalid " ) ;
return ;
}
PerformBasicLayer : : nextCallback ( sender ) ;
}
2014-02-20 10:53:49 +08:00
void LabelMenuLayer : : backCallback ( Ref * sender )
2013-12-13 12:42:15 +08:00
{
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 ;
2014-01-11 22:33:07 +08:00
_accumulativeTime = 0.0f ;
2013-12-13 12:42:15 +08:00
_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 ) ) ;
2014-07-10 00:45:27 +08:00
auto menu = Menu : : create ( decrease , increase , nullptr ) ;
2013-12-13 12:42:15 +08:00
menu - > alignItemsHorizontally ( ) ;
2014-05-15 01:07:09 +08:00
menu - > setPosition ( Vec2 ( s . width / 2 , s . height - 65 ) ) ;
2013-12-13 12:42:15 +08:00
addChild ( menu , 1 ) ;
2014-04-09 23:31:05 +08:00
auto infoLabel = Label : : createWithTTF ( " 0 nodes " , " fonts/Marker Felt.ttf " , 30 ) ;
2013-12-13 12:42:15 +08:00
infoLabel - > setColor ( Color3B ( 0 , 200 , 20 ) ) ;
2014-05-15 01:07:09 +08:00
infoLabel - > setPosition ( Vec2 ( s . width / 2 , s . height - 90 ) ) ;
2013-12-13 12:42:15 +08:00
addChild ( infoLabel , 1 , kTagInfoLayer ) ;
// add menu
2014-08-28 07:31:57 +08:00
auto menuLayer = new ( std : : nothrow ) LabelMenuLayer ( true , TEST_COUNT , LabelMainScene : : _s_labelCurCase ) ;
2013-12-13 12:42:15 +08:00
addChild ( menuLayer , 1 , kTagMenuLayer ) ;
menuLayer - > release ( ) ;
/**
* auto test menu
*/
auto menuAutoTest = Menu : : create ( ) ;
2014-05-15 01:07:09 +08:00
menuAutoTest - > setPosition ( Vec2 : : ZERO ) ;
2014-03-26 23:33:58 +08:00
MenuItemFont : : setFontName ( " fonts/arial.ttf " ) ;
2013-12-13 12:42:15 +08:00
MenuItemFont : : setFontSize ( 24 ) ;
2014-07-10 00:45:27 +08:00
MenuItemFont * autoTestItem = nullptr ;
2013-12-13 12:42:15 +08:00
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 ) ;
2014-05-15 01:07:09 +08:00
autoTestItem - > setPosition ( Vec2 ( s . width - 90 , s . height / 2 ) ) ;
2013-12-13 12:42:15 +08:00
autoTestItem - > setColor ( Color3B : : RED ) ;
menuAutoTest - > addChild ( autoTestItem ) ;
addChild ( menuAutoTest , 3 , kTagAutoTestMenu ) ;
2014-04-09 23:31:05 +08:00
_title = Label : : createWithTTF ( title ( ) . c_str ( ) , " fonts/arial.ttf " , 32 ) ;
2013-12-13 12:42:15 +08:00
addChild ( _title , 1 ) ;
2014-05-15 01:07:09 +08:00
_title - > setPosition ( Vec2 ( s . width / 2 , s . height - 50 ) ) ;
2013-12-13 12:42:15 +08:00
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 )
{
2014-03-26 23:33:58 +08:00
auto infoLabel = ( Label * ) getChildByTag ( kTagInfoLayer ) ;
2013-12-13 12:42:15 +08:00
char str [ 16 ] = { 0 } ;
sprintf ( str , " %u nodes " , _quantityNodes ) ;
infoLabel - > setString ( str ) ;
_lastRenderedCount = _quantityNodes ;
}
}
2014-02-20 10:53:49 +08:00
void LabelMainScene : : onIncrease ( Ref * sender )
2013-12-13 12:42:15 +08:00
{
if ( _quantityNodes > = kMaxNodes )
return ;
auto size = Director : : getInstance ( ) - > getWinSize ( ) ;
switch ( _s_labelCurCase )
{
case kCaseLabelTTFUpdate :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
2014-04-09 23:31:05 +08:00
auto label = Label : : createWithSystemFont ( " LabelTTF " , " Marker Felt " , 30 ) ;
2014-05-15 01:07:09 +08:00
label - > setPosition ( Vec2 ( ( 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 + + )
{
2014-03-28 10:28:44 +08:00
auto label = Label : : createWithBMFont ( " fonts/bitmapFontTest3.fnt " , " LabelBMFont " ) ;
2014-05-15 01:07:09 +08:00
label - > setPosition ( Vec2 ( ( 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 :
{
2014-02-19 20:26:14 +08:00
TTFConfig ttfConfig ( " fonts/arial.ttf " , 60 , GlyphCollection : : DYNAMIC ) ;
2014-01-11 22:33:07 +08:00
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
2014-02-19 20:26:14 +08:00
auto label = Label : : createWithTTF ( ttfConfig , " Label " , TextHAlignment : : LEFT ) ;
2014-05-15 01:07:09 +08:00
label - > setPosition ( Vec2 ( ( size . width / 2 + rand ( ) % 50 ) , ( ( int ) size . height / 2 + rand ( ) % 50 ) ) ) ;
2014-01-11 22:33:07 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
}
2013-12-13 12:42:15 +08:00
case kCaseLabelBMFontBigLabels :
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
2014-03-28 10:28:44 +08:00
auto label = Label : : createWithBMFont ( " fonts/bitmapFontTest3.fnt " , LongSentencesExample ) ;
2014-05-15 01:07:09 +08:00
label - > setPosition ( Vec2 ( ( 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 :
{
2014-01-11 22:33:07 +08:00
TTFConfig ttfConfig ( " fonts/arial.ttf " , 60 , GlyphCollection : : DYNAMIC ) ;
for ( int i = 0 ; i < kNodesIncrease ; i + + )
{
auto label = Label : : createWithTTF ( ttfConfig , LongSentencesExample , TextHAlignment : : CENTER , size . width ) ;
2014-05-15 01:07:09 +08:00
label - > setPosition ( Vec2 ( ( rand ( ) % 50 ) , rand ( ) % ( ( int ) size . height / 3 ) ) ) ;
2014-01-11 22:33:07 +08:00
_labelContainer - > addChild ( label , 1 , _quantityNodes ) ;
_quantityNodes + + ;
}
break ;
}
2013-12-13 12:42:15 +08:00
default :
break ;
}
updateNodes ( ) ;
}
2014-02-20 10:53:49 +08:00
void LabelMainScene : : onDecrease ( Ref * sender )
2013-12-13 12:42:15 +08:00
{
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-20 05:34:41 +08:00
for ( const auto & child : children ) {
2014-03-26 23:33:58 +08:00
Label * label = ( Label * ) child ;
2013-12-20 05:34:41 +08:00
label - > setString ( text ) ;
}
2013-12-13 12:42:15 +08:00
break ;
case kCaseLabelBMFontUpdate :
2013-12-20 05:34:41 +08:00
for ( const auto & child : children ) {
2014-03-28 10:28:44 +08:00
auto label = ( Label * ) child ;
2013-12-20 05:34:41 +08:00
label - > setString ( text ) ;
}
2013-12-13 12:42:15 +08:00
break ;
case kCaseLabelUpdate :
2013-12-20 05:34:41 +08:00
for ( const auto & child : children ) {
Label * label = ( Label * ) child ;
2014-01-15 17:21:08 +08:00
label - > setString ( text ) ;
2013-12-20 05:34:41 +08:00
}
2013-12-13 12:42:15 +08:00
break ;
default :
break ;
}
}
void LabelMainScene : : onEnter ( )
{
Scene : : onEnter ( ) ;
auto director = Director : : getInstance ( ) ;
auto sched = director - > getScheduler ( ) ;
2014-10-04 00:38:36 +08:00
sched - > schedule ( CC_SCHEDULE_SELECTOR ( LabelMainScene : : updateText ) , this , 0.0f , false ) ;
2013-12-13 12:42:15 +08:00
_vecFPS . clear ( ) ;
_executeTimes = 0 ;
2014-10-04 00:38:36 +08:00
sched - > schedule ( CC_SCHEDULE_SELECTOR ( LabelMainScene : : updateAutoTest ) , this , 0.2f , false ) ;
2013-12-13 12:42:15 +08:00
}
void LabelMainScene : : onExit ( )
{
auto director = Director : : getInstance ( ) ;
auto sched = director - > getScheduler ( ) ;
2014-10-04 00:38:36 +08:00
sched - > unschedule ( CC_SCHEDULE_SELECTOR ( LabelMainScene : : updateText ) , this ) ;
sched - > unschedule ( CC_SCHEDULE_SELECTOR ( LabelMainScene : : updateAutoTest ) , this ) ;
2013-12-13 12:42:15 +08:00
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 ;
2013-12-18 10:46:02 +08:00
autoShowLabelTests ( LabelMainScene : : _s_labelCurCase , _quantityNodes ) ;
2013-12-13 12:42:15 +08:00
}
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
}
2014-02-20 10:53:49 +08:00
void LabelMainScene : : onAutoTest ( Ref * sender )
2013-12-13 12:42:15 +08:00
{
LabelMainScene : : _s_autoTest = ! LabelMainScene : : _s_autoTest ;
MenuItemFont * menuItem = dynamic_cast < MenuItemFont * > ( sender ) ;
if ( nullptr ! = menuItem )
{
if ( LabelMainScene : : _s_autoTest )
{
menuItem - > setString ( " Auto Test On " ) ;
2013-12-18 10:46:02 +08:00
autoShowLabelTests ( 0 , _quantityNodes ) ;
2013-12-13 12:42:15 +08:00
}
else
{
menuItem - > setString ( " Auto Test Off " ) ;
endAutoTest ( ) ;
}
}
}
void runLabelTest ( )
{
LabelMainScene : : _s_autoTest = false ;
2014-08-28 07:31:57 +08:00
auto scene = new ( std : : nothrow ) LabelMainScene ;
2013-12-13 12:42:15 +08:00
scene - > initWithSubTest ( LabelMainScene : : AUTO_TEST_NODE_NUM ) ;
Director : : getInstance ( ) - > replaceScene ( scene ) ;
scene - > release ( ) ;
}