From 9d46c5413d93b3c908ccb2dd66d7e18f20e181e8 Mon Sep 17 00:00:00 2001 From: WuHao Date: Tue, 28 Apr 2015 15:18:40 +0800 Subject: [PATCH] added performance test for SpritePolygon --- .../SpritePolygonTest/SpritePolygonTest.cpp | 171 +++++++++++++++++- .../SpritePolygonTest/SpritePolygonTest.h | 92 +++++++++- 2 files changed, 255 insertions(+), 8 deletions(-) diff --git a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp index 2b7b3b2a73..ec40e03acd 100644 --- a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp +++ b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp @@ -18,7 +18,173 @@ SpritePolygonTest::SpritePolygonTest() ADD_TEST_CASE(SpritePolygonTest2); ADD_TEST_CASE(SpritePolygonTest3); ADD_TEST_CASE(SpritePolygonTest4); + ADD_TEST_CASE(SpritePolygonPerformanceTestStatic); + ADD_TEST_CASE(SpritePerformanceTestStatic); + ADD_TEST_CASE(SpritePolygonPerformanceTestDynamic); + ADD_TEST_CASE(SpritePerformanceTestDynamic); } +SpritePolygonPerformance::SpritePolygonPerformance() +{ + SpritePolygonCache::getInstance()->removeAllSpritePolygonCache(); + Director::getInstance()->setClearColor(Color4F(102./255, 184./255, 204./255, 255.)); + TTFConfig ttfConfig("fonts/arial.ttf", 10); + perfLabel = Label::createWithTTF(ttfConfig, "performance test"); + addChild(perfLabel); + perfLabel->setPosition(Director::getInstance()->getVisibleSize().width/2, 80); + + spriteCount = vertCount = triCount = pixelCount = continuousLowDt =0; + auto size = Director::getInstance()->getVisibleSize(); + elapsedTime = 0; + _posX = _leftX = size.width*0.15; + _rightX = size.width*0.85; + _posY = size.height/2; + prevDt = 0.016; + goRight = true; + ended = false; + scheduleUpdate(); + continuousHighDtTime = 0.0; + waitingTime = 0.0; +} +void SpritePolygonPerformance::updateLabel() +{ +// std::string temp = "Nodes: " + Value(spriteCount).asString() + " Triangles: " + Value(triCount).asString() + "\nPixels: " + Value(pixelCount).asString() + " Vertices: " + Value(vertCount).asString(); + if(!ended) + perfLabel->setString("Nodes: " + Value(spriteCount).asString() + " Triangles: " + Value(triCount).asString() + "\nPixels: " + Value(pixelCount).asString() + " Vertices: " + Value(vertCount).asString()); +} +Node *SpritePolygonPerformance::makeSprite() +{ + return Node::create(); +} +void SpritePolygonPerformance::update(float dt) +{ + dt = dt*0.3 + prevDt*0.7; + prevDt = dt; + elapsedTime += dt; + int loops = (0.025-dt)*1000; + if(dt < 0.025 && loops>0) + { + continuousHighDtTime = clampf(continuousHighDtTime-dt*2, 0.0, 1.0); + waitingTime = clampf(waitingTime-dt, 0.0, 5.0); + continuousLowDt++; + } + else + { + continuousHighDtTime+=dt; + continuousLowDt = 0; + } + if (continuousLowDt >= 5 && loops > 0) { + for(int i = 0; i < loops; i++) + { + if(_posX >= _rightX) + { + goRight = false; + } + else if(_posX <= _leftX) + { + goRight = true; + } + auto s = makeSprite(); + addChild(s); + s->setPosition(_posX, _posY); + if(goRight) + _posX++; + else + _posX--; + + incrementStats(); + } + updateLabel(); + } + + //if we have 10 continuous low dt, then we will start to create more sprites + else if(continuousHighDtTime >= .5 || waitingTime > 3.0){ + // its now 1 seconds with high DT time, time to end + ended = true; + unscheduleUpdate(); + perfLabel->setString("Test ended in " + Value(elapsedTime).asString() + " seconds\nNodes: " + Value(spriteCount).asString() + " Triangles: " + Value(triCount).asString() + "\nPixels: " + Value(pixelCount).asString() + " Vertices: " + Value(vertCount).asString()); + _subtitleLabel->setString("Test ended"); + } + else{ + waitingTime += dt; + } +} +void SpritePolygonPerformance::incrementStats() +{ + spriteCount ++; + vertCount += _incVert; + triCount += _incTri; + pixelCount += _incPix; +} + + + + + +SpritePolygonPerformanceTestStatic::SpritePolygonPerformanceTestStatic() +{ + _title = "Static SpritePolygon Performance"; + _subtitle = "Test running, please wait until it ends"; + initIncrementStats(); +} +experimental::SpritePolygon* SpritePolygonPerformanceTestStatic::makeSprite() +{ + return experimental::SpritePolygon::create(s_pathGrossini); +} +void SpritePolygonPerformanceTestStatic::initIncrementStats() +{ + auto t = experimental::SpritePolygon::create(s_pathGrossini); + _incVert = (int)t->getVertCount(); + _incTri = (int)t->getTrianglesCount(); + _incPix = (int)t->getArea(); +} + +SpritePolygonPerformanceTestDynamic::SpritePolygonPerformanceTestDynamic() +{ + _title = "Dynamic SpritePolygon Performance"; + _subtitle = "Test running, please wait until it ends"; + initIncrementStats(); +} +experimental::SpritePolygon* SpritePolygonPerformanceTestDynamic::makeSprite() +{ + auto ret = experimental::SpritePolygon::create(s_pathGrossini); + ret->runAction(RepeatForever::create(RotateBy::create(1.0,360.0))); + return ret; +} + + +SpritePerformanceTestStatic::SpritePerformanceTestStatic() +{ + _title = "Static Sprite Performance"; + _subtitle = "Test running, please wait until it ends"; + initIncrementStats(); +} +Sprite* SpritePerformanceTestStatic::makeSprite() +{ + return Sprite::create(s_pathGrossini); +} +void SpritePerformanceTestStatic::initIncrementStats() +{ + auto t = Sprite::create(s_pathGrossini); + _incVert = 4; + _incTri = 2; + _incPix = t->getContentSize().width * t->getContentSize().height; +} + +SpritePerformanceTestDynamic::SpritePerformanceTestDynamic() +{ + _title = "Dynamic Sprite Performance"; + _subtitle = "Test running, please wait until it ends"; + initIncrementStats(); +} +Sprite* SpritePerformanceTestDynamic::makeSprite() +{ + auto ret = Sprite::create(s_pathGrossini); + ret->runAction(RepeatForever::create(RotateBy::create(1.0,360.0))); + return ret; +} + + + void SpritePolygonTestDemo::initDefaultSprite(const std::string &filename, cocos2d::experimental::SpritePolygon * inst) { Director::getInstance()->setClearColor(Color4F(102./255, 184./255, 204./255, 255.)); @@ -82,7 +248,7 @@ void SpritePolygonTestDemo::initDefaultSprite(const std::string &filename, cocos spp->addChild(sppArea); sppArea->setAnchorPoint(Vec2(0,1)); } -void SpritePolygonTestDemo::onBackCallback(cocos2d::Ref *sender) +void SpritePolygonTestCase::onBackCallback(cocos2d::Ref *sender) { TestCase::onBackCallback(sender); Director::getInstance()->setClearColor(Color4F::BLACK); @@ -328,4 +494,5 @@ SpritePolygonTest4::SpritePolygonTest4(){ _subtitle = "SpritePolygon::create(\"Images/grossini.png\", vector v, vector indices)"; auto s = experimental::SpritePolygon::create(s_pathGrossini, vs, indices); initDefaultSprite(s_pathGrossini, s); -} \ No newline at end of file +} + diff --git a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h index d86bdc35c1..3353eb791f 100644 --- a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h +++ b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h @@ -4,7 +4,17 @@ #include "2d/SpritePolygon.h" DEFINE_TEST_SUITE(SpritePolygonTest); -class SpritePolygonTestDemo : public TestCase +class SpritePolygonTestCase : public TestCase +{ +protected: + virtual void onBackCallback(Ref* sender); + std::string _title; + std::string _subtitle; + virtual std::string title() const override {return _title;}; + virtual std::string subtitle() const override {return _subtitle;}; +}; + +class SpritePolygonTestDemo : public SpritePolygonTestCase { public: void initDefaultSprite(const std::string &filename, cocos2d::experimental::SpritePolygon *inst); @@ -12,13 +22,83 @@ protected: cocos2d::experimental::SpritePolygon* spp; cocos2d::Sprite* sp; cocos2d::DrawNode *debugForNormalSprite; - virtual void onBackCallback(Ref* sender); - std::string _title; - std::string _subtitle; - virtual std::string title() const override {return _title;}; - virtual std::string subtitle() const override {return _subtitle;}; }; +class SpritePolygonPerformance : public SpritePolygonTestCase +{ +public: + CREATE_FUNC(SpritePolygonPerformance); + SpritePolygonPerformance(); + virtual void update(float dt); +protected: + int spriteCount; + int vertCount; + int triCount; + int pixelCount; + float elapsedTime; + cocos2d::Label * perfLabel; + void updateLabel(); + int continuousLowDt; + float continuousHighDtTime; + float waitingTime; + + int _posX; + int _posY; + int _leftX; + int _rightX; + bool goRight; + bool ended; + + float prevDt; + + virtual Node* makeSprite(); + virtual void incrementStats(); + + void initIncrementStats(); + int _incVert; + int _incTri; + int _incPix; +}; + +class SpritePolygonPerformanceTestStatic : public SpritePolygonPerformance +{ +public: + CREATE_FUNC(SpritePolygonPerformanceTestStatic); + SpritePolygonPerformanceTestStatic(); +protected: + cocos2d::experimental::SpritePolygon* makeSprite(); + void initIncrementStats(); +}; + +class SpritePerformanceTestStatic : public SpritePolygonPerformance +{ +public: + CREATE_FUNC(SpritePerformanceTestStatic); + SpritePerformanceTestStatic(); +protected: + cocos2d::Sprite* makeSprite(); + void initIncrementStats(); +}; + +class SpritePolygonPerformanceTestDynamic : public SpritePolygonPerformanceTestStatic +{ +public: + CREATE_FUNC(SpritePolygonPerformanceTestDynamic); + SpritePolygonPerformanceTestDynamic(); +protected: + cocos2d::experimental::SpritePolygon* makeSprite(); +}; + +class SpritePerformanceTestDynamic : public SpritePerformanceTestStatic +{ +public: + CREATE_FUNC(SpritePerformanceTestDynamic); + SpritePerformanceTestDynamic(); +protected: + cocos2d::Sprite* makeSprite(); +}; + + class SpritePolygonTest1 : public SpritePolygonTestDemo { public: