perftest.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #if defined(__SSE4_2__)
  24. # define RAPIDJSON_SSE42
  25. #elif defined(__SSE2__)
  26. # define RAPIDJSON_SSE2
  27. #endif
  28. #define RAPIDJSON_HAS_STDSTRING 1
  29. ////////////////////////////////////////////////////////////////////////////////
  30. // Google Test
  31. #ifdef __cplusplus
  32. // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
  33. #ifndef __STDC_CONSTANT_MACROS
  34. # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
  35. #endif
  36. #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
  37. #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  38. #pragma GCC diagnostic push
  39. #endif
  40. #pragma GCC diagnostic ignored "-Weffc++"
  41. #endif
  42. #include "gtest/gtest.h"
  43. #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  44. #pragma GCC diagnostic pop
  45. #endif
  46. #ifdef _MSC_VER
  47. #define _CRTDBG_MAP_ALLOC
  48. #include <crtdbg.h>
  49. #pragma warning(disable : 4996) // 'function': was declared deprecated
  50. #endif
  51. //! Base class for all performance tests
  52. class PerfTest : public ::testing::Test {
  53. public:
  54. PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}
  55. virtual void SetUp() {
  56. {
  57. const char *paths[] = {
  58. "data/sample.json",
  59. "bin/data/sample.json",
  60. "../bin/data/sample.json",
  61. "../../bin/data/sample.json",
  62. "../../../bin/data/sample.json"
  63. };
  64. FILE *fp = 0;
  65. for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  66. fp = fopen(filename_ = paths[i], "rb");
  67. if (fp)
  68. break;
  69. }
  70. ASSERT_TRUE(fp != 0);
  71. fseek(fp, 0, SEEK_END);
  72. length_ = (size_t)ftell(fp);
  73. fseek(fp, 0, SEEK_SET);
  74. json_ = (char*)malloc(length_ + 1);
  75. ASSERT_EQ(length_, fread(json_, 1, length_, fp));
  76. json_[length_] = '\0';
  77. fclose(fp);
  78. }
  79. // whitespace test
  80. {
  81. whitespace_length_ = 1024 * 1024;
  82. whitespace_ = (char *)malloc(whitespace_length_ + 4);
  83. char *p = whitespace_;
  84. for (size_t i = 0; i < whitespace_length_; i += 4) {
  85. *p++ = ' ';
  86. *p++ = '\n';
  87. *p++ = '\r';
  88. *p++ = '\t';
  89. }
  90. *p++ = '[';
  91. *p++ = '0';
  92. *p++ = ']';
  93. *p++ = '\0';
  94. }
  95. // types test
  96. {
  97. const char *typespaths[] = {
  98. "data/types",
  99. "bin/types",
  100. "../bin/types",
  101. "../../bin/types/",
  102. "../../../bin/types"
  103. };
  104. const char* typesfilenames[] = {
  105. "booleans.json",
  106. "floats.json",
  107. "guids.json",
  108. "integers.json",
  109. "mixed.json",
  110. "nulls.json",
  111. "paragraphs.json"
  112. };
  113. for (size_t j = 0; j < sizeof(typesfilenames) / sizeof(typesfilenames[0]); j++) {
  114. types_[j] = 0;
  115. for (size_t i = 0; i < sizeof(typespaths) / sizeof(typespaths[0]); i++) {
  116. char filename[256];
  117. sprintf(filename, "%s/%s", typespaths[i], typesfilenames[j]);
  118. if (FILE* fp = fopen(filename, "rb")) {
  119. fseek(fp, 0, SEEK_END);
  120. typesLength_[j] = (size_t)ftell(fp);
  121. fseek(fp, 0, SEEK_SET);
  122. types_[j] = (char*)malloc(typesLength_[j] + 1);
  123. ASSERT_EQ(typesLength_[j], fread(types_[j], 1, typesLength_[j], fp));
  124. types_[j][typesLength_[j]] = '\0';
  125. fclose(fp);
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. virtual void TearDown() {
  133. free(json_);
  134. free(whitespace_);
  135. json_ = 0;
  136. whitespace_ = 0;
  137. for (size_t i = 0; i < 7; i++) {
  138. free(types_[i]);
  139. types_[i] = 0;
  140. }
  141. }
  142. private:
  143. PerfTest(const PerfTest&);
  144. PerfTest& operator=(const PerfTest&);
  145. protected:
  146. const char* filename_;
  147. char *json_;
  148. size_t length_;
  149. char *whitespace_;
  150. size_t whitespace_length_;
  151. char *types_[7];
  152. size_t typesLength_[7];
  153. static const size_t kTrialCount = 1000;
  154. };
  155. #endif // __cplusplus
  156. #endif // PERFTEST_H_