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
15#ifndef PERFTEST_H_
16#define PERFTEST_H_
17
18#define TEST_RAPIDJSON  1
19#define TEST_PLATFORM   0
20#define TEST_MISC       0
21
22#define TEST_VERSION_CODE(x,y,z) \
23  (((x)*100000) + ((y)*100) + (z))
24
25// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
26// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
27#if defined(__SSE4_2__)
28#  define RAPIDJSON_SSE42
29#elif defined(__SSE2__)
30#  define RAPIDJSON_SSE2
31#endif
32
33////////////////////////////////////////////////////////////////////////////////
34// Google Test
35
36#ifdef __cplusplus
37
38// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
39#ifndef __STDC_CONSTANT_MACROS
40#  define __STDC_CONSTANT_MACROS 1 // required by C++ standard
41#endif
42
43#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
44#if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
45#pragma GCC diagnostic push
46#endif
47#pragma GCC diagnostic ignored "-Weffc++"
48#endif
49
50#include "gtest/gtest.h"
51
52#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
53#pragma GCC diagnostic pop
54#endif
55
56#ifdef _MSC_VER
57#define _CRTDBG_MAP_ALLOC
58#include <crtdbg.h>
59#pragma warning(disable : 4996) // 'function': was declared deprecated
60#endif
61
62//! Base class for all performance tests
63class PerfTest : public ::testing::Test {
64public:
65    PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}
66
67    virtual void SetUp() {
68
69        const char *paths[] = {
70            "data/sample.json",
71            "bin/data/sample.json",
72            "../bin/data/sample.json",
73            "../../bin/data/sample.json",
74            "../../../bin/data/sample.json"
75        };
76        FILE *fp = 0;
77        for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
78            fp = fopen(filename_ = paths[i], "rb");
79            if (fp)
80                break;
81        }
82        ASSERT_TRUE(fp != 0);
83
84        fseek(fp, 0, SEEK_END);
85        length_ = (size_t)ftell(fp);
86        fseek(fp, 0, SEEK_SET);
87        json_ = (char*)malloc(length_ + 1);
88        ASSERT_EQ(length_, fread(json_, 1, length_, fp));
89        json_[length_] = '\0';
90        fclose(fp);
91
92        // whitespace test
93        whitespace_length_ = 1024 * 1024;
94        whitespace_ = (char *)malloc(whitespace_length_  + 4);
95        char *p = whitespace_;
96        for (size_t i = 0; i < whitespace_length_; i += 4) {
97            *p++ = ' ';
98            *p++ = '\n';
99            *p++ = '\r';
100            *p++ = '\t';
101        }
102        *p++ = '[';
103        *p++ = '0';
104        *p++ = ']';
105        *p++ = '\0';
106    }
107
108    virtual void TearDown() {
109        free(json_);
110        free(whitespace_);
111        json_ = 0;
112        whitespace_ = 0;
113    }
114
115private:
116    PerfTest(const PerfTest&);
117    PerfTest& operator=(const PerfTest&);
118
119protected:
120    const char* filename_;
121    char *json_;
122    size_t length_;
123    char *whitespace_;
124    size_t whitespace_length_;
125
126    static const size_t kTrialCount = 1000;
127};
128
129#endif // __cplusplus
130
131#endif // PERFTEST_H_
132