PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/performance-tests/Classes/tests/PerformanceScenarioTest.cpp

https://github.com/dumganhar/cocos2d-x
C++ | 438 lines | 329 code | 69 blank | 40 comment | 28 complexity | 6e1ab7637f4d537660d7c0a140c9d7d4 MD5 | raw file
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "PerformanceScenarioTest.h"
  21. #include "Profile.h"
  22. USING_NS_CC;
  23. #define DELAY_TIME 4
  24. #define STAT_TIME 3
  25. PerformceScenarioTests::PerformceScenarioTests()
  26. {
  27. ADD_TEST_CASE(ScenarioTest);
  28. }
  29. ////////////////////////////////////////////////////////
  30. //
  31. // ScenarioTest
  32. //
  33. ////////////////////////////////////////////////////////
  34. int ScenarioTest::_initParticleNum = 500;
  35. int ScenarioTest::_parStepNum = 500;
  36. int ScenarioTest::_initSpriteNum = 2000;
  37. int ScenarioTest::_spriteStepNum = 500;
  38. int ScenarioTest::_initParsysNum = 10;
  39. int ScenarioTest::_parsysStepNum = 5;
  40. typedef struct
  41. {
  42. int spriteCount;
  43. int particleCount;
  44. int particleSystemCount;
  45. } TestCaseInfo;
  46. static TestCaseInfo autoTestCounts[] = {
  47. { 500, 500, 50 },
  48. { 1000, 1000, 100 },
  49. { 1500, 1500, 150 }
  50. };
  51. bool ScenarioTest::init()
  52. {
  53. if (TestCase::init())
  54. {
  55. performTests();
  56. return true;
  57. }
  58. return false;
  59. }
  60. void ScenarioTest::performTests()
  61. {
  62. auto listener = EventListenerTouchAllAtOnce::create();
  63. listener->onTouchesMoved = CC_CALLBACK_2(ScenarioTest::onTouchesMoved, this);
  64. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  65. _particleNumber = _initParticleNum;
  66. // get the window size & origin position
  67. auto s = Director::getInstance()->getVisibleSize();
  68. auto origin = Director::getInstance()->getVisibleOrigin();
  69. // add tile map
  70. _map1 = TMXTiledMap::create("TileMaps/iso-test.tmx");
  71. _map1->setAnchorPoint( Vec2(0.5, 0.5) );
  72. _map1->setPosition(origin);
  73. this->addChild(_map1);
  74. _map2 = TMXTiledMap::create("TileMaps/iso-test2.tmx");
  75. _map2->setAnchorPoint( Vec2(0.5, 0.5) );
  76. _map2->setPosition(origin);
  77. this->addChild(_map2);
  78. // add toggle menu item
  79. MenuItemFont::setFontSize(20);
  80. _itemToggle = MenuItemToggle::createWithCallback(nullptr,
  81. MenuItemFont::create( "Add/Remove Sprite" ),
  82. MenuItemFont::create( "Add/Remove Particle"),
  83. MenuItemFont::create( "Add/Remove Particle System"),
  84. nullptr);
  85. _itemToggle->setAnchorPoint(Vec2(0.0f, 0.5f));
  86. _itemToggle->setPosition(Vec2(origin.x, origin.y + s.height / 2));
  87. // add decrease & increase menu item
  88. MenuItemFont::setFontSize(65);
  89. auto decrease = MenuItemFont::create(" - ", [&](Ref *sender) {
  90. int idx = _itemToggle->getSelectedIndex();
  91. switch (idx) {
  92. case 0:
  93. removeSprites();
  94. break;
  95. case 1:
  96. removeParticles();
  97. break;
  98. case 2:
  99. removeParticleSystem();
  100. break;
  101. default:
  102. break;
  103. }
  104. });
  105. decrease->setPosition(Vec2(origin.x + s.width / 2 - 80, origin.y + 80));
  106. decrease->setColor(Color3B(0,200,20));
  107. auto increase = MenuItemFont::create(" + ", [&](Ref *sender) {
  108. int idx = _itemToggle->getSelectedIndex();
  109. switch (idx) {
  110. case 0:
  111. addNewSprites(_spriteStepNum);
  112. break;
  113. case 1:
  114. addParticles(_parStepNum);
  115. break;
  116. case 2:
  117. addParticleSystem(_parsysStepNum);
  118. break;
  119. default:
  120. break;
  121. }
  122. });
  123. increase->setColor(Color3B(0,200,20));
  124. increase->setPosition(Vec2(origin.x + s.width / 2 + 80, origin.y + 80));
  125. auto menu = Menu::create(_itemToggle, decrease, increase, nullptr);
  126. menu->setPosition(Vec2(0.0f, 0.0f));
  127. addChild(menu, 10);
  128. // add tip labels
  129. _spriteLabel = Label::createWithTTF("Sprites : 0", "fonts/arial.ttf", 15);
  130. _spriteLabel->setAnchorPoint(Vec2(0.0f, 0.5f));
  131. addChild(_spriteLabel, 10);
  132. _spriteLabel->setPosition(Vec2(origin.x, origin.y + s.height/2 + 70));
  133. char str[32] = { 0 };
  134. sprintf(str, "Particles : %d", _particleNumber);
  135. _particleLabel = Label::createWithTTF(str, "fonts/arial.ttf", 15);
  136. _particleLabel->setAnchorPoint(Vec2(0.0f, 0.5f));
  137. addChild(_particleLabel, 10);
  138. _particleLabel->setPosition(Vec2(origin.x, origin.y + s.height/2 + 45));
  139. _parsysLabel = Label::createWithTTF("Particle System : 0", "fonts/arial.ttf", 15);
  140. _parsysLabel->setAnchorPoint(Vec2(0.0f, 0.5f));
  141. addChild(_parsysLabel, 10);
  142. _parsysLabel->setPosition(Vec2(origin.x, origin.y + s.height/2 + 20));
  143. // add sprites
  144. addNewSprites(_initSpriteNum);
  145. // add particle system
  146. addParticleSystem(_initParsysNum);
  147. }
  148. void ScenarioTest::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
  149. {
  150. auto touch = touches[0];
  151. auto diff = touch->getDelta();
  152. auto currentPos1 = _map1->getPosition();
  153. _map1->setPosition(currentPos1 + diff);
  154. auto currentPos2 = _map2->getPosition();
  155. _map2->setPosition(currentPos2 + diff);
  156. }
  157. void ScenarioTest::addParticles(int num)
  158. {
  159. _particleNumber += num;
  160. for (auto par : _parsysArray) {
  161. par->setTotalParticles(_particleNumber);
  162. }
  163. char str[25] = { 0 };
  164. sprintf(str, "Particles : %d", _particleNumber);
  165. _particleLabel->setString(str);
  166. }
  167. void ScenarioTest::removeParticles()
  168. {
  169. if (_particleNumber <= 0) {
  170. return;
  171. }
  172. int removeNum = MIN(_particleNumber, _parStepNum);
  173. _particleNumber -= removeNum;
  174. for (auto par : _parsysArray) {
  175. par->setTotalParticles(_particleNumber);
  176. }
  177. char str[25] = { 0 };
  178. sprintf(str, "Particles : %d", _particleNumber);
  179. _particleLabel->setString(str);
  180. }
  181. void ScenarioTest::addNewSprites(int num)
  182. {
  183. auto s = Director::getInstance()->getVisibleSize();
  184. auto origin = Director::getInstance()->getVisibleOrigin();
  185. for (int i = 0; i < num; ++i) {
  186. int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f);
  187. int x = (idx%5) * 85;
  188. int y = (idx/5) * 121;
  189. auto sprite = Sprite::create("Images/grossini_dance_atlas.png", Rect(x,y,85,121) );
  190. addChild( sprite );
  191. float randomx = CCRANDOM_0_1();
  192. float randomy = CCRANDOM_0_1();
  193. sprite->setPosition(origin + Vec2(randomx * s.width, randomy * s.height));
  194. ActionInterval* action;
  195. float random = CCRANDOM_0_1();
  196. if( random < 0.20 )
  197. action = ScaleBy::create(3, 2);
  198. else if(random < 0.40)
  199. action = RotateBy::create(3, 360);
  200. else if( random < 0.60)
  201. action = Blink::create(1, 3);
  202. else if( random < 0.8 )
  203. action = TintBy::create(2, 0, -255, -255);
  204. else
  205. action = FadeOut::create(2);
  206. auto action_back = action->reverse();
  207. auto seq = Sequence::create( action, action_back, nullptr );
  208. sprite->runAction( RepeatForever::create(seq) );
  209. _spriteArray.pushBack(sprite);
  210. }
  211. char str[20] = {0};
  212. sprintf(str, "Sprites : %d", (int)_spriteArray.size());
  213. _spriteLabel->setString(str);
  214. }
  215. void ScenarioTest::removeSprites()
  216. {
  217. ssize_t number = _spriteArray.size();
  218. if (number <= 0) {
  219. return;
  220. }
  221. ssize_t removeNum = MIN(number, _spriteStepNum);
  222. for (int i = 0; i < removeNum; ++i) {
  223. auto sprite = _spriteArray.getRandomObject();
  224. removeChild(sprite);
  225. _spriteArray.eraseObject(sprite);
  226. }
  227. char str[20] = {0};
  228. sprintf(str, "Sprites : %d", (int)_spriteArray.size());
  229. _spriteLabel->setString(str);
  230. }
  231. static const std::string _particleFiles[] = {
  232. "Particles/BoilingFoam.plist",
  233. "Particles/Galaxy.plist",
  234. "Particles/SmallSun.plist",
  235. "Particles/lines.plist",
  236. "Particles/Comet.plist",
  237. "Particles/LavaFlow.plist",
  238. "Particles/SpinningPeas.plist",
  239. "Particles/Flower.plist",
  240. "Particles/Phoenix.plist",
  241. "Particles/debian.plist",
  242. };
  243. void ScenarioTest::addParticleSystem(int num)
  244. {
  245. int filesSize = sizeof(_particleFiles) / sizeof(std::string);
  246. auto s = Director::getInstance()->getVisibleSize();
  247. auto origin = Director::getInstance()->getVisibleOrigin();
  248. for (int i = 0; i < num; ++i)
  249. {
  250. float randomIdx = CCRANDOM_0_1();
  251. int idx = (filesSize - 1) * randomIdx;
  252. std::string fileName = _particleFiles[idx];
  253. auto par = ParticleSystemQuad::create(fileName);
  254. float randomx = CCRANDOM_0_1();
  255. float randomy = CCRANDOM_0_1();
  256. par->setPosition(origin + Vec2(s.width * randomx, s.height * randomy));
  257. par->setTotalParticles(_particleNumber);
  258. addChild(par, 9);
  259. _parsysArray.pushBack(par);
  260. }
  261. char str[40] = {0};
  262. sprintf(str, "Particle System : %d", (int)_parsysArray.size());
  263. _parsysLabel->setString(str);
  264. }
  265. void ScenarioTest::removeParticleSystem()
  266. {
  267. ssize_t number = _parsysArray.size();
  268. if (number <= 0) {
  269. return;
  270. }
  271. ssize_t removeNum = MIN(number, _parsysStepNum);
  272. for (int i = 0; i < removeNum; ++i) {
  273. auto par = _parsysArray.getRandomObject();
  274. removeChild(par);
  275. _parsysArray.eraseObject(par);
  276. }
  277. char str[40] = {0};
  278. sprintf(str, "Particle System : %d", (int)_parsysArray.size());
  279. _parsysLabel->setString(str);
  280. }
  281. void ScenarioTest::onEnter()
  282. {
  283. TestCase::onEnter();
  284. if (isAutoTesting()) {
  285. autoTestIndex = 0;
  286. Profile::getInstance()->testCaseBegin("ScenarioTest",
  287. genStrVector("SpriteCount", "ParticleCount", "ParticleSystemCount", nullptr),
  288. genStrVector("Avg", "Min", "Max", nullptr));
  289. doAutoTest();
  290. scheduleUpdate();
  291. }
  292. }
  293. void ScenarioTest::onExit()
  294. {
  295. Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
  296. TestCase::onExit();
  297. }
  298. void ScenarioTest::update(float dt)
  299. {
  300. if (isStating) {
  301. totalStatTime += dt;
  302. statCount++;
  303. auto curFrameRate = Director::getInstance()->getFrameRate();
  304. if (maxFrameRate < 0 || curFrameRate > maxFrameRate)
  305. maxFrameRate = curFrameRate;
  306. if (minFrameRate < 0 || curFrameRate < minFrameRate)
  307. minFrameRate = curFrameRate;
  308. }
  309. }
  310. void ScenarioTest::beginStat(float dt)
  311. {
  312. unschedule(CC_SCHEDULE_SELECTOR(ScenarioTest::beginStat));
  313. isStating = true;
  314. }
  315. void ScenarioTest::endStat(float dt)
  316. {
  317. unschedule(CC_SCHEDULE_SELECTOR(ScenarioTest::endStat));
  318. isStating = false;
  319. // record test data
  320. auto avgStr = genStr("%.2f", (float) statCount / totalStatTime);
  321. Profile::getInstance()->addTestResult(genStrVector(genStr("%d", _spriteArray.size()).c_str(),
  322. genStr("%d", _particleNumber).c_str(),
  323. genStr("%d", _parsysArray.size()).c_str(),
  324. nullptr),
  325. genStrVector(avgStr.c_str(), genStr("%.2f", minFrameRate).c_str(),
  326. genStr("%.2f", maxFrameRate).c_str(), nullptr));
  327. // check the auto test is end or not
  328. int autoTestCount = sizeof(autoTestCounts) / sizeof(TestCaseInfo);
  329. if (autoTestIndex >= (autoTestCount - 1))
  330. {
  331. // auto test end
  332. Profile::getInstance()->testCaseEnd();
  333. setAutoTesting(false);
  334. return;
  335. }
  336. autoTestIndex++;
  337. doAutoTest();
  338. }
  339. void ScenarioTest::doAutoTest()
  340. {
  341. isStating = false;
  342. statCount = 0;
  343. totalStatTime = 0.0f;
  344. minFrameRate = -1.0f;
  345. maxFrameRate = -1.0f;
  346. // remove all nodes
  347. while (_spriteArray.size() > 0) {
  348. removeSprites();
  349. }
  350. while (_parsysArray.size() > 0) {
  351. removeParticleSystem();
  352. }
  353. // add nodes
  354. auto caseInfo = autoTestCounts[autoTestIndex];
  355. _particleNumber = 0;
  356. addNewSprites(caseInfo.spriteCount);
  357. addParticleSystem(caseInfo.particleSystemCount);
  358. addParticles(caseInfo.particleCount);
  359. schedule(CC_SCHEDULE_SELECTOR(ScenarioTest::beginStat), DELAY_TIME);
  360. schedule(CC_SCHEDULE_SELECTOR(ScenarioTest::endStat), DELAY_TIME + STAT_TIME);
  361. }
  362. std::string ScenarioTest::title() const
  363. {
  364. return "Scenario Performance Test";
  365. }