perftest.h 4.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (C) 2011 Milo Yip
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

21 22 23
#ifndef PERFTEST_H_
#define PERFTEST_H_

24 25 26
#define TEST_RAPIDJSON  1
#define TEST_JSONCPP    0
#define TEST_YAJL       0
27 28
#define TEST_ULTRAJSON  0
#define TEST_PLATFORM   0
29
#define TEST_MISC       0
30

31 32 33 34 35
#define TEST_VERSION_CODE(x,y,z) \
  (((x)*100000) + ((y)*100) + (z))

// Only gcc >4.3 supports SSE4.2
#if TEST_RAPIDJSON && !(defined(__GNUC__) && TEST_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) < TEST_VERSION_CODE(4,3,0))
36
//#define RAPIDJSON_SSE2
37
#define RAPIDJSON_SSE42
38 39 40 41 42 43 44 45 46 47 48 49 50
#endif

#if TEST_YAJL
#include "yajl/yajl_common.h"
#undef YAJL_MAX_DEPTH
#define YAJL_MAX_DEPTH 1024
#endif

////////////////////////////////////////////////////////////////////////////////
// Google Test

#ifdef __cplusplus

Milo Yip's avatar
Milo Yip committed
51 52 53 54 55
// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
#ifndef __STDC_CONSTANT_MACROS
#  define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#endif

56 57 58 59
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
#if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic push
#endif
60
#pragma GCC diagnostic ignored "-Weffc++"
Milo Yip's avatar
Milo Yip committed
61 62
#endif

63 64
#include "gtest/gtest.h"

65
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
Milo Yip's avatar
Milo Yip committed
66 67 68
#pragma GCC diagnostic pop
#endif

69 70 71 72 73 74 75 76 77
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated
#endif

//! Base class for all performance tests
class PerfTest : public ::testing::Test {
public:
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}

    virtual void SetUp() {
        FILE *fp = fopen(filename_ = "data/sample.json", "rb");
        if (!fp) 
            fp = fopen(filename_ = "../../bin/data/sample.json", "rb");
        ASSERT_TRUE(fp != 0);

        fseek(fp, 0, SEEK_END);
        length_ = (size_t)ftell(fp);
        fseek(fp, 0, SEEK_SET);
        json_ = (char*)malloc(length_ + 1);
        ASSERT_EQ(length_, fread(json_, 1, length_, fp));
        json_[length_] = '\0';
        fclose(fp);

        // whitespace test
        whitespace_length_ = 1024 * 1024;
        whitespace_ = (char *)malloc(whitespace_length_  + 4);
        char *p = whitespace_;
        for (size_t i = 0; i < whitespace_length_; i += 4) {
            *p++ = ' ';
            *p++ = '\n';
            *p++ = '\r';
            *p++ = '\t';
        }
        *p++ = '[';
        *p++ = '0';
        *p++ = ']';
        *p++ = '\0';
    }

    virtual void TearDown() {
        free(json_);
        free(whitespace_);
        json_ = 0;
        whitespace_ = 0;
    }
116

Milo Yip's avatar
Milo Yip committed
117
private:
118 119
    PerfTest(const PerfTest&);
    PerfTest& operator=(const PerfTest&);
Milo Yip's avatar
Milo Yip committed
120

121
protected:
122 123 124 125 126
    const char* filename_;
    char *json_;
    size_t length_;
    char *whitespace_;
    size_t whitespace_length_;
127

128
    static const size_t kTrialCount = 1000;
129 130 131 132 133
};

#endif // __cplusplus

#endif // PERFTEST_H_