2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-28 14:54:12 +08:00
|
|
|
#include "Profile.h"
|
2016-11-04 09:36:59 +08:00
|
|
|
#include "json/document-wrapper.h"
|
2015-08-31 16:57:46 +08:00
|
|
|
#include "json/prettywriter.h"
|
|
|
|
#include "json/stringbuffer.h"
|
2015-09-09 16:02:33 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2015-09-09 18:29:18 +08:00
|
|
|
#include "cocos2d.h"
|
|
|
|
#include <time.h>
|
2015-08-28 14:54:12 +08:00
|
|
|
|
2015-09-14 09:52:21 +08:00
|
|
|
#define LOG_FILE_NAME_FMT "PerformanceLog-%s-%s.json"
|
2015-08-31 16:57:46 +08:00
|
|
|
#define PLIST_FILE_NAME "PerformanceLog.plist"
|
2015-08-28 14:54:12 +08:00
|
|
|
|
2015-09-08 14:33:38 +08:00
|
|
|
#define KEY_DEVICE "device"
|
|
|
|
#define KEY_ENGINE_VERSION "engineVersion"
|
|
|
|
#define KEY_RESULTS "results"
|
|
|
|
#define KEY_CONDITION_HEADERS "conditionHeaders"
|
|
|
|
#define KEY_RESULT_HEADERS "resultHeaders"
|
2015-09-09 18:29:18 +08:00
|
|
|
#define KEY_FILE_VERSION "fileVersion"
|
|
|
|
#define KEY_OS_VERSION "osVersion"
|
|
|
|
#define KEY_TIMESTAMP "timeStamp"
|
|
|
|
|
|
|
|
#define FILE_VERSION 1
|
2015-09-08 14:33:38 +08:00
|
|
|
|
2015-09-14 09:52:21 +08:00
|
|
|
#define USE_PRETTY_OUTPUT_FORMAT 0
|
2015-09-08 15:29:56 +08:00
|
|
|
#define USE_JSON_FORMAT 1
|
|
|
|
|
2015-09-09 18:29:18 +08:00
|
|
|
|
|
|
|
// For different device & os, change these values
|
|
|
|
// TODO : get device info automatically
|
2015-09-14 09:52:21 +08:00
|
|
|
#define DEVICE_NAME "DeviceName"
|
|
|
|
#define OS_VERSION "SystemVersion"
|
2015-09-09 18:29:18 +08:00
|
|
|
|
2015-08-28 14:54:12 +08:00
|
|
|
static Profile* s_profile = nullptr;
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
// tools methods
|
|
|
|
std::string genStr(const char* format, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
va_start(arg_ptr, format);
|
2015-09-10 11:45:00 +08:00
|
|
|
|
|
|
|
char buf[MAX_LOG_LENGTH];
|
|
|
|
vsnprintf(buf, MAX_LOG_LENGTH - 3, format, arg_ptr);
|
2015-08-31 11:42:48 +08:00
|
|
|
|
2015-08-28 14:54:12 +08:00
|
|
|
va_end(arg_ptr);
|
2015-08-31 11:42:48 +08:00
|
|
|
|
|
|
|
return buf;
|
2015-08-28 14:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> genStrVector(const char* str1, ...)
|
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
va_list arg_ptr;
|
|
|
|
const char* str = str1;
|
|
|
|
va_start(arg_ptr, str1);
|
|
|
|
while (nullptr != str) {
|
|
|
|
std::string strObj = str;
|
|
|
|
ret.push_back(strObj);
|
|
|
|
str = va_arg(arg_ptr, const char*);
|
|
|
|
}
|
|
|
|
va_end(arg_ptr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:57:46 +08:00
|
|
|
// declare the methods
|
|
|
|
rapidjson::Value valueVectorToJson(cocos2d::ValueVector & theVector, rapidjson::Document::AllocatorType& allocator);
|
|
|
|
rapidjson::Value valueMapToJson(cocos2d::ValueMap & theMap, rapidjson::Document::AllocatorType& allocator);
|
|
|
|
|
|
|
|
rapidjson::Value convertToJsonValue(cocos2d::Value & value, rapidjson::Document::AllocatorType& allocator)
|
|
|
|
{
|
|
|
|
rapidjson::Value theJsonValue;
|
|
|
|
auto type = value.getType();
|
|
|
|
switch (type) {
|
|
|
|
case cocos2d::Value::Type::STRING:
|
|
|
|
theJsonValue.SetString(value.asString().c_str(), allocator);
|
|
|
|
break;
|
|
|
|
case cocos2d::Value::Type::MAP:
|
|
|
|
theJsonValue = valueMapToJson(value.asValueMap(), allocator);
|
|
|
|
break;
|
|
|
|
case cocos2d::Value::Type::VECTOR:
|
|
|
|
theJsonValue = valueVectorToJson(value.asValueVector(), allocator);
|
|
|
|
break;
|
|
|
|
case cocos2d::Value::Type::INTEGER:
|
|
|
|
theJsonValue.SetInt(value.asInt());
|
|
|
|
break;
|
|
|
|
case cocos2d::Value::Type::BOOLEAN:
|
|
|
|
theJsonValue.SetBool(value.asBool());
|
|
|
|
break;
|
|
|
|
case cocos2d::Value::Type::FLOAT:
|
|
|
|
case cocos2d::Value::Type::DOUBLE:
|
|
|
|
theJsonValue.SetDouble(value.asDouble());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return theJsonValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value valueMapToJson(cocos2d::ValueMap & theMap, rapidjson::Document::AllocatorType& allocator)
|
|
|
|
{
|
|
|
|
rapidjson::Value ret(rapidjson::kObjectType);
|
|
|
|
|
|
|
|
for (ValueMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
|
|
|
|
auto key = iter->first;
|
|
|
|
rapidjson::Value theJsonKey(rapidjson::kStringType);
|
|
|
|
theJsonKey.SetString(key.c_str(), allocator);
|
|
|
|
|
|
|
|
cocos2d::Value value = iter->second;
|
|
|
|
rapidjson::Value theJsonValue = convertToJsonValue(value, allocator);
|
|
|
|
ret.AddMember(theJsonKey, theJsonValue, allocator);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value valueVectorToJson(cocos2d::ValueVector & theVector, rapidjson::Document::AllocatorType& allocator)
|
|
|
|
{
|
|
|
|
rapidjson::Value ret(rapidjson::kArrayType);
|
|
|
|
|
|
|
|
auto vectorSize = theVector.size();
|
|
|
|
for (int i = 0; i < vectorSize; i++) {
|
|
|
|
cocos2d::Value value = theVector[i];
|
|
|
|
rapidjson::Value theJsonValue = convertToJsonValue(value, allocator);
|
|
|
|
ret.PushBack(theJsonValue, allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-28 14:54:12 +08:00
|
|
|
Profile* Profile::getInstance()
|
|
|
|
{
|
|
|
|
if (nullptr == s_profile)
|
|
|
|
{
|
|
|
|
s_profile = new Profile();
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_profile;
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:42:48 +08:00
|
|
|
void Profile::destroyInstance()
|
2015-08-28 14:54:12 +08:00
|
|
|
{
|
2015-09-07 16:40:47 +08:00
|
|
|
CC_SAFE_DELETE(s_profile);
|
2015-08-28 14:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Profile::Profile()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Profile::~Profile()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Profile::testCaseBegin(std::string testName, std::vector<std::string> condHeaders, std::vector<std::string> retHeaders)
|
|
|
|
{
|
|
|
|
curTestName = testName;
|
2015-09-08 14:33:38 +08:00
|
|
|
|
|
|
|
ValueVector conds;
|
|
|
|
for (int i = 0; i < condHeaders.size(); i++) {
|
|
|
|
conds.push_back(Value(condHeaders[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueVector rets;
|
|
|
|
for (int j = 0; j < retHeaders.size(); j++) {
|
|
|
|
rets.push_back(Value(retHeaders[j]));
|
|
|
|
}
|
2015-09-02 11:21:17 +08:00
|
|
|
|
|
|
|
auto findValue = testData.find(curTestName);
|
|
|
|
if (findValue != testData.end())
|
|
|
|
{
|
2015-09-08 14:33:38 +08:00
|
|
|
auto curMap = findValue->second.asValueMap();
|
|
|
|
curMap[KEY_CONDITION_HEADERS] = Value(conds);
|
|
|
|
curMap[KEY_RESULT_HEADERS] = Value(rets);
|
|
|
|
|
|
|
|
if (curMap.find(KEY_RESULTS) != curMap.end())
|
|
|
|
curTestResults = curMap[KEY_RESULTS].asValueVector();
|
|
|
|
else
|
|
|
|
curTestResults.clear();
|
2015-09-02 11:21:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-08 14:33:38 +08:00
|
|
|
ValueMap theData;
|
|
|
|
theData[KEY_CONDITION_HEADERS] = conds;
|
|
|
|
theData[KEY_RESULT_HEADERS] = rets;
|
|
|
|
testData[curTestName] = Value(theData);
|
2015-09-02 11:21:17 +08:00
|
|
|
curTestResults.clear();
|
|
|
|
}
|
2015-08-28 14:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profile::addTestResult(std::vector<std::string> conditions, std::vector<std::string> results)
|
|
|
|
{
|
2015-09-08 14:33:38 +08:00
|
|
|
ValueVector curRet;
|
2015-08-28 14:54:12 +08:00
|
|
|
|
2015-09-08 14:33:38 +08:00
|
|
|
for (int i = 0; i < conditions.size(); i++) {
|
|
|
|
curRet.push_back(Value(conditions[i]));
|
2015-08-28 14:54:12 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:33:38 +08:00
|
|
|
for (int j = 0; j < results.size(); j++) {
|
|
|
|
curRet.push_back(Value(results[j]));
|
|
|
|
}
|
|
|
|
curTestResults.push_back(Value(curRet));
|
2015-08-28 14:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profile::testCaseEnd()
|
|
|
|
{
|
|
|
|
// add the result of current test case into the testData.
|
2015-09-08 14:33:38 +08:00
|
|
|
ValueMap theData = testData[curTestName].asValueMap();
|
|
|
|
theData[KEY_RESULTS] = curTestResults;
|
|
|
|
testData[curTestName] = Value(theData);
|
2015-08-31 11:42:48 +08:00
|
|
|
}
|
2015-08-28 14:54:12 +08:00
|
|
|
|
2015-08-31 11:42:48 +08:00
|
|
|
void Profile::flush()
|
|
|
|
{
|
2015-09-09 18:29:18 +08:00
|
|
|
// record the format version
|
|
|
|
testData[KEY_FILE_VERSION] = Value(FILE_VERSION);
|
|
|
|
testData[KEY_DEVICE] = Value(DEVICE_NAME);
|
|
|
|
testData[KEY_OS_VERSION] = Value(OS_VERSION);
|
|
|
|
testData[KEY_ENGINE_VERSION] = Value(cocos2d::cocos2dVersion());
|
|
|
|
time_t t = time(0);
|
|
|
|
localtime(&t);
|
|
|
|
testData[KEY_TIMESTAMP] = Value(genStr("%ld", t));
|
|
|
|
|
2015-09-09 17:00:38 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
2015-09-14 09:52:21 +08:00
|
|
|
std::string checkPath = "/mnt/sdcard/PerfTest";
|
2015-09-09 17:00:38 +08:00
|
|
|
auto writablePath = checkPath;
|
|
|
|
if (! cocos2d::FileUtils::getInstance()->isDirectoryExist(checkPath)) {
|
2015-09-14 09:52:21 +08:00
|
|
|
auto createRet = cocos2d::FileUtils::getInstance()->createDirectory(checkPath);
|
|
|
|
if (! createRet) {
|
|
|
|
writablePath = cocos2d::FileUtils::getInstance()->getWritablePath();
|
|
|
|
}
|
2015-09-09 17:00:38 +08:00
|
|
|
}
|
|
|
|
cocos2d::log("write path : %s", writablePath.c_str());
|
|
|
|
#else
|
2015-08-28 14:54:12 +08:00
|
|
|
auto writablePath = cocos2d::FileUtils::getInstance()->getWritablePath();
|
2015-09-09 17:00:38 +08:00
|
|
|
#endif
|
2015-09-08 15:29:56 +08:00
|
|
|
|
|
|
|
#if USE_JSON_FORMAT
|
2015-09-14 09:52:21 +08:00
|
|
|
char timeStr[64];
|
|
|
|
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d-%H%M", localtime(&t));
|
|
|
|
std::string fileName = genStr(LOG_FILE_NAME_FMT, DEVICE_NAME, timeStr);
|
|
|
|
std::string fullPath = genStr("%s/%s", writablePath.c_str(), fileName.c_str());
|
|
|
|
|
2015-08-31 16:57:46 +08:00
|
|
|
rapidjson::Document document;
|
|
|
|
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
|
|
|
|
rapidjson::Value theData = valueMapToJson(testData, allocator);
|
|
|
|
|
|
|
|
rapidjson::StringBuffer buffer;
|
2015-09-02 11:21:17 +08:00
|
|
|
|
2015-09-08 15:29:56 +08:00
|
|
|
#if USE_PRETTY_OUTPUT_FORMAT
|
2015-09-02 11:21:17 +08:00
|
|
|
// write pretty format json
|
2015-08-31 16:57:46 +08:00
|
|
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
2015-09-08 15:29:56 +08:00
|
|
|
#else // #else USE_PRETTY_OUTPUT_FORMAT
|
2015-09-02 11:21:17 +08:00
|
|
|
// write json in one line
|
2015-09-08 15:29:56 +08:00
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
|
|
|
|
#endif // #endif USE_PRETTY_OUTPUT_FORMAT
|
2015-09-02 11:21:17 +08:00
|
|
|
|
2015-08-31 16:57:46 +08:00
|
|
|
theData.Accept(writer);
|
|
|
|
auto out = buffer.GetString();
|
|
|
|
|
|
|
|
FILE *fp = fopen(fullPath.c_str(), "w");
|
|
|
|
fputs(out, fp);
|
|
|
|
fclose(fp);
|
2015-09-08 15:29:56 +08:00
|
|
|
#else // #else USE_JSON_FORMAT
|
|
|
|
// Write the test data into plist file.
|
|
|
|
std::string plistFullPath = genStr("%s/%s", writablePath.c_str(), PLIST_FILE_NAME);
|
|
|
|
cocos2d::FileUtils::getInstance()->writeValueMapToFile(testData, plistFullPath);
|
|
|
|
#endif // #endif USE_JSON_FORMAT
|
2015-08-28 14:54:12 +08:00
|
|
|
}
|