perftest.h 5.7 KB

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