perftest.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef PERFTEST_H_
  15. #define PERFTEST_H_
  16. #define TEST_RAPIDJSON 1
  17. #define TEST_PLATFORM 0
  18. #define TEST_MISC 0
  19. #define TEST_VERSION_CODE(x,y,z) \
  20. (((x)*100000) + ((y)*100) + (z))
  21. // __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
  22. // We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
  23. // Likewise, __ARM_NEON is used to detect Neon.
  24. #if defined(__SSE4_2__)
  25. # define RAPIDJSON_SSE42
  26. #elif defined(__SSE2__)
  27. # define RAPIDJSON_SSE2
  28. #elif defined(__ARM_NEON)
  29. # define RAPIDJSON_NEON
  30. #endif
  31. #define RAPIDJSON_HAS_STDSTRING 1
  32. ////////////////////////////////////////////////////////////////////////////////
  33. // Google Test
  34. #ifdef __cplusplus
  35. // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
  36. #ifndef __STDC_CONSTANT_MACROS
  37. # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
  38. #endif
  39. #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
  40. #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  41. #pragma GCC diagnostic push
  42. #endif
  43. #pragma GCC diagnostic ignored "-Weffc++"
  44. #endif
  45. #include "gtest/gtest.h"
  46. #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  47. #pragma GCC diagnostic pop
  48. #endif
  49. #ifdef _MSC_VER
  50. #define _CRTDBG_MAP_ALLOC
  51. #include <crtdbg.h>
  52. #pragma warning(disable : 4996) // 'function': was declared deprecated
  53. #endif
  54. //! Base class for all performance tests
  55. class PerfTest : public ::testing::Test {
  56. public:
  57. PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}
  58. virtual void SetUp() {
  59. {
  60. const char *paths[] = {
  61. "data/sample.json",
  62. "bin/data/sample.json",
  63. "../bin/data/sample.json",
  64. "../../bin/data/sample.json",
  65. "../../../bin/data/sample.json"
  66. };
  67. FILE *fp = 0;
  68. for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  69. fp = fopen(filename_ = paths[i], "rb");
  70. if (fp)
  71. break;
  72. }
  73. ASSERT_TRUE(fp != 0);
  74. fseek(fp, 0, SEEK_END);
  75. length_ = (size_t)ftell(fp);
  76. fseek(fp, 0, SEEK_SET);
  77. json_ = (char*)malloc(length_ + 1);
  78. ASSERT_EQ(length_, fread(json_, 1, length_, fp));
  79. json_[length_] = '\0';
  80. fclose(fp);
  81. }
  82. // whitespace test
  83. {
  84. whitespace_length_ = 1024 * 1024;
  85. whitespace_ = (char *)malloc(whitespace_length_ + 4);
  86. char *p = whitespace_;
  87. for (size_t i = 0; i < whitespace_length_; i += 4) {
  88. *p++ = ' ';
  89. *p++ = '\n';
  90. *p++ = '\r';
  91. *p++ = '\t';
  92. }
  93. *p++ = '[';
  94. *p++ = '0';
  95. *p++ = ']';
  96. *p++ = '\0';
  97. }
  98. // types test
  99. {
  100. const char *typespaths[] = {
  101. "data/types",
  102. "bin/types",
  103. "../bin/types",
  104. "../../bin/types/",
  105. "../../../bin/types"
  106. };
  107. const char* typesfilenames[] = {
  108. "booleans.json",
  109. "floats.json",
  110. "guids.json",
  111. "integers.json",
  112. "mixed.json",
  113. "nulls.json",
  114. "paragraphs.json"
  115. };
  116. for (size_t j = 0; j < sizeof(typesfilenames) / sizeof(typesfilenames[0]); j++) {
  117. types_[j] = 0;
  118. for (size_t i = 0; i < sizeof(typespaths) / sizeof(typespaths[0]); i++) {
  119. char filename[256];
  120. sprintf(filename, "%s/%s", typespaths[i], typesfilenames[j]);
  121. if (FILE* fp = fopen(filename, "rb")) {
  122. fseek(fp, 0, SEEK_END);
  123. typesLength_[j] = (size_t)ftell(fp);
  124. fseek(fp, 0, SEEK_SET);
  125. types_[j] = (char*)malloc(typesLength_[j] + 1);
  126. ASSERT_EQ(typesLength_[j], fread(types_[j], 1, typesLength_[j], fp));
  127. types_[j][typesLength_[j]] = '\0';
  128. fclose(fp);
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. virtual void TearDown() {
  136. free(json_);
  137. free(whitespace_);
  138. json_ = 0;
  139. whitespace_ = 0;
  140. for (size_t i = 0; i < 7; i++) {
  141. free(types_[i]);
  142. types_[i] = 0;
  143. }
  144. }
  145. private:
  146. PerfTest(const PerfTest&);
  147. PerfTest& operator=(const PerfTest&);
  148. protected:
  149. const char* filename_;
  150. char *json_;
  151. size_t length_;
  152. char *whitespace_;
  153. size_t whitespace_length_;
  154. char *types_[7];
  155. size_t typesLength_[7];
  156. static const size_t kTrialCount = 1000;
  157. };
  158. #endif // __cplusplus
  159. #endif // PERFTEST_H_