perftest.h 2.56 KB
Newer Older
1 2 3
#ifndef PERFTEST_H_
#define PERFTEST_H_

4
#define TEST_RAPIDJSON	1
5 6 7 8
#define TEST_JSONCPP	0
#define TEST_YAJL		0
#define TEST_ULTRAJSON  0
#define TEST_PLATFORM   0
9
#define TEST_MISC		0
10

11 12
#if TEST_RAPIDJSON
//#define RAPIDJSON_SSE2
13
#define RAPIDJSON_SSE42
14 15 16 17 18 19 20 21 22 23 24 25 26
#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
27 28 29 30 31
// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
#ifndef __STDC_CONSTANT_MACROS
#  define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#endif

32 33 34 35
#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
36
#pragma GCC diagnostic ignored "-Weffc++"
Milo Yip's avatar
Milo Yip committed
37 38
#endif

39 40
#include "gtest/gtest.h"

41
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
Milo Yip's avatar
Milo Yip committed
42 43 44
#pragma GCC diagnostic pop
#endif

45 46 47 48 49 50 51 52 53
#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:
Milo Yip's avatar
Milo Yip committed
54 55
	PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}

56
	virtual void SetUp() {
57 58 59
		FILE *fp = fopen(filename_ = "data/sample.json", "rb");
		if (!fp) 
			fp = fopen(filename_ = "../../bin/data/sample.json", "rb");
60 61 62 63 64 65
		ASSERT_TRUE(fp != 0);

		fseek(fp, 0, SEEK_END);
		length_ = (size_t)ftell(fp);
		fseek(fp, 0, SEEK_SET);
		json_ = (char*)malloc(length_ + 1);
66
		ASSERT_EQ(length_, fread(json_, 1, length_, fp));
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		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_);
Milo Yip's avatar
Milo Yip committed
89 90
		json_ = 0;
		whitespace_ = 0;
91 92
	}

Milo Yip's avatar
Milo Yip committed
93 94 95 96
private:
	PerfTest(const PerfTest&);
	PerfTest& operator=(const PerfTest&);

97
protected:
98
	const char* filename_;
99 100 101 102 103 104 105 106 107 108 109
	char *json_;
	size_t length_;
	char *whitespace_;
	size_t whitespace_length_;

	static const size_t kTrialCount = 1000;
};

#endif // __cplusplus

#endif // PERFTEST_H_