perftest.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef PERFTEST_H_
  2. #define PERFTEST_H_
  3. #define TEST_RAPIDJSON 1
  4. #define TEST_JSONCPP 0
  5. #define TEST_YAJL 0
  6. #define TEST_ULTRAJSON 0
  7. #define TEST_PLATFORM 0
  8. #define TEST_MISC 0
  9. #if TEST_RAPIDJSON
  10. //#define RAPIDJSON_SSE2
  11. #define RAPIDJSON_SSE42
  12. #endif
  13. #if TEST_YAJL
  14. #include "yajl/yajl_common.h"
  15. #undef YAJL_MAX_DEPTH
  16. #define YAJL_MAX_DEPTH 1024
  17. #endif
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // Google Test
  20. #ifdef __cplusplus
  21. #include "gtest/gtest.h"
  22. #ifdef _MSC_VER
  23. #define _CRTDBG_MAP_ALLOC
  24. #include <crtdbg.h>
  25. #pragma warning(disable : 4996) // 'function': was declared deprecated
  26. #endif
  27. //! Base class for all performance tests
  28. class PerfTest : public ::testing::Test {
  29. public:
  30. virtual void SetUp() {
  31. FILE *fp = fopen(filename_ = "data/sample.json", "rb");
  32. if (!fp)
  33. fp = fopen(filename_ = "../../bin/data/sample.json", "rb");
  34. ASSERT_TRUE(fp != 0);
  35. fseek(fp, 0, SEEK_END);
  36. length_ = (size_t)ftell(fp);
  37. fseek(fp, 0, SEEK_SET);
  38. json_ = (char*)malloc(length_ + 1);
  39. ASSERT_EQ(length_, fread(json_, 1, length_, fp));
  40. json_[length_] = '\0';
  41. fclose(fp);
  42. // whitespace test
  43. whitespace_length_ = 1024 * 1024;
  44. whitespace_ = (char *)malloc(whitespace_length_ + 4);
  45. char *p = whitespace_;
  46. for (size_t i = 0; i < whitespace_length_; i += 4) {
  47. *p++ = ' ';
  48. *p++ = '\n';
  49. *p++ = '\r';
  50. *p++ = '\t';
  51. }
  52. *p++ = '[';
  53. *p++ = '0';
  54. *p++ = ']';
  55. *p++ = '\0';
  56. }
  57. virtual void TearDown() {
  58. free(json_);
  59. free(whitespace_);
  60. }
  61. protected:
  62. const char* filename_;
  63. char *json_;
  64. size_t length_;
  65. char *whitespace_;
  66. size_t whitespace_length_;
  67. static const size_t kTrialCount = 1000;
  68. };
  69. #endif // __cplusplus
  70. #endif // PERFTEST_H_